|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Twig\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Finder\Finder; |
| 18 | + |
| 19 | +/** |
| 20 | + * Command that will validate your template syntax and output encountered errors. |
| 21 | + * |
| 22 | + * @author Marc Weistroff <[email protected]> |
| 23 | + * @author Jérôme Tamarelle <[email protected]> |
| 24 | + */ |
| 25 | +class LintCommand extends Command |
| 26 | +{ |
| 27 | + private $twig; |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritDoc} |
| 31 | + */ |
| 32 | + public function __construct($name = 'twig:lint') |
| 33 | + { |
| 34 | + parent::__construct($name); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Sets the twig environment |
| 39 | + * |
| 40 | + * @param \Twig_Environment $twig |
| 41 | + */ |
| 42 | + public function setTwigEnvironment(\Twig_Environment $twig) |
| 43 | + { |
| 44 | + $this->twig = $twig; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return \Twig_Environment $twig |
| 49 | + */ |
| 50 | + protected function getTwigEnvironment() |
| 51 | + { |
| 52 | + return $this->twig; |
| 53 | + } |
| 54 | + |
| 55 | + protected function configure() |
| 56 | + { |
| 57 | + $this |
| 58 | + ->setDescription('Lints a template and outputs encountered errors') |
| 59 | + ->addArgument('filename') |
| 60 | + ->setHelp(<<<EOF |
| 61 | +The <info>%command.name%</info> command lints a template and outputs to stdout |
| 62 | +the first encountered syntax error. |
| 63 | +
|
| 64 | +<info>php %command.full_name% filename</info> |
| 65 | +
|
| 66 | +The command gets the contents of <comment>filename</comment> and validates its syntax. |
| 67 | +
|
| 68 | +<info>php %command.full_name% dirname</info> |
| 69 | +
|
| 70 | +The command finds all twig templates in <comment>dirname</comment> and validates the syntax |
| 71 | +of each Twig template. |
| 72 | +
|
| 73 | +<info>cat filename | php %command.full_name%</info> |
| 74 | +
|
| 75 | +The command gets the template contents from stdin and validates its syntax. |
| 76 | +EOF |
| 77 | + ) |
| 78 | + ; |
| 79 | + } |
| 80 | + |
| 81 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 82 | + { |
| 83 | + $twig = $this->getTwigEnvironment(); |
| 84 | + $template = null; |
| 85 | + $filename = $input->getArgument('filename'); |
| 86 | + |
| 87 | + if (!$filename) { |
| 88 | + if (0 !== ftell(STDIN)) { |
| 89 | + throw new \RuntimeException("Please provide a filename or pipe template content to stdin."); |
| 90 | + } |
| 91 | + |
| 92 | + while (!feof(STDIN)) { |
| 93 | + $template .= fread(STDIN, 1024); |
| 94 | + } |
| 95 | + |
| 96 | + return $this->validateTemplate($twig, $output, $template); |
| 97 | + } |
| 98 | + |
| 99 | + $files = $this->findFiles($filename); |
| 100 | + |
| 101 | + $errors = 0; |
| 102 | + foreach ($files as $file) { |
| 103 | + $errors += $this->validateTemplate($twig, $output, file_get_contents($file), $file); |
| 104 | + } |
| 105 | + |
| 106 | + return $errors > 0 ? 1 : 0; |
| 107 | + } |
| 108 | + |
| 109 | + protected function findFiles($filename) |
| 110 | + { |
| 111 | + if (is_file($filename)) { |
| 112 | + return array($filename); |
| 113 | + } elseif (is_dir($filename)) { |
| 114 | + return Finder::create()->files()->in($filename)->name('*.twig'); |
| 115 | + } |
| 116 | + |
| 117 | + throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename)); |
| 118 | + } |
| 119 | + |
| 120 | + protected function validateTemplate(\Twig_Environment $twig, OutputInterface $output, $template, $file = null) |
| 121 | + { |
| 122 | + try { |
| 123 | + $twig->parse($twig->tokenize($template, $file ? (string) $file : null)); |
| 124 | + $output->writeln('<info>OK</info>'.($file ? sprintf(' in %s', $file) : '')); |
| 125 | + } catch (\Twig_Error $e) { |
| 126 | + $this->renderException($output, $template, $e, $file); |
| 127 | + |
| 128 | + return 1; |
| 129 | + } |
| 130 | + |
| 131 | + return 0; |
| 132 | + } |
| 133 | + |
| 134 | + protected function renderException(OutputInterface $output, $template, \Twig_Error $exception, $file = null) |
| 135 | + { |
| 136 | + $line = $exception->getTemplateLine(); |
| 137 | + $lines = $this->getContext($template, $line); |
| 138 | + |
| 139 | + if ($file) { |
| 140 | + $output->writeln(sprintf("<error>KO</error> in %s (line %s)", $file, $line)); |
| 141 | + } else { |
| 142 | + $output->writeln(sprintf("<error>KO</error> (line %s)", $line)); |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($lines as $no => $code) { |
| 146 | + $output->writeln(sprintf( |
| 147 | + "%s %-6s %s", |
| 148 | + $no == $line ? '<error>>></error>' : ' ', |
| 149 | + $no, |
| 150 | + $code |
| 151 | + )); |
| 152 | + if ($no == $line) { |
| 153 | + $output->writeln(sprintf('<error>>> %s</error> ', $exception->getRawMessage())); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + protected function getContext($template, $line, $context = 3) |
| 159 | + { |
| 160 | + $lines = explode("\n", $template); |
| 161 | + |
| 162 | + $position = max(0, $line - $context); |
| 163 | + $max = min(count($lines), $line - 1 + $context); |
| 164 | + |
| 165 | + $result = array(); |
| 166 | + while ($position < $max) { |
| 167 | + $result[$position + 1] = $lines[$position]; |
| 168 | + $position++; |
| 169 | + } |
| 170 | + |
| 171 | + return $result; |
| 172 | + } |
| 173 | +} |
0 commit comments