merged branch marcw/feat-twig-lint (PR #3804)
Commits -------8726ade
Adds more features to twig:lint command1d7e9d9
Adds a linter command for templates Discussion ---------- Adds a linter command for templates Let's this PR be stoffed. ;) --------------------------------------------------------------------------- by Tobion at 2012-04-06T15:48:18Z Checking a single file is very limited. Checking a whole directory would be more useful, wouldn't it? --------------------------------------------------------------------------- by willdurand at 2012-04-06T17:56:57Z I think you should provide a way to validate all templates for a given bundle, something like: php app/console twig:lint @MySuperBundle --------------------------------------------------------------------------- by henrikbjorn at 2012-04-06T18:03:45Z Wouldnt it be better to throw some kind of exception if the lint is erroneous? --------------------------------------------------------------------------- by marcw at 2012-04-07T11:22:34Z @Tobion @willdurand You can do that by combining unix tools. @henrikbjorn Why ? --------------------------------------------------------------------------- by marcw at 2012-04-07T11:27:11Z Updated. --------------------------------------------------------------------------- by dlsniper at 2012-04-07T13:15:53Z @marcw it would be indeed nice to have support for a bundle/directory out of the box as some of the Symfony2 users might not be running unix or know the right commands to make this work. I could help you with a PR in your repo if you want. --------------------------------------------------------------------------- by henrikbjorn at 2012-04-07T18:55:34Z @marcw as the console component will catch them and convert them into the right error code, also will display what went wrong instead of just dieing. --------------------------------------------------------------------------- by marcw at 2012-04-08T09:15:37Z Updated with all comments and requested features.
This commit is contained in:
commit
9c0fba93b2
@ -148,6 +148,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
|
|||||||
### TwigBundle
|
### TwigBundle
|
||||||
|
|
||||||
* added the real template name when an error occurs in a Twig template
|
* added the real template name when an error occurs in a Twig template
|
||||||
|
* added the twig:lint command that will validate a Twig template syntax.
|
||||||
|
|
||||||
### WebProfilerBundle
|
### WebProfilerBundle
|
||||||
|
|
||||||
|
107
src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
Normal file
107
src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
<?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\Bundle\TwigBundle\Command;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
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>
|
||||||
|
*/
|
||||||
|
class LintCommand extends ContainerAwareCommand
|
||||||
|
{
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->setName('twig:lint')
|
||||||
|
->setDescription('Lints a template and outputs eventual errors.')
|
||||||
|
->addArgument('filename')
|
||||||
|
->setHelp(<<<EOF
|
||||||
|
the <info>%command.name%</info> command lints a template and outputs to stdout
|
||||||
|
the first encountered syntax error.
|
||||||
|
|
||||||
|
<info>php %command.full_name% filename</info>
|
||||||
|
|
||||||
|
The command will get the contents of "filename" and will validates its syntax.
|
||||||
|
|
||||||
|
<info>php %command.full_name% dirname</info>
|
||||||
|
|
||||||
|
The command will find all twig templates in dirname and will validate the syntax
|
||||||
|
of each Twig template.
|
||||||
|
|
||||||
|
<info>php %command.full_name% @AcmeMyBundle</info>
|
||||||
|
|
||||||
|
The command will find all twig templates in bundle AcmeMyBundle and will validate
|
||||||
|
the syntax of each one.
|
||||||
|
|
||||||
|
<info>cat filename | php %command.full_name%</info>
|
||||||
|
|
||||||
|
The command will get the template contents from stdin and will validates its syntax.
|
||||||
|
|
||||||
|
This command will return these error codes:
|
||||||
|
- 1 if template is invalid
|
||||||
|
- 2 if file doesn't exists or stdin is empty.
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$twig = $this->getContainer()->get('twig');
|
||||||
|
$template = null;
|
||||||
|
$filename = $input->getArgument('filename');
|
||||||
|
|
||||||
|
if (!$filename) {
|
||||||
|
if (0 !== ftell(STDIN)) {
|
||||||
|
throw new \RuntimeException("Please provide a filename or pipe template content to stdin.");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!feof(STDIN)) {
|
||||||
|
$template .= fread(STDIN, 1024);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $twig->parse($twig->tokenize($template));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 !== strpos($filename, '@') && !is_readable($filename)) {
|
||||||
|
throw new \RuntimeException("File or directory '%s' is not readable");
|
||||||
|
}
|
||||||
|
|
||||||
|
$files = array();
|
||||||
|
if (is_file($filename)) {
|
||||||
|
$files = array($filename);
|
||||||
|
} elseif (is_dir($filename)) {
|
||||||
|
$files = Finder::create()->files()->in($filename)->name('*.twig');
|
||||||
|
} else {
|
||||||
|
$dir = $this->getApplication()->getKernel()->locateResource($filename);
|
||||||
|
$files = Finder::create()->files()->in($dir)->name('*.twig');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
try {
|
||||||
|
$twig->parse($twig->tokenize(file_get_contents($file)));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$output->writeln(sprintf('<error>Syntax error in %s</error>', $file));
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$output->writeln('<info>No syntax error detected.</info>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user