|
| 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 | + |
| 18 | +/** |
| 19 | + * Command that will validate your template syntax and output encountered errors. |
| 20 | + * |
| 21 | + * @author Marc Weistroff <marc.weistroff@sensiolabs.com> |
| 22 | + */ |
| 23 | +class LintCommand extends ContainerAwareCommand |
| 24 | +{ |
| 25 | + protected function configure() |
| 26 | + { |
| 27 | + $this |
| 28 | + ->setName('twig:lint') |
| 29 | + ->setDescription('Lints a template and outputs eventual errors.') |
| 30 | + ->addArgument('filename') |
| 31 | + ->setHelp(<<<EOF |
| 32 | +the <info>%command.name%</info> command lints a template and outputs to stdout |
| 33 | +the first encountered syntax error. |
| 34 | +
|
| 35 | +<info>php %command.full_name% filename</info> |
| 36 | +
|
| 37 | +The command will get the contents of "filename" and will validates its syntax. |
| 38 | +
|
| 39 | +<info>cat filename | php %command.full_name%</info> |
| 40 | +
|
| 41 | +The command will get the template contents from stdin and will validates its syntax. |
| 42 | +
|
| 43 | +This command will return these error codes: |
| 44 | + - 1 if template is invalid |
| 45 | + - 2 if file doesn't exists or stdin is empty. |
| 46 | +EOF |
| 47 | + ) |
| 48 | + ; |
| 49 | + } |
| 50 | + |
| 51 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 52 | + { |
| 53 | + $twig = $this->getContainer()->get('twig'); |
| 54 | + $template = null; |
| 55 | + $filename = $input->getArgument('filename'); |
| 56 | + |
| 57 | + if ($filename && !is_readable($filename)) { |
| 58 | + $output->writeln(sprintf('<error>File %s is not readable</error>', $filename)); |
| 59 | + |
| 60 | + return 2; |
| 61 | + } |
| 62 | + |
| 63 | + if ($filename) { |
| 64 | + $template = file_get_contents($filename); |
| 65 | + } else { |
| 66 | + if (0 !== ftell(STDIN)) { |
| 67 | + $output->writeln(sprintf('<error>Please provide a filename or pipe template content to stdin.</error>')); |
| 68 | + |
| 69 | + return 2; |
| 70 | + } |
| 71 | + while (!feof(STDIN)) { |
| 72 | + $template .= fread(STDIN, 1024); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + $twig->parse($twig->tokenize($template)); |
| 78 | + } catch(\Twig_Error_Syntax $e) { |
| 79 | + $output->writeln($e->getMessage()); |
| 80 | + |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + $output->writeln("<info>Template's syntax is valid.</info>"); |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments