merged branch alexandresalome/twig-linter-context (PR #4452)

Commits
-------

df5590e [TwigBundle] Fix return code in LintComand
604a79a [TwigBundle] Fix line start in twig:lint command
91936b5 [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) (merged 91936b53 into adf07f1e).

---------------------------------------------------------------------------

by travisbot at 2012-05-29T21:21:54Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1470353) (merged 604a79ab into adf07f1e).

---------------------------------------------------------------------------

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) (merged df5590ec into adf07f1e).
This commit is contained in:
Fabien Potencier 2012-06-08 09:58:39 +02:00
commit f8e68e58bf

View File

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