14
14
use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
15
15
use Symfony \Component \Console \Input \InputInterface ;
16
16
use Symfony \Component \Console \Output \OutputInterface ;
17
+ use Symfony \Component \Finder \Finder ;
17
18
18
19
/**
19
20
* Command that will validate your template syntax and output encountered errors.
@@ -36,6 +37,16 @@ protected function configure()
36
37
37
38
The command will get the contents of "filename" and will validates its syntax.
38
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
+
39
50
<info>cat filename | php %command.full_name%</info>
40
51
41
52
The command will get the template contents from stdin and will validates its syntax.
@@ -54,34 +65,43 @@ protected function execute(InputInterface $input, OutputInterface $output)
54
65
$ template = null ;
55
66
$ filename = $ input ->getArgument ('filename ' );
56
67
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 ) {
66
69
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. " );
70
71
}
72
+
71
73
while (!feof (STDIN )) {
72
74
$ template .= fread (STDIN , 1024 );
73
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 " );
74
82
}
75
83
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
+ }
80
93
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
+ }
82
102
}
83
103
84
- $ output ->writeln (" <info>Template's syntax is valid .</info> " );
104
+ $ output ->writeln (' <info>No syntax error detected .</info> ' );
85
105
}
86
106
}
87
107
0 commit comments