diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php index 758dbf038ecfe..3982d78a8be29 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php @@ -49,7 +49,7 @@ protected function configure(): void new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'), new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'), new InputOption('resolve-env', null, InputOption::VALUE_NONE, 'Display resolved environment variable values instead of placeholders'), - new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'yaml' : 'json'), + new InputOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), class_exists(Yaml::class) ? 'txt' : 'json'), ]) ->setHelp(<<%command.name% command dumps the current configuration for an @@ -97,16 +97,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int $format = $input->getOption('format'); - if ('yaml' === $format && !class_exists(Yaml::class)) { - $errorIo->error('Setting the "format" option to "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=json" instead.'); + if (\in_array($format, ['txt', 'yml'], true) && !class_exists(Yaml::class)) { + $errorIo->error('Setting the "format" option to "txt" or "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=json" instead.'); return 1; } if (null === $path = $input->getArgument('path')) { - $io->title( - sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name)) - ); + if ('txt' === $input->getOption('format')) { + $io->title( + sprintf('Current configuration for %s', $name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name)) + ); + } $io->writeln($this->convertToFormat([$extensionAlias => $config], $format)); @@ -131,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function convertToFormat(mixed $config, string $format): string { return match ($format) { - 'yaml' => Yaml::dump($config, 10), + 'txt', 'yaml' => Yaml::dump($config, 10), 'json' => json_encode($config, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE), default => throw new InvalidArgumentException(sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))), }; @@ -268,6 +270,6 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''): private function getAvailableFormatOptions(): array { - return ['yaml', 'json']; + return ['txt', 'yaml', 'json']; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php index 70e795a3febab..f81c32f662645 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php @@ -84,6 +84,19 @@ public function testDumpBundleOption(bool $debug) $this->assertStringContainsString('foo', $tester->getDisplay()); } + /** + * @testWith [true] + * [false] + */ + public function testDumpWithoutTitleIsValidJson(bool $debug) + { + $tester = $this->createCommandTester($debug); + $ret = $tester->execute(['name' => 'TestBundle', '--format' => 'json']); + + $this->assertSame(0, $ret, 'Returns 0 in case of success'); + $this->assertJson($tester->getDisplay()); + } + /** * @testWith [true] * [false] @@ -93,7 +106,7 @@ public function testDumpWithUnsupportedFormat(bool $debug) $tester = $this->createCommandTester($debug); $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Supported formats are "yaml", "json"'); + $this->expectExceptionMessage('Supported formats are "txt", "yaml", "json"'); $tester->execute([ 'name' => 'test',