Skip to content

Commit adeacad

Browse files
committed
Use Generator to iterate over the filesystem
Move STDIN related code in a method Use RecursiveIteratorIterator::LEAVES_ONLY rather than SELF_FIRST Stop using the Finder component when available (Make findFiles() private) Re-add FrameworkBundle YamlLintCommandTest
1 parent b15d23d commit adeacad

File tree

4 files changed

+55
-60
lines changed

4 files changed

+55
-60
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14-
use Symfony\Component\Finder\Finder;
1514
use Symfony\Component\Yaml\Command\LintCommand as BaseLintCommand;
1615

1716
/**
@@ -34,21 +33,12 @@ protected function configure()
3433
3534
Or find all files in a bundle:
3635
37-
<info>php %command.full_name% @AcmeDemoBundle</info>
36+
<info>php %command.full_name% @AcmeDemoBundle</info>
3837
3938
EOF
4039
);
4140
}
4241

43-
protected function findFilesInDirectory($directory)
44-
{
45-
if (!is_dir($directory)) {
46-
$directory = $this->getApplication()->getKernel()->locateResource($directory);
47-
}
48-
49-
return Finder::create()->files()->in($directory)->name('*.yml');
50-
}
51-
5242
protected function isReadable($fileOrDirectory)
5343
{
5444
return 0 === strpos($fileOrDirectory, '@') || parent::isReadable($fileOrDirectory);

src/Symfony/Bundle/FrameworkBundle/Tests/Command/YamlLintCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testGetHelp()
8484
8585
Or find all files in a bundle:
8686
87-
<info>php %command.full_name% @AcmeDemoBundle</info>
87+
<info>php %command.full_name% @AcmeDemoBundle</info>
8888
8989
EOF;
9090

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"symfony/process": "~2.8|~3.0",
5050
"symfony/serializer": "~2.8|^3.0",
5151
"symfony/validator": "~3.1",
52-
"symfony/yaml": "~2.8|~3.0",
52+
"symfony/yaml": "~3.2",
5353
"symfony/property-info": "~2.8|~3.0",
5454
"phpdocumentor/reflection-docblock": "^3.0",
5555
"twig/twig": "~1.23|~2.0"

src/Symfony/Component/Yaml/Command/LintCommand.php

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ class LintCommand extends Command
2929
{
3030
private $parser;
3131

32+
/**
33+
* {@inheritdoc}
34+
*/
3235
protected 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
3643
The <info>%command.name%</info> command lints a YAML file and outputs to STDOUT
3744
the first encountered syntax error.
@@ -51,10 +58,6 @@ protected function configure()
5158
5259
EOF
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

6669
if (!$filename) {
67-
if (0 !== ftell(STDIN)) {
70+
if (!$stdin = $this->getStdin()) {
6871
throw 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

7977
if (!$this->isReadable($filename)) {
8078
throw 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

9889
private function validate($content, $file = null)
9990
{
100-
$parser = $this->getParser();
101-
10291
try {
103-
$parser->parse($content);
92+
$this->getParser()->parse($content);
10493
} catch (ParseException $e) {
10594
return array('file' => $file, 'valid' => false, 'message' => $e->getMessage());
10695
}
@@ -122,25 +111,26 @@ private function display(InputInterface $input, OutputInterface $output, Symfony
122111

123112
private function displayTxt(OutputInterface $output, SymfonyStyle $io, $filesInfo)
124113
{
125-
$errors = 0;
114+
$countFiles = count($filesInfo);
115+
$erroredFiles = 0;
126116

127117
foreach ($filesInfo as $info) {
128118
if ($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

146136
private function displayJson(OutputInterface $output, $filesInfo)
@@ -159,36 +149,51 @@ private function displayJson(OutputInterface $output, $filesInfo)
159149
return 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();
184163
foreach ($iterator as $file) {
185-
if ('yml' !== $file->getExtension()) {
164+
if (!in_array($file->getExtension(), array('yml', 'yaml'))) {
186165
continue;
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
}

0 commit comments

Comments
 (0)