merged branch alexandresalome/twig-linter-context (PR #4452)
Commits -------df5590e
[TwigBundle] Fix return code in LintComand604a79a
[TwigBundle] Fix line start in twig:lint command91936b5
[TwigBundle] Fancy output for twig:lint Discussion ---------- [TwigBundle] Fancy output for twig:lint Previous PR : #3804 @marcw @fabpot Since no exception is raised, the return code is always 0. Do I add ``return rand(64, 113)`` ? Screenshot : http://twitpic.com/9qql09 --------------------------------------------------------------------------- by travisbot at 2012-05-29T21:18:33Z This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1470256) (merged91936b53
intoadf07f1e
). --------------------------------------------------------------------------- by travisbot at 2012-05-29T21:21:54Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1470353) (merged604a79ab
intoadf07f1e
). --------------------------------------------------------------------------- by fabpot at 2012-05-30T16:45:24Z @alexandresalome just return 1 in case of a problem. --------------------------------------------------------------------------- by travisbot at 2012-06-06T20:06:04Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1550631) (mergeddf5590ec
intoadf07f1e
).
This commit is contained in:
commit
f8e68e58bf
@ -87,10 +87,53 @@ EOF
|
||||
$files = Finder::create()->files()->in($dir)->name('*.twig');
|
||||
}
|
||||
|
||||
$error = false;
|
||||
foreach ($files as $file) {
|
||||
$twig->parse($twig->tokenize(file_get_contents($file), (string) $file));
|
||||
try {
|
||||
$twig->parse($twig->tokenize(file_get_contents($file), (string) $file));
|
||||
$output->writeln(sprintf("<info>OK</info> in %s", $file));
|
||||
} catch (\Twig_Error $e) {
|
||||
$this->renderException($output, $file, $e);
|
||||
$error = true;
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln('<info>No syntax error detected.</info>');
|
||||
return $error ? 1 : 0;
|
||||
}
|
||||
|
||||
protected function renderException(OutputInterface $output, $file, \Twig_Error $exception)
|
||||
{
|
||||
$line = $exception->getTemplateLine();
|
||||
$lines = $this->getContext($file, $line);
|
||||
|
||||
$output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line));
|
||||
foreach ($lines as $no => $code) {
|
||||
$output->writeln(sprintf(
|
||||
"%s %-6s %s",
|
||||
$no == $line ? '<error>>></error>' : ' ',
|
||||
$no,
|
||||
$code
|
||||
));
|
||||
if ($no == $line) {
|
||||
$output->writeln(sprintf('<error>>> %s</error> ', $exception->getRawMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getContext($file, $line, $context = 3)
|
||||
{
|
||||
$fileContent = file_get_contents($file);
|
||||
$lines = explode("\n", $fileContent);
|
||||
|
||||
$position = max(0, $line - $context);
|
||||
$max = min(count($lines), $line - 1 + $context);
|
||||
|
||||
$result = array();
|
||||
while ($position < $max) {
|
||||
$result[$position + 1] = $lines[$position];
|
||||
$position++;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user