diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php index 1d065f7001..083c12c036 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php @@ -11,6 +11,9 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Yaml\Command\LintCommand as BaseLintCommand; /** @@ -19,17 +22,39 @@ use Symfony\Component\Yaml\Command\LintCommand as BaseLintCommand; * @author Grégoire Pineau * @author Robin Chalas */ -class YamlLintCommand extends BaseLintCommand +class YamlLintCommand extends Command { + private $command; + /** * {@inheritdoc} */ protected function configure() { - parent::configure(); + $this->setName('lint:yaml'); - $this->setHelp( - $this->getHelp().<<isEnabled()) { + return; + } + + $directoryIteratorProvider = function ($directory, $default) { + if (!is_dir($directory)) { + $directory = $this->getApplication()->getKernel()->locateResource($directory); + } + + return $default($directory); + }; + + $isReadableProvider = function ($fileOrDirectory, $default) { + return 0 === strpos($fileOrDirectory, '@') || $default($fileOrDirectory); + }; + + $this->command = new BaseLintCommand(null, $directoryIteratorProvider, $isReadableProvider); + + $this + ->setDescription($this->command->getDescription()) + ->setDefinition($this->command->getDefinition()) + ->setHelp($this->command->getHelp().<<getApplication()->getKernel()->locateResource($directory); - } - - return parent::getDirectoryIterator($directory); + return class_exists(BaseLintCommand::class) && parent::isEnabled(); } - protected function isReadable($fileOrDirectory) + protected function execute(InputInterface $input, OutputInterface $output) { - return 0 === strpos($fileOrDirectory, '@') || parent::isReadable($fileOrDirectory); + return $this->command->execute($input, $output); } } diff --git a/src/Symfony/Component/Yaml/Command/LintCommand.php b/src/Symfony/Component/Yaml/Command/LintCommand.php index c88778f130..ea86a1f8c1 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -30,6 +30,16 @@ class LintCommand extends Command private $parser; private $format; private $displayCorrectFiles; + private $directoryIteratorProvider; + private $isReadableProvider; + + public function __construct($name = null, $directoryIteratorProvider = null, $isReadableProvider = null) + { + parent::__construct($name); + + $this->directoryIteratorProvider = $directoryIteratorProvider; + $this->isReadableProvider = $isReadableProvider; + } /** * {@inheritdoc} @@ -170,14 +180,6 @@ EOF } } - protected function getDirectoryIterator($directory) - { - return new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), - \RecursiveIteratorIterator::LEAVES_ONLY - ); - } - private function getStdin() { if (0 !== ftell(STDIN)) { @@ -201,8 +203,32 @@ EOF return $this->parser; } - protected function isReadable($fileOrDirectory) + private function getDirectoryIterator($directory) { - return is_readable($fileOrDirectory); + $default = function ($directory) { + return new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), + \RecursiveIteratorIterator::LEAVES_ONLY + ); + }; + + if (null !== $this->directoryIteratorProvider) { + return call_user_func($this->directoryIteratorProvider, $directory, $default); + } + + return $default($directory); + } + + private function isReadable($fileOrDirectory) + { + $default = function ($fileOrDirectory) { + return is_readable($fileOrDirectory); + }; + + if (null !== $this->isReadableProvider) { + return call_user_func($this->isReadableProvider, $fileOrDirectory, $default); + } + + return $default($fileOrDirectory); } }