From 418cea174df1b7dd69926431c34f254a5c41076b Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 2 May 2014 17:24:43 +0000 Subject: [PATCH] [TwigBridge] Added compile-time issues checking in twig:lint command --- src/Symfony/Bridge/Twig/Command/LintCommand.php | 12 +++++++++--- .../Bridge/Twig/Tests/Command/LintCommandTest.php | 11 +++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 79cbfce22b..08525d2daa 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -99,7 +99,7 @@ EOF $template .= fread(STDIN, 1024); } - return $this->display($input, $output, array($this->validate($twig, $template))); + return $this->display($input, $output, array($this->validate($twig, $template, uniqid('sf_')))); } $filesInfo = array(); @@ -121,11 +121,17 @@ EOF throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename)); } - private function validate(\Twig_Environment $twig, $template, $file = null) + private function validate(\Twig_Environment $twig, $template, $file) { + $realLoader = $twig->getLoader(); try { - $twig->parse($twig->tokenize($template, $file ? (string) $file : null)); + $temporaryLoader = new \Twig_Loader_Array(array((string) $file => $template)); + $twig->setLoader($temporaryLoader); + $nodeTree = $twig->parse($twig->tokenize($template, (string) $file)); + $twig->compile($nodeTree); + $twig->setLoader($realLoader); } catch (\Twig_Error $e) { + $twig->setLoader($realLoader); return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e); } diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index 3fe54cbabf..8d5bd99136 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -57,6 +57,17 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase $ret = $tester->execute(array('filename' => $filename)); } + public function testLintFileCompileTimeException() + { + $tester = $this->createCommandTester(); + $filename = $this->createFile("{{ 2|number_format(2, decimal_point='.', ',') }}"); + + $ret = $tester->execute(array('filename' => $filename)); + + $this->assertEquals(1, $ret, 'Returns 1 in case of error'); + $this->assertRegExp('/^KO in /', $tester->getDisplay()); + } + /** * @return CommandTester */