[TwigBridge] Added compile-time issues checking in twig:lint command

This commit is contained in:
Max 2014-05-02 17:24:43 +00:00
parent 8f8b20c16f
commit 418cea174d
2 changed files with 20 additions and 3 deletions

View File

@ -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);
}

View File

@ -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
*/