[TwigBridge] Added compile-time issues checking in twig:lint command
This commit is contained in:
parent
8f8b20c16f
commit
418cea174d
@ -99,7 +99,7 @@ EOF
|
|||||||
$template .= fread(STDIN, 1024);
|
$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();
|
$filesInfo = array();
|
||||||
@ -121,11 +121,17 @@ EOF
|
|||||||
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
|
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 {
|
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) {
|
} catch (\Twig_Error $e) {
|
||||||
|
$twig->setLoader($realLoader);
|
||||||
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
|
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +57,17 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase
|
|||||||
$ret = $tester->execute(array('filename' => $filename));
|
$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
|
* @return CommandTester
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user