From 9218cacf317c0df071b4c338dd4173c63922328d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 8 Dec 2016 14:23:09 +0100 Subject: [PATCH] [Twig] Fix deprecations with Twig 1.29 --- .../Tests/Translation/TwigExtractorTest.php | 17 ++++++++++++++--- .../Bridge/Twig/Translation/TwigExtractor.php | 12 ++++++++---- src/Symfony/Bundle/TwigBundle/TwigEngine.php | 8 +++++++- .../Resources/views/Profiler/layout.html.twig | 8 +++++--- .../Resources/views/Profiler/toolbar.html.twig | 18 ++++++++++-------- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index 5e7c4ce521..23869da436 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -72,8 +72,7 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \Twig_Error - * @expectedExceptionMessageRegExp /Unclosed "block" in ".*extractor(\/|\\)syntax_error\.twig" at line 1/ + * @expectedException \Twig_Error * @dataProvider resourcesWithSyntaxErrorsProvider */ public function testExtractSyntaxError($resources) @@ -82,7 +81,19 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase $twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface'))); $extractor = new TwigExtractor($twig); - $extractor->extract($resources, new MessageCatalogue('en')); + + try { + $extractor->extract($resources, new MessageCatalogue('en')); + } catch (\Twig_Error $e) { + if (method_exists($e, 'getSourceContext')) { + $this->assertSame(dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', DIRECTORY_SEPARATOR), $e->getFile()); + $this->assertSame(1, $e->getLine()); + $this->assertSame('Unclosed "block".', $e->getMessage()); + } else { + $this->expectExceptionMessageRegExp('/Unclosed "block" in ".*extractor(\\/|\\\\)syntax_error\\.twig" at line 1/'); + } + throw $e; + } } /** diff --git a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php index 950c4d0810..35995dbd64 100644 --- a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php +++ b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php @@ -61,10 +61,14 @@ class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface try { $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue); } catch (\Twig_Error $e) { - if ($file instanceof SplFileInfo) { - $e->setTemplateName($file->getRelativePathname()); - } elseif ($file instanceof \SplFileInfo) { - $e->setTemplateName($file->getRealPath() ?: $file->getPathname()); + if ($file instanceof \SplFileInfo) { + $path = $file->getRealPath() ?: $file->getPathname(); + $name = $file instanceof SplFileInfo ? $file->getRelativePathname() : $path; + if (method_exists($e, 'setSourceContext')) { + $e->setSourceContext(new \Twig_Source('', $name, $path)); + } else { + $e->setTemplateName($name); + } } throw $e; diff --git a/src/Symfony/Bundle/TwigBundle/TwigEngine.php b/src/Symfony/Bundle/TwigBundle/TwigEngine.php index 0d01648d57..fb4b728a5b 100644 --- a/src/Symfony/Bundle/TwigBundle/TwigEngine.php +++ b/src/Symfony/Bundle/TwigBundle/TwigEngine.php @@ -74,7 +74,13 @@ class TwigEngine extends BaseEngine implements EngineInterface if ($name instanceof TemplateReference) { try { // try to get the real name of the template where the error occurred - $e->setTemplateName(sprintf('%s', $this->locator->locate($this->parser->parse($e->getTemplateName())))); + $name = $e->getTemplateName(); + $path = (string) $this->locator->locate($this->parser->parse($name)); + if (method_exists($e, 'setSourceContext')) { + $e->setSourceContext(new \Twig_Source('', $name, $path)); + } else { + $e->setTemplateName($path); + } } catch (\Exception $e2) { } } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig index a3d761deed..37be3ab98d 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/layout.html.twig @@ -37,9 +37,11 @@