@@ -29,9 +29,16 @@ class LintCommand extends Command
2929{
3030private $ parser ;
3131
32+ /**
33+ * {@inheritdoc}
34+ */
3235protected function configure ()
3336 {
3437$ this
38+ ->setName ('lint:yaml ' )
39+ ->setDescription ('Lints a file and outputs encountered errors ' )
40+ ->addArgument ('filename ' ,null ,'A file or a directory or STDIN ' )
41+ ->addOption ('format ' ,null , InputOption::VALUE_REQUIRED ,'The output format ' ,'txt ' )
3542 ->setHelp (<<<EOF
3643The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
3744the first encountered syntax error.
@@ -51,10 +58,6 @@ protected function configure()
5158
5259EOF
5360 )
54- ->setName ('lint:yaml ' )
55- ->setDescription ('Lints a file and outputs encountered errors ' )
56- ->addArgument ('filename ' ,null ,'A file or a directory or STDIN ' )
57- ->addOption ('format ' ,null , InputOption::VALUE_REQUIRED ,'The output format ' ,'txt ' )
5861 ;
5962 }
6063
@@ -64,31 +67,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
6467$ filename =$ input ->getArgument ('filename ' );
6568
6669if (!$ filename ) {
67- if (0 !== ftell ( STDIN )) {
70+ if (! $ stdin = $ this -> getStdin ( )) {
6871throw new \RuntimeException ('Please provide a filename or pipe file content to STDIN. ' );
6972 }
7073
71- $ content ='' ;
72- while (!feof (STDIN )) {
73- $ content .=fread (STDIN ,1024 );
74- }
75-
76- return $ this ->display ($ input ,$ output ,$ io ,array ($ this ->validate ($ content )));
74+ return $ this ->display ($ input ,$ output ,$ io ,array ($ this ->validate ($ stdin )));
7775 }
7876
7977if (!$ this ->isReadable ($ filename )) {
8078throw new \RuntimeException (sprintf ('File or directory "%s" is not readable ' ,$ filename ));
8179 }
8280
83- $ files =array ();
84- if (is_file ($ filename )) {
85- $ files =array ($ filename );
86- }else {
87- $ files =$ this ->findFilesInDirectory ($ filename );
88- }
89-
9081$ filesInfo =array ();
91- foreach ($ files as $ file ) {
82+ foreach ($ this -> getFiles ( $ filename ) as $ file ) {
9283$ filesInfo [] =$ this ->validate (file_get_contents ($ file ),$ file );
9384 }
9485
@@ -97,10 +88,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
9788
9889private function validate ($ content ,$ file =null )
9990 {
100- $ parser =$ this ->getParser ();
101-
10291try {
103- $ parser ->parse ($ content );
92+ $ this -> getParser () ->parse ($ content );
10493 }catch (ParseException $ e ) {
10594return array ('file ' =>$ file ,'valid ' =>false ,'message ' =>$ e ->getMessage ());
10695 }
@@ -122,25 +111,26 @@ private function display(InputInterface $input, OutputInterface $output, Symfony
122111
123112private function displayTxt (OutputInterface $ output ,SymfonyStyle $ io ,$ filesInfo )
124113 {
125- $ errors =0 ;
114+ $ countFiles =count ($ filesInfo );
115+ $ erroredFiles =0 ;
126116
127117foreach ($ filesInfoas $ info ) {
128118if ($ info ['valid ' ] &&$ output ->isVerbose ()) {
129119$ io ->comment ('<info>OK</info> ' .($ info ['file ' ] ?sprintf (' in %s ' ,$ info ['file ' ]) :'' ));
130120 }elseif (!$ info ['valid ' ]) {
131- ++$ errors ;
121+ ++$ erroredFiles ;
132122$ io ->text (sprintf ('<error> ERROR </error> in %s ' ,$ info ['file ' ]));
133123$ io ->text (sprintf ('<error> >> %s</error> ' ,$ info ['message ' ]));
134124 }
135125 }
136126
137- if ($ errors ===0 ) {
138- $ io ->success (sprintf ('All %d YAML files contain valid syntax. ' ,count ( $ filesInfo ) ));
127+ if ($ erroredFiles ===0 ) {
128+ $ io ->success (sprintf ('All %d YAML files contain valid syntax. ' ,$ countFiles ));
139129 }else {
140- $ io ->warning (sprintf ('%d YAML files have valid syntax and %d contain errors. ' ,count ( $ filesInfo ) -$ errors ,$ errors ));
130+ $ io ->warning (sprintf ('%d YAML files have valid syntax and %d contain errors. ' ,$ countFiles -$ erroredFiles ,$ erroredFiles ));
141131 }
142132
143- return min ($ errors ,1 );
133+ return min ($ erroredFiles ,1 );
144134 }
145135
146136private function displayJson (OutputInterface $ output ,$ filesInfo )
@@ -159,36 +149,51 @@ private function displayJson(OutputInterface $output, $filesInfo)
159149return min ($ errors ,1 );
160150 }
161151
162- protected function isReadable ($ fileOrDirectory )
163- {
164- return is_readable ($ fileOrDirectory );
165- }
166-
167- protected function getParser ()
152+ private function getFiles ($ fileOrDirectory )
168153 {
169- if (! $ this -> parser ) {
170- $ this -> parser = new Parser ( );
154+ if (is_file ( $ fileOrDirectory ) ) {
155+ return yield new \ SplFileInfo ( $ fileOrDirectory );
171156 }
172157
173- return $ this ->parser ;
174- }
175-
176- protected function findFilesInDirectory ($ directory )
177- {
178158$ iterator =new \RecursiveIteratorIterator (
179- new \RecursiveDirectoryIterator ($ directory , \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS ),
180- \RecursiveIteratorIterator::SELF_FIRST
159+ new \RecursiveDirectoryIterator ($ fileOrDirectory , \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS ),
160+ \RecursiveIteratorIterator::LEAVES_ONLY
181161 );
182162
183- $ files =array ();
184163foreach ($ iteratoras $ file ) {
185- if (' yml ' !== $ file ->getExtension ()) {
164+ if (! in_array ( $ file ->getExtension (), array ( ' yml ' , ' yaml ' ) )) {
186165continue ;
187166 }
188167
189- $ files [] =$ file ;
168+ yield $ file ;
169+ }
170+ }
171+
172+ private function getStdin ()
173+ {
174+ if (0 !==ftell (STDIN )) {
175+ return ;
176+ }
177+
178+ $ inputs ='' ;
179+ while (!feof (STDIN )) {
180+ $ inputs .=fread (STDIN ,1024 );
190181 }
191182
192- return $ files ;
183+ return $ inputs ;
184+ }
185+
186+ private function getParser ()
187+ {
188+ if (!$ this ->parser ) {
189+ $ this ->parser =new Parser ();
190+ }
191+
192+ return $ this ->parser ;
193+ }
194+
195+ protected function isReadable ($ fileOrDirectory )
196+ {
197+ return is_readable ($ fileOrDirectory );
193198 }
194199}