-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] config:dump-reference command can now dump current configuration #10165
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\DependencyInjection\Extension\Extension; | ||
|
||
/** | ||
* A console command for dumping available configuration reference | ||
* | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
* @author Wouter J <waldio.webdesign@gmail.com> | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class AbstractConfigCommand extends ContainerDebugCommand | ||
{ | ||
protected function listBundles(OutputInterface $output) | ||
{ | ||
$output->writeln('Available registered bundles with their extension alias if available:'); | ||
|
||
$table = $this->getHelperSet()->get('table'); | ||
$table->setHeaders(array('Bundle name', 'Extension alias')); | ||
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { | ||
$extension = $bundle->getContainerExtension(); | ||
$table->addRow(array($bundle->getName(), $extension ? $extension->getAlias() : '')); | ||
} | ||
|
||
$table->render($output); | ||
} | ||
|
||
protected function findExtention($name) | ||
{ | ||
$extension = null; | ||
|
||
$bundles = $this->getContainer()->get('kernel')->getBundles(); | ||
|
||
if (preg_match('/Bundle$/', $name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's harder to read. And speed does not matter here. It's not a production command, and were are talking about something like 0.001 ms ? |
||
// input is bundle name | ||
|
||
if (isset($bundles[$name])) { | ||
$extension = $bundles[$name]->getContainerExtension(); | ||
} | ||
|
||
if (!$extension) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we not add a check to see if the extension has a configuration, you can also have an extension without configuration, in that case the exception should also be thrown imo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method is about getting the extension, not the configuration for an extension |
||
throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name)); | ||
} | ||
} else { | ||
foreach ($bundles as $bundle) { | ||
$extension = $bundle->getContainerExtension(); | ||
|
||
if ($extension && $name === $extension->getAlias()) { | ||
break; | ||
} | ||
|
||
$extension = null; | ||
} | ||
|
||
if (!$extension) { | ||
throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name)); | ||
} | ||
} | ||
|
||
return $extension; | ||
} | ||
|
||
public function validateConfiguration(Extension $extension, $configuration) | ||
{ | ||
if (!$configuration) { | ||
throw new \LogicException(sprintf('The extension with alias "%s" does not have its getConfiguration() method setup', $extension->getAlias())); | ||
} | ||
|
||
if (!$configuration instanceof ConfigurationInterface) { | ||
throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable', get_class($configuration))); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* A console command for dumping available configuration reference | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class ConfigDebugCommand extends AbstractConfigCommand | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('config:debug') | ||
->setDefinition(array( | ||
new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'), | ||
)) | ||
->setDescription('Dumps the current configuration for an extension') | ||
->setHelp(<<<EOF | ||
|
||
The <info>%command.name%</info> command dumps the current configuration for an | ||
extension/bundle. | ||
|
||
Either the extension alias or bundle name can be used: | ||
|
||
<info>php %command.full_name% framework</info> | ||
<info>php %command.full_name% FrameworkBundle</info> | ||
|
||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$name = $input->getArgument('name'); | ||
|
||
if (empty($name)) { | ||
$this->listBundles($output); | ||
|
||
return; | ||
} | ||
|
||
$extension = $this->findExtention($name); | ||
|
||
$kernel = $this->getContainer()->get('kernel'); | ||
$method = new \ReflectionMethod($kernel, 'buildContainer'); | ||
$method->setAccessible(true); | ||
$container = $method->invoke($kernel); | ||
|
||
$configs = $container->getExtensionConfig($extension->getAlias()); | ||
$configuration = $extension->getConfiguration($configs, $container); | ||
|
||
$this->validateConfiguration($extension, $configuration); | ||
|
||
$processor = new Processor(); | ||
$config = $processor->processConfiguration($configuration, $configs); | ||
|
||
$config = $container->getParameterBag()->resolveValue($config); | ||
|
||
if ($name === $extension->getAlias()) { | ||
$output->writeln(sprintf('# Current configuration for extension with alias: "%s"', $name)); | ||
} else { | ||
$output->writeln(sprintf('# Current configuration for "%s"', $name)); | ||
} | ||
|
||
$output->writeln(Yaml::dump(array($extension->getAlias() => $config), 3)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there is no extension, the bundle can't be configured nor has it a config tree. So does it makes sense to list it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to break BC