|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Bundle\TwigBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 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 <marc.weistroff@sensiolabs.com> |
| 23 | + */ |
| 24 | +class LintCommand extends ContainerAwareCommand |
| 25 | +{ |
| 26 | + protected function configure() |
| 27 | + { |
| 28 | + $this |
| 29 | + ->setName('twig:lint') |
| 30 | + ->setDescription('Lints a template and outputs eventual errors.') |
| 31 | + ->addArgument('filename') |
| 32 | + ->setHelp(<<<EOF |
| 33 | +the <info>%command.name%</info> command lints a template and outputs to stdout |
| 34 | +the first encountered syntax error. |
| 35 | +
|
| 36 | +<info>php %command.full_name% filename</info> |
| 37 | +
|
| 38 | +The command will get the contents of "filename" and will validates its syntax. |
| 39 | +
|
| 40 | +<info>php %command.full_name% dirname</info> |
| 41 | +
|
| 42 | +The command will find all twig templates in dirname and will validate the syntax |
| 43 | +of each Twig template. |
| 44 | +
|
| 45 | +<info>php %command.full_name% @AcmeMyBundle</info> |
| 46 | +
|
| 47 | +The command will find all twig templates in bundle AcmeMyBundle and will validate |
| 48 | +the syntax of each one. |
| 49 | +
|
| 50 | +<info>cat filename | php %command.full_name%</info> |
| 51 | +
|
| 52 | +The command will get the template contents from stdin and will validates its syntax. |
| 53 | +
|
| 54 | +This command will return these error codes: |
| 55 | + - 1 if template is invalid |
| 56 | + - 2 if file doesn't exists or stdin is empty. |
| 57 | +EOF |
| 58 | + ) |
| 59 | + ; |
| 60 | + } |
| 61 | + |
| 62 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 63 | + { |
| 64 | + $twig = $this->getContainer()->get('twig'); |
| 65 | + $template = null; |
| 66 | + $filename = $input->getArgument('filename'); |
| 67 | + |
| 68 | + if (!$filename) { |
| 69 | + if (0 !== ftell(STDIN)) { |
| 70 | + throw new \RuntimeException("Please provide a filename or pipe template content to stdin."); |
| 71 | + } |
| 72 | + |
| 73 | + while (!feof(STDIN)) { |
| 74 | + $template .= fread(STDIN, 1024); |
| 75 | + } |
| 76 | + |
| 77 | + return $twig->parse($twig->tokenize($template)); |
| 78 | + } |
| 79 | + |
| 80 | + if (0 !== strpos($filename, '@') && !is_readable($filename)) { |
| 81 | + throw new \RuntimeException("File or directory '%s' is not readable"); |
| 82 | + } |
| 83 | + |
| 84 | + $files = array(); |
| 85 | + if (is_file($filename)) { |
| 86 | + $files = array($filename); |
| 87 | + } elseif (is_dir($filename)) { |
| 88 | + $files = Finder::create()->files()->in($filename)->name('*.twig'); |
| 89 | + } else { |
| 90 | + $dir = $this->getApplication()->getKernel()->locateResource($filename); |
| 91 | + $files = Finder::create()->files()->in($dir)->name('*.twig'); |
| 92 | + } |
| 93 | + |
| 94 | + foreach ($files as $file) { |
| 95 | + try { |
| 96 | + $twig->parse($twig->tokenize(file_get_contents($file))); |
| 97 | + } catch (\Exception $e) { |
| 98 | + $output->writeln(sprintf('<error>Syntax error in %s</error>', $file)); |
| 99 | + |
| 100 | + throw $e; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + $output->writeln('<info>No syntax error detected.</info>'); |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments