Skip to content

Commit f8e68e5

Browse files
committed
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 91936b5 into adf07f1). --------------------------------------------------------------------------- by travisbot at 2012-05-29T21:21:54Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1470353) (merged 604a79a into adf07f1). --------------------------------------------------------------------------- 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 df5590e into adf07f1).
2 parents 3c8947e + df5590e commit f8e68e5

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/Symfony/Bundle/TwigBundle/Command/LintCommand.php

+45-2
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,53 @@ protected function execute(InputInterface $input, OutputInterface $output)
8787
$files = Finder::create()->files()->in($dir)->name('*.twig');
8888
}
8989

90+
$error = false;
9091
foreach ($files as $file) {
91-
$twig->parse($twig->tokenize(file_get_contents($file), (string) $file));
92+
try {
93+
$twig->parse($twig->tokenize(file_get_contents($file), (string) $file));
94+
$output->writeln(sprintf("<info>OK</info> in %s", $file));
95+
} catch (\Twig_Error $e) {
96+
$this->renderException($output, $file, $e);
97+
$error = true;
98+
}
99+
}
100+
101+
return $error ? 1 : 0;
102+
}
103+
104+
protected function renderException(OutputInterface $output, $file, \Twig_Error $exception)
105+
{
106+
$line = $exception->getTemplateLine();
107+
$lines = $this->getContext($file, $line);
108+
109+
$output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line));
110+
foreach ($lines as $no => $code) {
111+
$output->writeln(sprintf(
112+
"%s %-6s %s",
113+
$no == $line ? '<error>>></error>' : ' ',
114+
$no,
115+
$code
116+
));
117+
if ($no == $line) {
118+
$output->writeln(sprintf('<error>>> %s</error> ', $exception->getRawMessage()));
119+
}
120+
}
121+
}
122+
123+
protected function getContext($file, $line, $context = 3)
124+
{
125+
$fileContent = file_get_contents($file);
126+
$lines = explode("\n", $fileContent);
127+
128+
$position = max(0, $line - $context);
129+
$max = min(count($lines), $line - 1 + $context);
130+
131+
$result = array();
132+
while ($position < $max) {
133+
$result[$position + 1] = $lines[$position];
134+
$position++;
92135
}
93136

94-
$output->writeln('<info>No syntax error detected.</info>');
137+
return $result;
95138
}
96139
}

0 commit comments

Comments
 (0)