Skip to content
Permalink
Browse files

feature #20119 [TwigBundle] changed the runtime loader to return null…

… 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 98bd062b0c8eb653b392be0c4af1a1ed68601530
@@ -11,6 +11,7 @@
namespace Symfony\Bundle\TwigBundle;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@@ -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));
}
}
}
@@ -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>
@@ -11,6 +11,7 @@
namespace Symfony\Bundle\TwigBundle\Tests;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\ContainerAwareRuntimeLoader;
@@ -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.
You can’t perform that action at this time.