Skip to content

Commit 1cbfc29

Browse files
committed
bug #50637 [FrameworkBundle] Fixed parsing new JSON output of debug:config not possible (Toflar)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [FrameworkBundle] Fixed parsing new JSON output of debug:config not possible | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | See my comment to the original PR which introduced `debug:config --format=json`: #48457 (comment) Currently, it's not possible to parse the JSON output (neither is the YAML) which makes `--format=json` kind of pointless. The argument against this - I guess - is that the JSON format was introduced to make the command independent from the YAML component but still, why is `--format` an option then? So I guess by delivering an additional option, we can fix this issue and make it parsable. (Maybe instead of having `--no-tilte` we should call it `parsable` in the first place so that if in the future there's something on top of the title, the option would still be valid?) Commits ------- a3e8560 [FrameworkBundle] Fixed parsing new JSON output of debug:config not possible
2 parents b615a67 + a3e8560 commit 1cbfc29

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function configure(): void
4949
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
5050
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
5151
new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Display resolved environment variable values instead of placeholders'),
52-
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'yaml' : 'json'),
52+
new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'txt' : 'json'),
5353
])
5454
->setHelp(<<<EOF
5555
The <info>%command.name%</info> command dumps the current configuration for an
@@ -97,16 +97,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9797

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

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

103103
return 1;
104104
}
105105

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

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

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

269271
private function getAvailableFormatOptions(): array
270272
{
271-
return ['yaml', 'json'];
273+
return ['txt', 'yaml', 'json'];
272274
}
273275
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ public function testDumpBundleOption(bool $debug)
8484
$this->assertStringContainsString('foo', $tester->getDisplay());
8585
}
8686

87+
/**
88+
* @testWith [true]
89+
* [false]
90+
*/
91+
public function testDumpWithoutTitleIsValidJson(bool $debug)
92+
{
93+
$tester = $this->createCommandTester($debug);
94+
$ret = $tester->execute(['name' => 'TestBundle', '--format' => 'json']);
95+
96+
$this->assertSame(0, $ret, 'Returns 0 in case of success');
97+
$this->assertJson($tester->getDisplay());
98+
}
99+
87100
/**
88101
* @testWith [true]
89102
* [false]
@@ -93,7 +106,7 @@ public function testDumpWithUnsupportedFormat(bool $debug)
93106
$tester = $this->createCommandTester($debug);
94107

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

98111
$tester->execute([
99112
'name' => 'test',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy