Skip to content

Commit 8fe497c

Browse files
committed
[FrameworkBundle] fixed yaml:lint when yaml is not installed along side framwork-bundle
1 parent f1c1e37 commit 8fe497c

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

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

+36-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
1417
use Symfony\Component\Yaml\Command\LintCommand as BaseLintCommand;
1518

1619
/**
@@ -19,17 +22,39 @@
1922
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2023
* @author Robin Chalas <robin.chalas@gmail.com>
2124
*/
22-
class YamlLintCommand extends BaseLintCommand
25+
class YamlLintCommand extends Command
2326
{
27+
private $command;
28+
2429
/**
2530
* {@inheritdoc}
2631
*/
2732
protected function configure()
2833
{
29-
parent::configure();
34+
$this->setName('lint:yaml');
35+
36+
if (!$this->isEnabled() && null !== $this->command) {
37+
return;
38+
}
39+
40+
$directoryIteratorProvider = function ($directory, $default) {
41+
if (!is_dir($directory)) {
42+
$directory = $this->getApplication()->getKernel()->locateResource($directory);
43+
}
44+
45+
return $default($directory);
46+
};
47+
48+
$isReadableProvider = function ($fileOrDirectory, $default) {
49+
return 0 === strpos($fileOrDirectory, '@') || $default($fileOrDirectory);
50+
};
3051

31-
$this->setHelp(
32-
$this->getHelp().<<<EOF
52+
$this->command = new BaseLintCommand(null, $directoryIteratorProvider, $isReadableProvider);
53+
54+
$this
55+
->setDescription($this->command->getDescription())
56+
->setDefinition($this->command->getDefinition())
57+
->setHelp($this->command->getHelp().<<<EOF
3358
3459
Or find all files in a bundle:
3560
@@ -39,17 +64,16 @@ protected function configure()
3964
);
4065
}
4166

42-
protected function getDirectoryIterator($directory)
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
public function isEnabled()
4371
{
44-
if (!is_dir($directory)) {
45-
$directory = $this->getApplication()->getKernel()->locateResource($directory);
46-
}
47-
48-
return parent::getDirectoryIterator($directory);
72+
return class_exists('Symfony\Component\Yaml\Command\LintCommand') && parent::isEnabled();
4973
}
5074

51-
protected function isReadable($fileOrDirectory)
75+
protected function execute(InputInterface $input, OutputInterface $output)
5276
{
53-
return 0 === strpos($fileOrDirectory, '@') || parent::isReadable($fileOrDirectory);
77+
return $this->command->execute($input, $output);
5478
}
5579
}

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

+36-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ class LintCommand extends Command
3030
private $parser;
3131
private $format;
3232
private $displayCorrectFiles;
33+
private $directoryIteratorProvider;
34+
private $isReadableProvider;
35+
36+
public function __construct($name = null, $directoryIteratorProvider = null, $isReadableProvider = null)
37+
{
38+
parent::__construct($name);
39+
40+
$this->directoryIteratorProvider = $directoryIteratorProvider;
41+
$this->isReadableProvider = $isReadableProvider;
42+
}
3343

3444
/**
3545
* {@inheritdoc}
@@ -170,14 +180,6 @@ private function getFiles($fileOrDirectory)
170180
}
171181
}
172182

173-
protected function getDirectoryIterator($directory)
174-
{
175-
return new \RecursiveIteratorIterator(
176-
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
177-
\RecursiveIteratorIterator::LEAVES_ONLY
178-
);
179-
}
180-
181183
private function getStdin()
182184
{
183185
if (0 !== ftell(STDIN)) {
@@ -201,8 +203,32 @@ private function getParser()
201203
return $this->parser;
202204
}
203205

204-
protected function isReadable($fileOrDirectory)
206+
private function getDirectoryIterator($directory)
205207
{
206-
return is_readable($fileOrDirectory);
208+
$default = function ($directory) {
209+
return new \RecursiveIteratorIterator(
210+
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
211+
\RecursiveIteratorIterator::LEAVES_ONLY
212+
);
213+
};
214+
215+
if (null !== $this->directoryIteratorProvider) {
216+
return call_user_func($this->directoryIteratorProvider, $directory, $default);
217+
}
218+
219+
return $default($directory);
220+
}
221+
222+
private function isReadable($fileOrDirectory)
223+
{
224+
$default = function ($fileOrDirectory) {
225+
return is_readable($fileOrDirectory);
226+
};
227+
228+
if (null !== $this->isReadableProvider) {
229+
return call_user_func($this->isReadableProvider, $fileOrDirectory, $default);
230+
}
231+
232+
return $default($fileOrDirectory);
207233
}
208234
}

0 commit comments

Comments
 (0)