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

Skip to content

Commit 6bf414c

Browse files
committed
feature #48457 [FrameworkBundle] Improve UX ConfigDebugCommand has not yaml component (alamirault)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [FrameworkBundle] Improve UX ConfigDebugCommand has not yaml component | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #48429 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Throw exception in order to improve UX when Yaml component is missing Message is inspired by https://github.com/symfony/symfony/blob/043257f6fc1fceb28bc0c37c59f2bea4d9c8d79d/src/Symfony/Component/Serializer/Encoder/YamlEncoder.php#L56 Commits ------- b20fce5 [FrameworkBundle] Improve UX ConfigDebugCommand has not yaml component
2 parents cb6f2c5 + b20fce5 commit 6bf414c

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `DomCrawlerAssertionsTrait::assertSelectorCount(int $count, string $selector)`
88
* Allow to avoid `limit` definition in a RateLimiter configuration when using the `no_limit` policy
9+
* Add `--format` option to the `debug:config` command
910

1011
6.2
1112
---

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Attribute\AsCommand;
1717
use Symfony\Component\Console\Completion\CompletionInput;
1818
use Symfony\Component\Console\Completion\CompletionSuggestions;
19+
use Symfony\Component\Console\Exception\InvalidArgumentException;
1920
use Symfony\Component\Console\Exception\LogicException;
2021
use Symfony\Component\Console\Input\InputArgument;
2122
use Symfony\Component\Console\Input\InputInterface;
@@ -40,13 +41,17 @@ class ConfigDebugCommand extends AbstractConfigCommand
4041
{
4142
protected function configure()
4243
{
44+
$commentedHelpFormats = array_map(static fn (string $format): string => sprintf('<comment>%s</comment>', $format), $this->getAvailableFormatOptions());
45+
$helpFormats = implode('", "', $commentedHelpFormats);
46+
4347
$this
4448
->setDefinition([
4549
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
4650
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
4751
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'),
4853
])
49-
->setHelp(<<<'EOF'
54+
->setHelp(<<<EOF
5055
The <info>%command.name%</info> command dumps the current configuration for an
5156
extension/bundle.
5257
@@ -55,6 +60,11 @@ protected function configure()
5560
<info>php %command.full_name% framework</info>
5661
<info>php %command.full_name% FrameworkBundle</info>
5762
63+
The <info>--format</info> option specifies the format of the configuration,
64+
this is either "{$helpFormats}".
65+
66+
<info>php %command.full_name% framework --format=json</info>
67+
5868
For dumping a specific option, add its path as second argument:
5969
6070
<info>php %command.full_name% framework serializer.enabled</info>
@@ -92,12 +102,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
92102

93103
$config = $this->getConfig($extension, $container, $input->getOption('resolve-env'));
94104

105+
$format = $input->getOption('format');
106+
107+
if ('yaml' === $format && !class_exists(Yaml::class)) {
108+
$errorIo->error('Setting the "format" option to "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=json" instead.');
109+
110+
return 1;
111+
}
112+
95113
if (null === $path = $input->getArgument('path')) {
96114
$io->title(
97115
sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name))
98116
);
99117

100-
$io->writeln(Yaml::dump([$extensionAlias => $config], 10));
118+
$io->writeln($this->convertToFormat([$extensionAlias => $config], $format));
101119

102120
return 0;
103121
}
@@ -112,11 +130,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
112130

113131
$io->title(sprintf('Current configuration for "%s.%s"', $extensionAlias, $path));
114132

115-
$io->writeln(Yaml::dump($config, 10));
133+
$io->writeln($this->convertToFormat($config, $format));
116134

117135
return 0;
118136
}
119137

138+
private function convertToFormat(mixed $config, string $format): string
139+
{
140+
return match ($format) {
141+
'yaml' => Yaml::dump($config, 10),
142+
'json' => json_encode($config, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE),
143+
default => throw new InvalidArgumentException(sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))),
144+
};
145+
}
146+
120147
private function compileContainer(): ContainerBuilder
121148
{
122149
$kernel = clone $this->getApplication()->getKernel();
@@ -194,6 +221,10 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
194221
} catch (LogicException) {
195222
}
196223
}
224+
225+
if ($input->mustSuggestOptionValuesFor('format')) {
226+
$suggestions->suggestValues($this->getAvailableFormatOptions());
227+
}
197228
}
198229

199230
private function getAvailableBundles(bool $alias): array
@@ -228,4 +259,9 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''):
228259

229260
return $completionPaths;
230261
}
262+
263+
private function getAvailableFormatOptions(): array
264+
{
265+
return ['yaml', 'json'];
266+
}
231267
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand;
1515
use Symfony\Bundle\FrameworkBundle\Console\Application;
16+
use Symfony\Component\Console\Exception\InvalidArgumentException;
1617
use Symfony\Component\Console\Input\ArrayInput;
1718
use Symfony\Component\Console\Output\NullOutput;
1819
use Symfony\Component\Console\Tester\CommandCompletionTester;
@@ -50,6 +51,19 @@ public function testDumpBundleOption()
5051
$this->assertStringContainsString('foo', $tester->getDisplay());
5152
}
5253

54+
public function testDumpWithUnsupportedFormat()
55+
{
56+
$tester = $this->createCommandTester();
57+
58+
$this->expectException(InvalidArgumentException::class);
59+
$this->expectExceptionMessage('Supported formats are "yaml", "json"');
60+
61+
$tester->execute([
62+
'name' => 'test',
63+
'--format' => 'xml',
64+
]);
65+
}
66+
5367
public function testParametersValuesAreResolved()
5468
{
5569
$tester = $this->createCommandTester();
@@ -157,6 +171,8 @@ public function provideCompletionSuggestions(): \Generator
157171
yield 'name (started CamelCase)' => [['Fra'], ['DefaultConfigTestBundle', 'ExtensionWithoutConfigTestBundle', 'FrameworkBundle', 'TestBundle']];
158172

159173
yield 'name with existing path' => [['framework', ''], ['secret', 'router.resource', 'router.utf8', 'router.enabled', 'validation.enabled', 'default_locale']];
174+
175+
yield 'option --format' => [['--format', ''], ['yaml', 'json']];
160176
}
161177

162178
private function createCommandTester(): CommandTester

0 commit comments

Comments
 (0)