8000 [TwigBridge] Added compile-time issues checking in twig:lint command · symfony/symfony@418cea1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 418cea1

Browse files
committed
[TwigBridge] Added compile-time issues checking in twig:lint command
1 parent 8f8b20c commit 418cea1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9999
$template .= fread(STDIN, 1024);
100100
}
101101

102-
return $this->display($input, $output, array($this->validate($twig, $template)));
102+
return $this->display($input, $output, array($this->validate($twig, $template, uniqid('sf_'))));
103103
}
104104

105105
$filesInfo = array();
@@ -121,11 +121,17 @@ protected function findFiles($filename)
121121
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
122122
}
123123

124-
private function validate(\Twig_Environment $twig, $template, $file = null)
124+
private function validate(\Twig_Environment $twig, $template, $file)
125125
{
126+
$realLoader = $twig->getLoader();
126127
try {
127-
$twig->parse($twig->tokenize($template, $file ? (string) $file : null));
128+
$temporaryLoader = new \Twig_Loader_Array(array((string) $file => $template));
129+
$twig->setLoader($temporaryLoader);
130+
$nodeTree = $twig->parse($twig->tokenize($template, (string) $file));
131+
$twig->compile($nodeTree);
132+
$twig->setLoader($realLoader);
128133
} catch (\Twig_Error $e) {
134+
$twig->setLoader($realLoader);
129135
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
130136
}
131137

src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ public function testLintFileNotReadable()
5757
$ret = $tester->execute(array('filename' => $filename));
5858
}
5959

60+
public function testLintFileCompileTimeException()
61+
{
62+
$tester = $this->createCommandTester();
63+
$filename = $this->createFile("{{ 2|number_format(2, decimal_point='.', ',') }}");
64+
65+
$ret = $tester->execute(array('filename' => $filename));
66+
67+
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
68+
$this->assertRegExp('/^KO in /', $tester->getDisplay());
69+
}
70+
6071
/**
6172
* @return CommandTester
6273
*/

0 commit comments

Comments
 (0)
0