Skip to content

Updated the stlyes of the YAML commands #15991

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

Closed
wants to merge 2 commits into from
Closed
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
29 changes: 18 additions & 11 deletions src/Symfony/Bundle/FrameworkBundle/Command/YamlLintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
Expand Down Expand Up @@ -62,8 +63,10 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$stdout = $output;
$output = new SymfonyStyle($input, $output);
if (false !== strpos($input->getFirstArgument(), ':l')) {
$output->writeln('<comment>The use of "yaml:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:yaml" instead.</comment>');
$output->caution('The use of "yaml:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:yaml" instead.');
}

$filename = $input->getArgument('filename');
Expand All @@ -78,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$content .= fread(STDIN, 1024);
}

return $this->display($input, $output, array($this->validate($content)));
return $this->display($input, $stdout, $output, array($this->validate($content)));
}

if (0 !== strpos($filename, '@') && !is_readable($filename)) {
Expand All @@ -100,7 +103,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filesInfo[] = $this->validate(file_get_contents($file), $file);
}

return $this->display($input, $output, $filesInfo);
return $this->display($input, $stdout, $output, $filesInfo);
}

private function validate($content, $file = null)
Expand All @@ -115,33 +118,37 @@ private function validate($content, $file = null)
return array('file' => $file, 'valid' => true);
}

private function display(InputInterface $input, OutputInterface $output, $files)
private function display(InputInterface $input, OutputInterface $stdout, $output, $files)
{
switch ($input->getOption('format')) {
case 'txt':
return $this->displayTxt($output, $files);
return $this->displayTxt($stdout, $output, $files);
case 'json':
return $this->displayJson($output, $files);
default:
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
}
}

private function displayTxt(OutputInterface $output, $filesInfo)
private function displayTxt(OutputInterface $stdout, $output, $filesInfo)
{
$errors = 0;

foreach ($filesInfo as $info) {
if ($info['valid'] && $output->isVerbose()) {
$output->writeln('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
if ($info['valid'] && $stdout->isVerbose()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not convinced having the $stdout in every methods is needed just in order to use isVerbose here (SymfonyStyle implements OutputInterface which has the getVerbosity method). However, would it be a good idea to expose those methods (i.e: isQuiet, isVerbose, ...) in OutputStyle ?

$output->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
} elseif (!$info['valid']) {
++$errors;
$output->writeln(sprintf('<error>KO</error> in %s', $info['file']));
$output->writeln(sprintf('<error>>> %s</error>', $info['message']));
$output->text(sprintf('<error> ERROR </error> in %s', $info['file']));
$output->text(sprintf('<error> >> %s</error>', $info['message']));
}
}

$output->writeln(sprintf('<comment>%d/%d valid files</comment>', count($filesInfo) - $errors, count($filesInfo)));
if ($errors === 0) {
$output->success(sprintf('All %d YAML files contain valid syntax.', count($filesInfo)));
} else {
$output->warning(sprintf('%d YAML files have valid syntax and %d contain errors.', count($filesInfo) - $errors, $errors));
}

return min($errors, 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public function testDebugAllRoutes()
$ret = $tester->execute(array('name' => null), array('decorated' => false));

$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertContains('Path', $tester->getDisplay());
$this->assertContains('/foo', $tester->getDisplay());
$this->assertContains('Name Method Scheme Host Path', $tester->getDisplay());
}

public function testDebugSingleRoute()
Expand Down