Skip to content

Commit

Permalink
feature #20119 [TwigBundle] changed the runtime loader to return null…
Browse files Browse the repository at this point in the history
… if there is no match (fabpot)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[TwigBundle] changed the runtime loader to return null if there is no match

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

ping @stof

Commits
-------

c03fd80 [TwigBundle] changed the runtime loader to return null if there is no match
  • Loading branch information
fabpot committed Oct 3, 2016
2 parents 8c4ed1b + c03fd80 commit 98bd062
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
13 changes: 9 additions & 4 deletions src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\TwigBundle;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -22,22 +23,26 @@ class ContainerAwareRuntimeLoader implements \Twig_RuntimeLoaderInterface
{
private $container;
private $mapping;
private $logger;

public function __construct(ContainerInterface $container, array $mapping)
public function __construct(ContainerInterface $container, array $mapping, LoggerInterface $logger = null)
{
$this->container = $container;
$this->mapping = $mapping;
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
public function load($class)
{
if (!isset($this->mapping[$class])) {
throw new \LogicException(sprintf('Class "%s" is not configured as a Twig runtime. Add the "twig.runtime" tag to the related service in the container.', $class));
if (isset($this->mapping[$class])) {
return $this->container->get($this->mapping[$class]);
}

return $this->container->get($this->mapping[$class]);
if (null !== $this->logger) {
$this->logger->warning(sprintf('Class "%s" is not configured as a Twig runtime. Add the "twig.runtime" tag to the related service in the container.', $class));
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -165,6 +165,7 @@
<service id="twig.runtime_loader" class="Symfony\Bundle\TwigBundle\ContainerAwareRuntimeLoader" public="false">
<argument type="service" id="service_container" />
<argument type="collection" /> <!-- the mapping between class names and service names -->
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Tests;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\ContainerAwareRuntimeLoader;

Expand All @@ -27,13 +28,11 @@ public function testLoad()
$loader->load('FooClass');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage Class "BarClass" is not configured as a Twig runtime.
*/
public function testLoadWithoutAMatch()
{
$loader = new ContainerAwareRuntimeLoader($this->getMockBuilder(ContainerInterface::class)->getMock(), array());
$loader->load('BarClass');
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$logger->expects($this->once())->method('warning')->with('Class "BarClass" is not configured as a Twig runtime. Add the "twig.runtime" tag to the related service in the container.');
$loader = new ContainerAwareRuntimeLoader($this->getMockBuilder(ContainerInterface::class)->getMock(), array(), $logger);
$this->assertNull($loader->load('BarClass'));
}
}

0 comments on commit 98bd062

Please sign in to comment.