16
16
use Symfony \Component \Console \Attribute \AsCommand ;
17
17
use Symfony \Component \Console \Completion \CompletionInput ;
18
18
use Symfony \Component \Console \Completion \CompletionSuggestions ;
19
+ use Symfony \Component \Console \Exception \InvalidArgumentException ;
19
20
use Symfony \Component \Console \Exception \LogicException ;
20
21
use Symfony \Component \Console \Input \InputArgument ;
21
22
use Symfony \Component \Console \Input \InputInterface ;
@@ -40,13 +41,17 @@ class ConfigDebugCommand extends AbstractConfigCommand
40
41
{
41
42
protected function configure ()
42
43
{
44
+ $ commentedHelpFormats = array_map (static fn (string $ format ): string => sprintf ('<comment>%s</comment> ' , $ format ), $ this ->getAvailableFormatOptions ());
45
+ $ helpFormats = implode ('", " ' , $ commentedHelpFormats );
46
+
43
47
$ this
44
48
->setDefinition ([
45
49
new InputArgument ('name ' , InputArgument::OPTIONAL , 'The bundle name or the extension alias ' ),
46
50
new InputArgument ('path ' , InputArgument::OPTIONAL , 'The configuration option path ' ),
47
51
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 ' ),
48
53
])
49
- ->setHelp (<<<' EOF'
54
+ ->setHelp (<<<EOF
50
55
The <info>%command.name%</info> command dumps the current configuration for an
51
56
extension/bundle.
52
57
@@ -55,6 +60,11 @@ protected function configure()
55
60
<info>php %command.full_name% framework</info>
56
61
<info>php %command.full_name% FrameworkBundle</info>
57
62
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
+
58
68
For dumping a specific option, add its path as second argument:
59
69
60
70
<info>php %command.full_name% framework serializer.enabled</info>
@@ -92,12 +102,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
92
102
93
103
$ config = $ this ->getConfig ($ extension , $ container , $ input ->getOption ('resolve-env ' ));
94
104
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
+
95
113
if (null === $ path = $ input ->getArgument ('path ' )) {
96
114
$ io ->title (
97
115
sprintf ('Current configuration for %s ' , $ name === $ extensionAlias ? sprintf ('extension with alias "%s" ' , $ extensionAlias ) : sprintf ('"%s" ' , $ name ))
98
116
);
99
117
100
- $ io ->writeln (Yaml:: dump ([$ extensionAlias => $ config ], 10 ));
118
+ $ io ->writeln ($ this -> convertToFormat ([$ extensionAlias => $ config ], $ format ));
101
119
102
120
return 0 ;
103
121
}
@@ -112,11 +130,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
112
130
113
131
$ io ->title (sprintf ('Current configuration for "%s.%s" ' , $ extensionAlias , $ path ));
114
132
115
- $ io ->writeln (Yaml:: dump ($ config , 10 ));
133
+ $ io ->writeln ($ this -> convertToFormat ($ config , $ format ));
116
134
117
135
return 0 ;
118
136
}
119
137
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
+
120
147
private function compileContainer (): ContainerBuilder
121
148
{
122
149
$ kernel = clone $ this ->getApplication ()->getKernel ();
@@ -194,6 +221,10 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
194
221
} catch (LogicException ) {
195
222
}
196
223
}
224
+
225
+ if ($ input ->mustSuggestOptionValuesFor ('format ' )) {
226
+ $ suggestions ->suggestValues ($ this ->getAvailableFormatOptions ());
227
+ }
197
228
}
198
229
199
230
private function getAvailableBundles (bool $ alias ): array
@@ -228,4 +259,9 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''):
228
259
229
260
return $ completionPaths ;
230
261
}
262
+
263
+ private function getAvailableFormatOptions (): array
264
+ {
265
+ return ['yaml ' , 'json ' ];
266
+ }
231
267
}
0 commit comments