Skip to content

[FrameworkBundle] Add path argument to dump a specific option in debug:config #18940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;

/**
Expand All @@ -34,6 +35,7 @@ protected function configure()
->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
Expand All @@ -60,14 +62,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
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;
}

$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);
Expand All @@ -77,13 +81,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
$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()
Expand All @@ -98,4 +116,28 @@ private function compileContainer()

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a PropertyAccessor instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice but dot notation seems to be supported for objects only.

$array = array('foo' => array('bar' => 'value'));
$value = PropertyAccessor::getValue($arr, 'foo.bar');

GIves

Cannot read property "foo" from an array. Maybe you intended to write the property path as "[foo]" instead.

It needs to be PropertyAccessor::getValue($array, '[foo][bar]').
Is there a real gain to parse the propertyPath then wrap each level in [level] for finally use the property accessor?


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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public function testDumpBundleName()
$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
*/
Expand Down