feature #18940 [Console] Add path argument to dump a specific option in debug:config (chalasr)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[Console] Add path argument to dump a specific option in debug:config

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

This adds the ability to dump a specific bundle config option from the `debug:config` command.

For instance `debug:config StofDoctrineExtensionsBundle uploadable` gives:

![](http://image.prntscr.com/image/b78953dbe34c4efd817bb6f831ddd0ba.png)

I hesitated to just look for a `.` in the `name` argument rather than adding a `path` argument that doesn't include the bundle alias (that is took from the first argument of the command), let me know what you think about that.

Commits
-------

05ae01b [Console] Add path argument to dump a specific option in debug:config
This commit is contained in:
Fabien Potencier 2016-06-13 14:37:07 +02:00
commit 030abb25dc
2 changed files with 65 additions and 6 deletions

View File

@ -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(<<<EOF
@ -60,6 +62,7 @@ EOF
if (null === $name = $input->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. <comment>debug:config FrameworkBundle</comment>)');
$io->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>debug:config FrameworkBundle serializer</comment> to dump the <comment>framework.serializer</comment> 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;
}
}

View File

@ -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
*/