8000 [TwigBundle] changed the runtime loader to return null if there is no… · symfony/symfony@c03fd80 · GitHub
[go: up one dir, main page]

Skip to content

Commit c03fd80

Browse files
committed
[TwigBundle] changed the runtime loader to return null if there is no match
1 parent 362a8ac commit c03fd80

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\TwigBundle;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\DependencyInjection\ContainerInterface;
1516

1617
/**
@@ -22,22 +23,26 @@ class ContainerAwareRuntimeLoader implements \Twig_RuntimeLoaderInterface
2223
{
2324
private $container;
2425
private $mapping;
26+
private $logger;
2527

26-
public function __construct(ContainerInterface $container, array $mapping)
28+
public function __construct(ContainerInterface $container, array $mapping, LoggerInterface $logger = null)
2729
{
2830
$this->container = $container;
2931
$this->mapping = $mapping;
32+
$this->logger = $logger;
3033
}
3134

3235
/**
3336
* {@inheritdoc}
3437
*/
3538
public function load($class)
3639
{
37-
if (!isset($this->mapping[$class])) {
38-
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));
40+
if (isset($this->mapping[$class])) {
41+
return $this->container->get($this->mapping[$class]);
3942
}
4043

41-
return $this->container->get($this->mapping[$class]);
44+
if (null !== $this->logger) {
45+
$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));
46+
}
4247
}
4348
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<service id="twig.runtime_loader" class="Symfony\Bundle\TwigBundle\ContainerAwareRuntimeLoader" public="false">
166166
<argument type="service" id="service_container" />
167167
<argument type="collection" /> <!-- the mapping between class names and service names -->
168+
<argument type="service" id="logger" on-invalid="null" />
168169
</service>
169170
</services>
170171
</container>

src/Symfony/Bundle/TwigBundle/Tests/ContainerAwareRuntimeLoaderTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\Tests;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\DependencyInjection\ContainerInterface;
1516
use Symfony\Bundle\TwigBundle\ContainerAwareRuntimeLoader;
1617

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

30-
/**
31-
* @expectedException \LogicException
32-
* @expectedExceptionMessage Class "BarClass" is not configured as a Twig runtime.
33-
*/
3431
public function testLoadWithoutAMatch()
3532
{
36-
$loader = new ContainerAwareRuntimeLoader($this->getMockBuilder(ContainerInterface::class)->getMock(), array());
37-
$loader->load('BarClass');
33+
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
34+
$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.');
35+
$loader = new ContainerAwareRuntimeLoader($this->getMockBuilder(ContainerInterface::class)->getMock(), array(), $logger);
36+
$this->assertNull($loader->load('BarClass'));
3837
}
3938
}

0 commit comments

Comments
 (0)
0