From 05ae01bf02bd9fffadce56c1c5985e2abbc3b8db Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Wed, 1 Jun 2016 13:46:08 +0200 Subject: [PATCH] [Console] Add path argument to dump a specific option in debug:config Handle failure on undefined path, Add tests PHPDoc fix Remove useless assertion --- .../Command/ConfigDebugCommand.php | 54 ++++++++++++++++--- .../Functional/ConfigDebugCommandTest.php | 17 ++++++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php index 140d12fe35..3cd06fb46a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Console\Exception\LogicException; use Symfony\Component\Yaml\Yaml; /** @@ -34,6 +35,7 @@ class ConfigDebugCommand extends AbstractConfigCommand ->setName('debug:config') ->setDefinition(array( new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'), + new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'), )) ->setDescription('Dumps the current configuration for an extension') ->setHelp(<<getArgument('name')) { $this->listBundles($io); $io->comment('Provide the name of a bundle as the first argument of this command to dump its configuration. (e.g. debug:config FrameworkBundle)'); + $io->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. debug:config FrameworkBundle serializer to dump the framework.serializer configuration)'); return; } @@ -67,7 +70,8 @@ EOF $extension = $this->findExtension($name); $container = $this->compileContainer(); - $configs = $container->getExtensionConfig($extension->getAlias()); + $extensionAlias = $extension->getAlias(); + $configs = $container->getExtensionConfig($extensionAlias); $configuration = $extension->getConfiguration($configs, $container); $this->validateConfiguration($extension, $configuration); @@ -77,13 +81,27 @@ EOF $processor = new Processor(); $config = $processor->processConfiguration($configuration, $configs); - if ($name === $extension->getAlias()) { - $io->title(sprintf('Current configuration for extension with alias "%s"', $name)); - } else { - $io->title(sprintf('Current configuration for "%s"', $name)); + if (null === $path = $input->getArgument('path')) { + $io->title( + sprintf('Current configuration for %s', ($name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name))) + ); + + $io->writeln(Yaml::dump(array($extensionAlias => $config), 10)); + + return; } - $io->writeln(Yaml::dump(array($extension->getAlias() => $config), 10)); + try { + $config = $this->getConfigForPath($config, $path, $extensionAlias); + } catch (LogicException $e) { + $io->error($e->getMessage()); + + return; + } + + $io->title(sprintf('Current configuration for "%s.%s"', $extensionAlias, $path)); + + $io->writeln(Yaml::dump($config, 10)); } private function compileContainer() @@ -98,4 +116,28 @@ EOF return $container; } + + /** + * Iterate over configuration until the last step of the given path. + * + * @param array $config A bundle configuration. + * + * @throws LogicException If the configuration does not exist + * + * @return mixed + */ + private function getConfigForPath(array $config = array(), $path, $alias) + { + $steps = explode('.', $path); + + foreach ($steps as $step) { + if (!array_key_exists($step, $config)) { + throw new LogicException(sprintf('Unable to find configuration for "%s.%s"', $alias, $path)); + } + + $config = $config[$step]; + } + + return $config; + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php index dcffe78d97..0305d65f6f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ConfigDebugCommandTest.php @@ -39,6 +39,23 @@ class ConfigDebugCommandTest extends WebTestCase $this->assertContains('custom: foo', $tester->getDisplay()); } + public function testDumpBundleOption() + { + $tester = $this->createCommandTester(); + $ret = $tester->execute(array('name' => 'TestBundle', 'path' => 'custom')); + + $this->assertSame(0, $ret, 'Returns 0 in case of success'); + $this->assertContains('foo', $tester->getDisplay()); + } + + public function testDumpUndefinedBundleOption() + { + $tester = $this->createCommandTester(); + $ret = $tester->execute(array('name' => 'TestBundle', 'path' => 'foo')); + + $this->assertContains('Unable to find configuration for "test.foo"', $tester->getDisplay()); + } + /** * @return CommandTester */