Skip to content

Commit 9c0fba9

Browse files
committed
merged branch marcw/feat-twig-lint (PR #3804)
Commits ------- 8726ade Adds more features to twig:lint command 1d7e9d9 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.
2 parents 5122d68 + 8726ade commit 9c0fba9

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

CHANGELOG-2.1.md

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
148148
### TwigBundle
149149

150150
* added the real template name when an error occurs in a Twig template
151+
* added the twig:lint command that will validate a Twig template syntax.
151152

152153
### WebProfilerBundle
153154

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)