diff --git a/src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php b/src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php index 89607a55912f9..780454aed986e 100644 --- a/src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php +++ b/src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\TwigBundle; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -22,11 +23,13 @@ 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; } /** @@ -34,10 +37,12 @@ public function __construct(ContainerInterface $container, array $mapping) */ 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)); + } } } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 075788624a9c6..2720b11cfb933 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -165,6 +165,7 @@ + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/ContainerAwareRuntimeLoaderTest.php b/src/Symfony/Bundle/TwigBundle/Tests/ContainerAwareRuntimeLoaderTest.php index 82862289624d6..1e11e63e48687 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/ContainerAwareRuntimeLoaderTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/ContainerAwareRuntimeLoaderTest.php @@ -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')); } }