1414use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
1515use Symfony \Component \Console \Input \InputInterface ;
1616use Symfony \Component \Console \Output \OutputInterface ;
17+ use Symfony \Component \Finder \Finder ;
1718
1819/**
1920 * Command that will validate your template syntax and output encountered errors.
@@ -36,6 +37,16 @@ protected function configure()
3637
3738The command will get the contents of "filename" and will validates its syntax.
3839
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+
3950<info>cat filename | php %command.full_name%</info>
4051
4152The command will get the template contents from stdin and will validates its syntax.
@@ -54,34 +65,43 @@ protected function execute(InputInterface $input, OutputInterface $output)
5465 $ template = null ;
5566 $ filename = $ input ->getArgument ('filename ' );
5667
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 {
68+ if (!$ filename ) {
6669 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+ throw new \RuntimeException ("Please provide a filename or pipe template content to stdin. " );
7071 }
72+
7173 while (!feof (STDIN )) {
7274 $ template .= fread (STDIN , 1024 );
7375 }
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 " );
7482 }
7583
76- try {
77- $ twig ->parse ($ twig ->tokenize ($ template ));
78- } catch (\Twig_Error_Syntax $ e ) {
79- $ output ->writeln ($ e ->getMessage ());
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+ }
8093
81- return 1 ;
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+ }
82102 }
83103
84- $ output ->writeln (" <info>Template's syntax is valid .</info> " );
104+ $ output ->writeln (' <info>No syntax error detected .</info> ' );
85105 }
86106}
87107
0 commit comments