Permalink
Browse files

[TwigBundle] Fixed return code in lint command

  • Loading branch information...
1 parent adf0c0f commit 56a62e143aac65b29ef60f524ce4baa6e13e5ac4 @hason committed Sep 21, 2012
Showing with 28 additions and 16 deletions.
  1. +28 −16 src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
View
44 src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
@@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$template .= fread(STDIN, 1024);
}
- return $twig->parse($twig->tokenize($template));
+ return $this->validateTemplate($twig, $output, $template);
}
if (0 !== strpos($filename, '@') && !is_readable($filename)) {
@@ -87,26 +87,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
$files = Finder::create()->files()->in($dir)->name('*.twig');
}
- $error = false;
+ $errors = 0;
foreach ($files as $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;
- }
+ $errors += $this->validateTemplate($twig, $output, file_get_contents($file), $file);
+ }
+
+ return $errors > 0 ? 1 : 0;
+ }
+
+ protected function validateTemplate(\Twig_Environment $twig, OutputInterface $output, $template, $file = null)
+ {
+ try {
+ $twig->parse($twig->tokenize($template, $file ? (string) $file : null));
+ $output->writeln('<info>OK</info>'.($file ? sprintf(' in %s', $file) : ''));
+ } catch (\Twig_Error $e) {
+ $this->renderException($output, $template, $e, $file);
+
+ return 1;
}
- return $error ? 1 : 0;
+ return 0;
}
- protected function renderException(OutputInterface $output, $file, \Twig_Error $exception)
+ protected function renderException(OutputInterface $output, $template, \Twig_Error $exception, $file = null)
{
$line = $exception->getTemplateLine();
- $lines = $this->getContext($file, $line);
+ $lines = $this->getContext($template, $line);
+
+ if ($file) {
+ $output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line));
+ } else {
+ $output->writeln(sprintf("<error>KO</error> (line %s)", $line));
+ }
- $output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line));
foreach ($lines as $no => $code) {
$output->writeln(sprintf(
"%s %-6s %s",
@@ -120,10 +133,9 @@ protected function renderException(OutputInterface $output, $file, \Twig_Error $
}
}
- protected function getContext($file, $line, $context = 3)
+ protected function getContext($template, $line, $context = 3)
{
- $fileContent = file_get_contents($file);
- $lines = explode("\n", $fileContent);
+ $lines = explode("\n", $template);
$position = max(0, $line - $context);
$max = min(count($lines), $line - 1 + $context);

0 comments on commit 56a62e1

Please sign in to comment.