@@ -88,9 +88,48 @@ protected function execute(InputInterface $input, OutputInterface $output)
8888 }
8989
9090 foreach ($ files as $ file ) {
91- $ twig ->parse ($ twig ->tokenize (file_get_contents ($ file ), (string ) $ file ));
91+ try {
92+ $ twig ->parse ($ twig ->tokenize (file_get_contents ($ file ), (string ) $ file ));
93+ $ output ->writeln (sprintf ("<info>OK</info> in %s " , $ file ));
94+ } catch (\Twig_Error $ e ) {
95+ $ this ->renderException ($ output , $ file , $ e );
96+ }
97+ }
98+ }
99+
100+ protected function renderException (OutputInterface $ output , $ file , \Twig_Error $ exception )
101+ {
102+ $ line = $ exception ->getTemplateLine ();
103+ $ lines = $ this ->getContext ($ file , $ line );
104+
105+ $ output ->writeln (sprintf ("<error>KO</error> in %s (line %s) " , $ file , $ line ));
106+ foreach ($ lines as $ no => $ code ) {
107+ $ output ->writeln (sprintf (
108+ "%s %-6s %s " ,
109+ $ no == $ line ? '<error>>></error> ' : ' ' ,
110+ $ no ,
111+ $ code
112+ ));
113+ if ($ no == $ line ) {
114+ $ output ->writeln (sprintf ('<error>>> %s</error> ' , $ exception ->getRawMessage ()));
115+ }
116+ }
117+ }
118+
119+ protected function getContext ($ file , $ line , $ context = 3 )
120+ {
121+ $ fileContent = file_get_contents ($ file );
122+ $ lines = explode ("\n" , $ fileContent );
123+
124+ $ position = min (0 , $ line - $ context );
125+ $ max = min (count ($ lines ), $ line - 1 + $ context );
126+
127+ $ result = array ();
128+ while ($ position < $ max ) {
129+ $ result [$ position + 1 ] = $ lines [$ position ];
130+ $ position ++;
92131 }
93132
94- $ output -> writeln ( ' <info>No syntax error detected.</info> ' ) ;
133+ return $ result ;
95134 }
96135}
0 commit comments