2013-12-19 22:11:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bridge\Twig\Command;
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
if (!defined('JSON_PRETTY_PRINT')) {
|
|
|
|
define('JSON_PRETTY_PRINT', 128);
|
|
|
|
}
|
|
|
|
|
2013-12-19 22:11:11 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2014-01-18 13:22:54 +00:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2013-12-19 22:11:11 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Command that will validate your template syntax and output encountered errors.
|
|
|
|
*
|
|
|
|
* @author Marc Weistroff <marc.weistroff@sensiolabs.com>
|
|
|
|
* @author Jérôme Tamarelle <jerome@tamarelle.net>
|
|
|
|
*/
|
|
|
|
class LintCommand extends Command
|
|
|
|
{
|
|
|
|
private $twig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function __construct($name = 'twig:lint')
|
|
|
|
{
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the twig environment
|
|
|
|
*
|
|
|
|
* @param \Twig_Environment $twig
|
|
|
|
*/
|
|
|
|
public function setTwigEnvironment(\Twig_Environment $twig)
|
|
|
|
{
|
|
|
|
$this->twig = $twig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Twig_Environment $twig
|
|
|
|
*/
|
|
|
|
protected function getTwigEnvironment()
|
|
|
|
{
|
|
|
|
return $this->twig;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setDescription('Lints a template and outputs encountered errors')
|
2014-01-18 13:22:54 +00:00
|
|
|
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
|
2013-12-19 22:11:11 +00:00
|
|
|
->addArgument('filename')
|
|
|
|
->setHelp(<<<EOF
|
2014-01-18 12:57:30 +00:00
|
|
|
The <info>%command.name%</info> command lints a template and outputs to STDOUT
|
2013-12-19 22:11:11 +00:00
|
|
|
the first encountered syntax error.
|
|
|
|
|
2014-01-18 12:57:30 +00:00
|
|
|
You can validate the syntax of a file:
|
|
|
|
|
2013-12-19 22:11:11 +00:00
|
|
|
<info>php %command.full_name% filename</info>
|
|
|
|
|
2014-01-18 12:57:30 +00:00
|
|
|
Or of a whole directory:
|
2013-12-19 22:11:11 +00:00
|
|
|
|
|
|
|
<info>php %command.full_name% dirname</info>
|
2014-01-18 13:22:54 +00:00
|
|
|
<info>php %command.full_name% dirname --format=json</info>
|
2013-12-19 22:11:11 +00:00
|
|
|
|
2014-01-18 12:57:30 +00:00
|
|
|
You can also pass the template contents from STDIN:
|
2013-12-19 22:11:11 +00:00
|
|
|
|
|
|
|
<info>cat filename | php %command.full_name%</info>
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$twig = $this->getTwigEnvironment();
|
|
|
|
$filename = $input->getArgument('filename');
|
|
|
|
|
|
|
|
if (!$filename) {
|
|
|
|
if (0 !== ftell(STDIN)) {
|
2014-01-18 12:57:30 +00:00
|
|
|
throw new \RuntimeException("Please provide a filename or pipe template content to STDIN.");
|
2013-12-19 22:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
$template = '';
|
2013-12-19 22:11:11 +00:00
|
|
|
while (!feof(STDIN)) {
|
|
|
|
$template .= fread(STDIN, 1024);
|
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
return $this->display($input, $output, array($this->validate($twig, $template)));
|
2013-12-19 22:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
$filesInfo = array();
|
|
|
|
foreach ($this->findFiles($filename) as $file) {
|
|
|
|
$filesInfo[] = $this->validate($twig, file_get_contents($file), $file);
|
2013-12-19 22:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
return $this->display($input, $output, $filesInfo);
|
2013-12-19 22:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function findFiles($filename)
|
|
|
|
{
|
|
|
|
if (is_file($filename)) {
|
|
|
|
return array($filename);
|
|
|
|
} elseif (is_dir($filename)) {
|
|
|
|
return Finder::create()->files()->in($filename)->name('*.twig');
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
|
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
private function validate(\Twig_Environment $twig, $template, $file = null)
|
2013-12-19 22:11:11 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$twig->parse($twig->tokenize($template, $file ? (string) $file : null));
|
|
|
|
} catch (\Twig_Error $e) {
|
2014-01-18 13:22:54 +00:00
|
|
|
return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array('template' => $template, 'file' => $file, 'valid' => true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function display(InputInterface $input, OutputInterface $output, $files)
|
|
|
|
{
|
|
|
|
switch ($input->getOption('format')) {
|
|
|
|
case 'txt':
|
|
|
|
return $this->displayTxt($output, $files);
|
|
|
|
case 'json':
|
|
|
|
return $this->displayJson($output, $files);
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function displayTxt(OutputInterface $output, $filesInfo)
|
|
|
|
{
|
|
|
|
$errors = 0;
|
2013-12-19 22:11:11 +00:00
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
foreach ($filesInfo as $info) {
|
|
|
|
if ($info['valid'] && $output->isVerbose()) {
|
|
|
|
$output->writeln('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
|
|
|
|
} elseif (!$info['valid']) {
|
|
|
|
$errors++;
|
|
|
|
$this->renderException($output, $info['template'], $info['exception'], $info['file']);
|
|
|
|
}
|
2013-12-19 22:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
$output->writeln(sprintf('<comment>%d/%d valid files</comment>', count($filesInfo) - $errors, count($filesInfo)));
|
|
|
|
|
|
|
|
return min($errors, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function displayJson(OutputInterface $output, $filesInfo)
|
|
|
|
{
|
|
|
|
$errors = 0;
|
|
|
|
|
|
|
|
array_walk($filesInfo, function (&$v) use (&$errors) {
|
|
|
|
$v['file'] = (string) $v['file'];
|
|
|
|
unset($v['template']);
|
|
|
|
if (!$v['valid']) {
|
|
|
|
$v['message'] = $v['exception']->getMessage();
|
|
|
|
unset($v['exception']);
|
|
|
|
$errors++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$output->writeln(json_encode($filesInfo, JSON_PRETTY_PRINT));
|
|
|
|
|
|
|
|
return min($errors, 1);
|
2013-12-19 22:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
private function renderException(OutputInterface $output, $template, \Twig_Error $exception, $file = null)
|
2013-12-19 22:11:11 +00:00
|
|
|
{
|
|
|
|
$line = $exception->getTemplateLine();
|
|
|
|
|
|
|
|
if ($file) {
|
|
|
|
$output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line));
|
|
|
|
} else {
|
|
|
|
$output->writeln(sprintf("<error>KO</error> (line %s)", $line));
|
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
foreach ($this->getContext($template, $line) as $no => $code) {
|
2013-12-19 22:11:11 +00:00
|
|
|
$output->writeln(sprintf(
|
|
|
|
"%s %-6s %s",
|
|
|
|
$no == $line ? '<error>>></error>' : ' ',
|
|
|
|
$no,
|
|
|
|
$code
|
|
|
|
));
|
|
|
|
if ($no == $line) {
|
|
|
|
$output->writeln(sprintf('<error>>> %s</error> ', $exception->getRawMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-18 13:22:54 +00:00
|
|
|
private function getContext($template, $line, $context = 3)
|
2013-12-19 22:11:11 +00:00
|
|
|
{
|
|
|
|
$lines = explode("\n", $template);
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|