1616use Symfony \Component \Console \Attribute \AsCommand ;
1717use Symfony \Component \Console \Completion \CompletionInput ;
1818use Symfony \Component \Console \Completion \CompletionSuggestions ;
19+ use Symfony \Component \Console \Exception \InvalidArgumentException ;
1920use Symfony \Component \Console \Exception \LogicException ;
2021use Symfony \Component \Console \Input \InputArgument ;
2122use Symfony \Component \Console \Input \InputInterface ;
3839#[AsCommand(name: 'debug:config ' , description: 'Dump the current configuration for an extension ' )]
3940class 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'
5952The <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
7170EOF
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}
0 commit comments