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

Skip to content

Commit 058f9f0

Browse files
committed
Add format option to debug:config command
1 parent 21043b2 commit 058f9f0

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `--format` option to the `debug:config` command
8+
49
6.2
510
---
611

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

Lines changed: 44 additions & 14 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;
@@ -38,22 +39,14 @@
3839
#[AsCommand(name: 'debug:config', description: 'Dump the current configuration for an extension')]
3940
class ConfigDebugCommand extends AbstractConfigCommand
4041
{
41-
public function __construct(string $name = null)
42-
{
43-
if (!class_exists(Yaml::class)) {
44-
throw new \RuntimeException('The ConfigDebugCommand class requires the "Yaml" component. Install "symfony/yaml" to use it.');
45-
}
46-
47-
parent::__construct($name);
48-
}
49-
5042
protected function configure()
5143
{
5244
$this
5345
->setDefinition([
5446
new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
5547
new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
5648
new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Display resolved environment variable values instead of placeholders'),
49+
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (yaml or json)', 'yaml'),
5750
])
5851
->setHelp(<<<'EOF'
5952
The <info>%command.name%</info> command dumps the current configuration for an
@@ -64,13 +57,18 @@ protected function configure()
6457
<info>php %command.full_name% framework</info>
6558
<info>php %command.full_name% FrameworkBundle</info>
6659
67-
For dumping a specific option, add its path as second argument:
60+
With the <info>--format</info> option specifies the format of the configuration,
61+
this is either <comment>yaml</comment> or <comment>json</comment>.
62+
When the option is not provided, <comment>yaml</comment> is used.
63+
64+
<info>php %command.full_name% FrameworkBundle --format=json</info>
65+
66+
For dumping a specific option, add its path as second argument (only available for the yaml format):
6867
6968
<info>php %command.full_name% framework serializer.enabled</info>
7069

7170
EOF
72-
)
73-
;
71+
);
7472
}
7573

7674
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -101,16 +99,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10199

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

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

109-
$io->writeln(Yaml::dump([$extensionAlias => $config], 10));
115+
$io->writeln($this->convertToFormat([$extensionAlias => $config], $format));
110116

111117
return 0;
112118
}
113119

120+
if ('yaml' !== $format) {
121+
$errorIo->error('The "path" option is only available for the "yaml" format.');
122+
123+
return 1;
124+
}
125+
114126
try {
115127
$config = $this->getConfigForPath($config, $path, $extensionAlias);
116128
} catch (LogicException $e) {
@@ -121,11 +133,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
121133

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

124-
$io->writeln(Yaml::dump($config, 10));
136+
$io->writeln($this->convertToFormat($config, $format));
125137

126138
return 0;
127139
}
128140

141+
private function convertToFormat(mixed $config, string $format): string
142+
{
143+
return match ($format) {
144+
'yaml' => Yaml::dump($config, 10),
145+
'json' => json_encode($config, \JSON_PRETTY_PRINT),
146+
default => throw new InvalidArgumentException('Only the yaml and json formats are supported.'),
147+
};
148+
}
149+
129150
private function compileContainer(): ContainerBuilder
130151
{
131152
$kernel = clone $this->getApplication()->getKernel();
@@ -203,6 +224,10 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
203224
} catch (LogicException) {
204225
}
205226
}
227+
228+
if ($input->mustSuggestOptionValuesFor('format')) {
229+
$suggestions->suggestValues($this->getAvailableFormatOptions());
230+
}
206231
}
207232

208233
private function getAvailableBundles(bool $alias): array
@@ -237,4 +262,9 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''):
237262

238263
return $completionPaths;
239264
}
265+
266+
private function getAvailableFormatOptions(): array
267+
{
268+
return ['yaml', 'json'];
269+
}
240270
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public function testDumpBundleOption()
5050
$this->assertStringContainsString('foo', $tester->getDisplay());
5151
}
5252

53+
public function testDumpAtPathWithFormat()
54+
{
55+
$tester = $this->createCommandTester();
56+
$ret = $tester->execute([
57+
'name' => 'test',
58+
'path' => 'array',
59+
'--format' => 'json',
60+
]);
61+
62+
$this->assertSame(1, $ret);
63+
$this->assertStringContainsString('[ERROR] The "path" option is only available for the "yaml" format.', $tester->getDisplay());
64+
}
65+
5366
public function testParametersValuesAreResolved()
5467
{
5568
$tester = $this->createCommandTester();
@@ -157,6 +170,8 @@ public function provideCompletionSuggestions(): \Generator
157170
yield 'name (started CamelCase)' => [['Fra'], ['DefaultConfigTestBundle', 'ExtensionWithoutConfigTestBundle', 'FrameworkBundle', 'TestBundle']];
158171

159172
yield 'name with existing path' => [['framework', ''], ['secret', 'router.resource', 'router.utf8', 'router.enabled', 'validation.enabled', 'default_locale']];
173+
174+
yield 'option --format' => [['--format', ''], ['yaml', 'json']];
160175
}
161176

162177
private function createCommandTester(): CommandTester

0 commit comments

Comments
 (0)