Skip to content

[FrameworkBundle] fix yaml:lint when yaml is not installed along side framework-bundle #20066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,17 +22,39 @@
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class YamlLintCommand extends BaseLintCommand
class YamlLintCommand extends Command
{
private $command;

/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this->setName('lint:yaml');

if (!$this->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->setHelp(
$this->getHelp().<<<EOF
$this->command = new BaseLintCommand(null, $directoryIteratorProvider, $isReadableProvider);

$this
->setDescription($this->command->getDescription())
->setDefinition($this->command->getDefinition())
->setHelp($this->command->getHelp().<<<EOF

Or find all files in a bundle:

Expand All @@ -39,17 +64,16 @@ protected function configure()
);
}

protected function getDirectoryIterator($directory)
/**
* {@inheritdoc}
*/
public function isEnabled()
{
if (!is_dir($directory)) {
$directory = $this->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);
}
}
46 changes: 36 additions & 10 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't $directoryIteratorProvider and $isReadableProvider be type-hinted as callable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They could

{
parent::__construct($name);

$this->directoryIteratorProvider = $directoryIteratorProvider;
$this->isReadableProvider = $isReadableProvider;
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -170,14 +180,6 @@ private function getFiles($fileOrDirectory)
}
}

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)) {
Expand All @@ -201,8 +203,32 @@ private function getParser()
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);
}
}