diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 4d44da8e0d..be1ea4b7ba 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -43,6 +43,10 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * moved the data collector to the bridge * replaced MessageLogger class with the one from Swiftmailer 4.1.3 +### TwigBundle + + * added the real template name when an error occurs in a Twig template + ### WebProfilerBundle * added a routing panel diff --git a/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php b/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php index 1e3eedaf25..54037d6c61 100644 --- a/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/Debug/TimedTwigEngine.php @@ -15,6 +15,7 @@ use Symfony\Bundle\TwigBundle\TwigEngine; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; use Symfony\Component\Templating\TemplateNameParserInterface; use Symfony\Component\HttpKernel\Debug\Stopwatch; +use Symfony\Component\Config\FileLocatorInterface; /** * Times the time spent to render a template. @@ -33,9 +34,9 @@ class TimedTwigEngine extends TwigEngine * @param GlobalVariables|null $globals A GlobalVariables instance or null * @param Stopwatch $stopwatch A Stopwatch instance */ - public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, Stopwatch $stopwatch, GlobalVariables $globals = null) + public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator, Stopwatch $stopwatch, GlobalVariables $globals = null) { - parent::__construct($environment, $parser, $globals); + parent::__construct($environment, $parser, $locator, $globals); $this->stopwatch = $stopwatch; } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml index b83eb8b0dc..cf8e8a0466 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml @@ -12,6 +12,7 @@ + diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index e334253dfc..7c24db417d 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -40,6 +40,7 @@ + diff --git a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php index 23889abd7a..77b79dd545 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/TwigEngineTest.php @@ -25,7 +25,8 @@ class TwigEngineTest extends TestCase { $environment = $this->getTwigEnvironment(); $container = $this->getContainer(); - $engine = new TwigEngine($environment, new TemplateNameParser(), $app = new GlobalVariables($container)); + $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface'); + $engine = new TwigEngine($environment, new TemplateNameParser(), $locator, $app = new GlobalVariables($container)); $template = $this->getMock('\Twig_TemplateInterface'); @@ -44,7 +45,8 @@ class TwigEngineTest extends TestCase { $environment = $this->getTwigEnvironment(); $container = new Container(); - $engine = new TwigEngine($environment, new TemplateNameParser(), new GlobalVariables($container)); + $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface'); + $engine = new TwigEngine($environment, new TemplateNameParser(), $locator, new GlobalVariables($container)); $template = $this->getMock('\Twig_TemplateInterface'); diff --git a/src/Symfony/Bundle/TwigBundle/TwigEngine.php b/src/Symfony/Bundle/TwigBundle/TwigEngine.php index 639212acc3..5f73f6780e 100644 --- a/src/Symfony/Bundle/TwigBundle/TwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/TwigEngine.php @@ -13,8 +13,10 @@ namespace Symfony\Bundle\TwigBundle; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; +use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; use Symfony\Component\Templating\TemplateNameParserInterface; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Config\FileLocatorInterface; /** * This engine knows how to render Twig templates. @@ -25,6 +27,7 @@ class TwigEngine implements EngineInterface { protected $environment; protected $parser; + protected $locator; /** * Constructor. @@ -33,10 +36,11 @@ class TwigEngine implements EngineInterface * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance * @param GlobalVariables|null $globals A GlobalVariables instance or null */ - public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, GlobalVariables $globals = null) + public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, FileLocatorInterface $locator, GlobalVariables $globals = null) { $this->environment = $environment; $this->parser = $parser; + $this->locator = $locator; if (null !== $globals) { $environment->addGlobal('app', $globals); @@ -56,7 +60,19 @@ class TwigEngine implements EngineInterface */ public function render($name, array $parameters = array()) { - return $this->load($name)->render($parameters); + try { + return $this->load($name)->render($parameters); + } catch (\Exception $e) { + if ($name instanceof TemplateReference) { + try { + // try to get the real file name of the template where the error occurred + $e->setTemplateFile(sprintf('%s', $this->locator->locate($this->parser->parse($e->getTemplateFile())))); + } catch (\Exception $ex) { + } + } + + throw $e; + } } /** diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ExceptionControllerTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ExceptionControllerTest.php index 2417aff93b..db390ece60 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ExceptionControllerTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ExceptionControllerTest.php @@ -76,6 +76,7 @@ class ExceptionControllerTest extends TestCase $container->register('templating.engine.twig',$this->getMockClass('Symfony\\Bundle\\TwigBundle\\TwigEngine')) ->addArgument($this->getMock('Twig_Environment')) ->addArgument($this->getMock('Symfony\\Component\\Templating\\TemplateNameParserInterface')) + ->addArgument(new Definition($this->getMockClass('Symfony\Component\Config\FileLocatorInterface'))) ->addArgument($this->getMock('Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables', array(), array($this->getMock('Symfony\\Component\\DependencyInjection\\Container')))); $container->setAlias('templating', 'templating.engine.twig'); $container->setParameter('kernel.bundles', array()); diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php index 776f604615..61de024a6f 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php @@ -60,6 +60,7 @@ class WebProfilerExtensionTest extends TestCase $this->container->register('templating.engine.twig', $this->getMockClass('Symfony\\Bundle\\TwigBundle\\TwigEngine')) ->addArgument(new Definition($this->getMockClass('Twig_Environment'))) ->addArgument(new Definition($this->getMockClass('Symfony\\Component\\Templating\\TemplateNameParserInterface'))) + ->addArgument(new Definition($this->getMockClass('Symfony\Component\Config\FileLocatorInterface'))) ->addArgument(new Definition($this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables'), array(new Definition($this->getMockClass('Symfony\\Component\\DependencyInjection\\Container'))))); $this->container->setParameter('kernel.bundles', array()); $this->container->setParameter('kernel.cache_dir', __DIR__);