feature #10843 [TwigBridge] Added compile-time issues checking in twig:lint command (maxromanovsky)

This PR was merged into the 2.5-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10019
| License       | MIT
|Doc PR	| none

Commits
-------

418cea1 [TwigBridge] Added compile-time issues checking in twig:lint command
This commit is contained in:
Fabien Potencier 2014-05-04 19:06:57 +02:00
commit 19db5f7c4b
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
*/