@@ -29,9 +29,16 @@ class LintCommand extends Command
29
29
{
30
30
private $ parser ;
31
31
32
+ /**
33
+ * {@inheritdoc}
34
+ */
32
35
protected function configure ()
33
36
{
34
37
$ 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 ' )
35
42
->setHelp (<<<EOF
36
43
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
37
44
the first encountered syntax error.
@@ -51,10 +58,6 @@ protected function configure()
51
58
52
59
EOF
53
60
)
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 ' )
58
61
;
59
62
}
60
63
@@ -64,31 +67,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
64
67
$ filename = $ input ->getArgument ('filename ' );
65
68
66
69
if (!$ filename ) {
67
- if (0 !== ftell ( STDIN )) {
70
+ if (! $ stdin = $ this -> getStdin ( )) {
68
71
throw new \RuntimeException ('Please provide a filename or pipe file content to STDIN. ' );
69
72
}
70
73
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 )));
77
75
}
78
76
79
77
if (!$ this ->isReadable ($ filename )) {
80
78
throw new \RuntimeException (sprintf ('File or directory "%s" is not readable ' , $ filename ));
81
79
}
82
80
83
- $ files = array ();
84
- if (is_file ($ filename )) {
85
- $ files = array ($ filename );
86
- } else {
87
- $ files = $ this ->findFilesInDirectory ($ filename );
88
- }
89
-
90
81
$ filesInfo = array ();
91
- foreach ($ files as $ file ) {
82
+ foreach ($ this -> getFiles ( $ filename ) as $ file ) {
92
83
$ filesInfo [] = $ this ->validate (file_get_contents ($ file ), $ file );
93
84
}
94
85
@@ -97,10 +88,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
97
88
98
89
private function validate ($ content , $ file = null )
99
90
{
100
- $ parser = $ this ->getParser ();
101
-
102
91
try {
103
- $ parser ->parse ($ content );
92
+ $ this -> getParser () ->parse ($ content );
104
93
} catch (ParseException $ e ) {
105
94
return array ('file ' => $ file , 'valid ' => false , 'message ' => $ e ->getMessage ());
106
95
}
@@ -122,25 +111,26 @@ private function display(InputInterface $input, OutputInterface $output, Symfony
122
111
123
112
private function displayTxt (OutputInterface $ output , SymfonyStyle $ io , $ filesInfo )
124
113
{
125
- $ errors = 0 ;
114
+ $ countFiles = count ($ filesInfo );
115
+ $ erroredFiles = 0 ;
126
116
127
117
foreach ($ filesInfo as $ info ) {
128
118
if ($ info ['valid ' ] && $ output ->isVerbose ()) {
129
119
$ io ->comment ('<info>OK</info> ' .($ info ['file ' ] ? sprintf (' in %s ' , $ info ['file ' ]) : '' ));
130
120
} elseif (!$ info ['valid ' ]) {
131
- ++$ errors ;
121
+ ++$ erroredFiles ;
132
122
$ io ->text (sprintf ('<error> ERROR </error> in %s ' , $ info ['file ' ]));
133
123
$ io ->text (sprintf ('<error> >> %s</error> ' , $ info ['message ' ]));
134
124
}
135
125
}
136
126
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 ));
139
129
} 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 ));
141
131
}
142
132
143
- return min ($ errors , 1 );
133
+ return min ($ erroredFiles , 1 );
144
134
}
145
135
146
136
private function displayJson (OutputInterface $ output , $ filesInfo )
@@ -159,36 +149,51 @@ private function displayJson(OutputInterface $output, $filesInfo)
159
149
return min ($ errors , 1 );
160
150
}
161
151
162
- protected function isReadable ($ fileOrDirectory )
163
- {
164
- return is_readable ($ fileOrDirectory );
165
- }
166
-
167
- protected function getParser ()
152
+ private function getFiles ($ fileOrDirectory )
168
153
{
169
- if (! $ this -> parser ) {
170
- $ this -> parser = new Parser ( );
154
+ if (is_file ( $ fileOrDirectory ) ) {
155
+ return yield new \ SplFileInfo ( $ fileOrDirectory );
171
156
}
172
157
173
- return $ this ->parser ;
174
- }
175
-
176
- protected function findFilesInDirectory ($ directory )
177
- {
178
158
$ 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
181
161
);
182
162
183
- $ files = array ();
184
163
foreach ($ iterator as $ file ) {
185
- if (' yml ' !== $ file ->getExtension ()) {
164
+ if (! in_array ( $ file ->getExtension (), array ( ' yml ' , ' yaml ' ) )) {
186
165
continue ;
187
166
}
188
167
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 );
190
181
}
191
182
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 );
193
198
}
194
199
}
0 commit comments