From aa7cbbd2053f56564450313d5d2e763df17a2a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20S=C3=A1nchez?= Date: Wed, 22 Jul 2015 16:34:23 -0300 Subject: [PATCH 1/3] Introduce failing test case when a SplFileInfo object is passed to the extract() method in the TwigExtractor. The problem is that when there's a twig error, symfony expects the `getRelativePath` method that the native object doesn't have. --- .../Twig/Tests/Translation/TwigExtractorTest.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index 94ac80d884..f2b15fc5da 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -74,14 +74,26 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase /** * @expectedException \Twig_Error * @expectedExceptionMessageRegExp /Unclosed "block" in "extractor(\/|\\)syntax_error\.twig" at line 1/ + * @dataProvider resourcesWithSyntaxErrorsProvider */ - public function testExtractSyntaxError() + public function testExtractSyntaxError($resources) { $twig = new \Twig_Environment(new \Twig_Loader_Array(array())); $twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface'))); $extractor = new TwigExtractor($twig); - $extractor->extract(__DIR__.'/../Fixtures', new MessageCatalogue('en')); + $extractor->extract($resources, new MessageCatalogue('en')); + } + + /** + * @return array + */ + public function resourcesWithSyntaxErrorsProvider() + { + return array( + array(__DIR__.'/../Fixtures'), + array(new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig')), + ); } /** From 2bf78e5f20d0d6ce60e052c9dcb0ae31da604305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20S=C3=A1nchez?= Date: Thu, 23 Jul 2015 09:53:04 -0300 Subject: [PATCH 2/3] Resources as string have the same problem --- src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index f2b15fc5da..fb860ec2ad 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -92,6 +92,7 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase { return array( array(__DIR__.'/../Fixtures'), + array(__DIR__.'/../Fixtures/extractor/syntax_error.twig'), array(new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig')), ); } From 1e15761ac722e2459ebc775025e33f22cc996cc1 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 26 Jul 2015 08:27:26 +0200 Subject: [PATCH 3/3] [TwigBridge] type-dependent path discovery With the introduction of the `AbstractFileExtractor` in Symfony 2.7, the `extract()` method in the `TwigExtractor` class does not necessarily deal with `SplFileInfo` instances from the Finder component, but also receives `\SplFileInfo` objects initialized by the base extractor class. --- .../Bridge/Twig/Tests/Translation/TwigExtractorTest.php | 2 +- src/Symfony/Bridge/Twig/Translation/TwigExtractor.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index fb860ec2ad..610d5a6068 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -73,7 +73,7 @@ class TwigExtractorTest extends \PHPUnit_Framework_TestCase /** * @expectedException \Twig_Error - * @expectedExceptionMessageRegExp /Unclosed "block" in "extractor(\/|\\)syntax_error\.twig" at line 1/ + * @expectedExceptionMessageRegExp /Unclosed "block" in ".*extractor(\/|\\)syntax_error\.twig" at line 1/ * @dataProvider resourcesWithSyntaxErrorsProvider */ public function testExtractSyntaxError($resources) diff --git a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php index ad3fd9fe33..d892fe7303 100644 --- a/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php +++ b/src/Symfony/Bridge/Twig/Translation/TwigExtractor.php @@ -12,6 +12,7 @@ namespace Symfony\Bridge\Twig\Translation; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use Symfony\Component\Translation\Extractor\AbstractFileExtractor; use Symfony\Component\Translation\Extractor\ExtractorInterface; use Symfony\Component\Translation\MessageCatalogue; @@ -60,7 +61,11 @@ class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface try { $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue); } catch (\Twig_Error $e) { - $e->setTemplateFile($file->getRelativePathname()); + if ($file instanceof SplFileInfo) { + $e->setTemplateFile($file->getRelativePathname()); + } elseif ($file instanceof \SplFileInfo) { + $e->setTemplateFile($file->getRealPath()); + } throw $e; }