Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Fixed parsing new JSON output of debug:config not possible #50637

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
Jun 24, 2023
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
18 changes: 10 additions & 8 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function configure(): void
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Display resolved environment variable values instead of placeholders'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'yaml' : 'json'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'txt' : 'json'),
])
->setHelp(<<<EOF
The <info>%command.name%</info> command dumps the current configuration for an
Expand Down Expand Up @@ -97,16 +97,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$format = $input->getOption('format');

if ('yaml' === $format && !class_exists(Yaml::class)) {
$errorIo->error('Setting the "format" option to "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=json" instead.');
if (\in_array($format, ['txt', 'yml'], true) && !class_exists(Yaml::class)) {
$errorIo->error('Setting the "format" option to "txt" or "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=json" instead.');

return 1;
}

if (null === $path = $input->getArgument('path')) {
$io->title(
sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name))
);
if ('txt' === $input->getOption('format')) {
$io->title(
sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name))
);
}

$io->writeln($this->convertToFormat([$extensionAlias => $config], $format));

Expand All @@ -131,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function convertToFormat(mixed $config, string $format): string
{
return match ($format) {
'yaml' => Yaml::dump($config, 10),
'txt', 'yaml' => Yaml::dump($config, 10),
'json' => json_encode($config, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE),
default => throw new InvalidArgumentException(sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
};
Expand Down Expand Up @@ -268,6 +270,6 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''):

private function getAvailableFormatOptions(): array
{
return ['yaml', 'json'];
return ['txt', 'yaml', 'json'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public function testDumpBundleOption(bool $debug)
$this->assertStringContainsString('foo', $tester->getDisplay());
}

/**
* @testWith [true]
* [false]
*/
public function testDumpWithoutTitleIsValidJson(bool $debug)
{
$tester = $this->createCommandTester($debug);
$ret = $tester->execute(['name' => 'TestBundle', '--format' => 'json']);

$this->assertSame(0, $ret, 'Returns 0 in case of success');
$this->assertJson($tester->getDisplay());
}

/**
* @testWith [true]
* [false]
Expand All @@ -93,7 +106,7 @@ public function testDumpWithUnsupportedFormat(bool $debug)
$tester = $this->createCommandTester($debug);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Supported formats are "yaml", "json"');
$this->expectExceptionMessage('Supported formats are "txt", "yaml", "json"');

$tester->execute([
'name' => 'test',
Expand Down