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 ;
38
39
#[AsCommand(name: 'debug:config ' , description: 'Dump the current configuration for an extension ' )]
39
40
class ConfigDebugCommand extends AbstractConfigCommand
40
41
{
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
-
50
42
protected function configure ()
51
43
{
52
44
$ this
53
45
->setDefinition ([
54
46
new InputArgument ('name ' , InputArgument::OPTIONAL , 'The bundle name or the extension alias ' ),
55
47
new InputArgument ('path ' , InputArgument::OPTIONAL , 'The configuration option path ' ),
56
48
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 ' ),
57
50
])
58
51
->setHelp (<<<'EOF'
59
52
The <info>%command.name%</info> command dumps the current configuration for an
@@ -64,13 +57,18 @@ protected function configure()
64
57
<info>php %command.full_name% framework</info>
65
58
<info>php %command.full_name% FrameworkBundle</info>
66
59
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):
68
67
69
68
<info>php %command.full_name% framework serializer.enabled</info>
70
69
71
70
EOF
72
- )
73
- ;
71
+ );
74
72
}
75
73
76
74
protected function execute (InputInterface $ input , OutputInterface $ output ): int
@@ -101,16 +99,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
101
99
102
100
$ config = $ this ->getConfig ($ extension , $ container , $ input ->getOption ('resolve-env ' ));
103
101
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
+
104
110
if (null === $ path = $ input ->getArgument ('path ' )) {
105
111
$ io ->title (
106
112
sprintf ('Current configuration for %s ' , $ name === $ extensionAlias ? sprintf ('extension with alias "%s" ' , $ extensionAlias ) : sprintf ('"%s" ' , $ name ))
107
113
);
108
114
109
- $ io ->writeln (Yaml:: dump ([$ extensionAlias => $ config ], 10 ));
115
+ $ io ->writeln ($ this -> convertToFormat ([$ extensionAlias => $ config ], $ format ));
110
116
111
117
return 0 ;
112
118
}
113
119
120
+ if ('yaml ' !== $ format ) {
121
+ $ errorIo ->error ('The "path" option is only available for the "yaml" format. ' );
122
+
123
+ return 1 ;
124
+ }
125
+
114
126
try {
115
127
$ config = $ this ->getConfigForPath ($ config , $ path , $ extensionAlias );
116
128
} catch (LogicException $ e ) {
@@ -121,11 +133,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
121
133
122
134
$ io ->title (sprintf ('Current configuration for "%s.%s" ' , $ extensionAlias , $ path ));
123
135
124
- $ io ->writeln (Yaml:: dump ($ config , 10 ));
136
+ $ io ->writeln ($ this -> convertToFormat ($ config , $ format ));
125
137
126
138
return 0 ;
127
139
}
128
140
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
+
129
150
private function compileContainer (): ContainerBuilder
130
151
{
131
152
$ kernel = clone $ this ->getApplication ()->getKernel ();
@@ -203,6 +224,10 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
203
224
} catch (LogicException ) {
204
225
}
205
226
}
227
+
228
+ if ($ input ->mustSuggestOptionValuesFor ('format ' )) {
229
+ $ suggestions ->suggestValues ($ this ->getAvailableFormatOptions ());
230
+ }
206
231
}
207
232
208
233
private function getAvailableBundles (bool $ alias ): array
@@ -237,4 +262,9 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''):
237
262
238
263
return $ completionPaths ;
239
264
}
265
+
266
+ private function getAvailableFormatOptions (): array
267
+ {
268
+ return ['yaml ' , 'json ' ];
269
+ }
240
270
}
0 commit comments