Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Symfony/Bundle/TwigBundle/ContainerAwareRuntimeLoader.php
Original file line number Diff line number Diff line change
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));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about simply injecting a null/void logger if non is configured?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I don't like forcing using a Logger if not available.

}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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'));
}
}