diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 3845ac5f7c0f0..ffb90d2ed671b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 2.5.0 ----- + * Added `config:debug` command * Added `yaml:lint` command * Deprecated the `RouterApacheDumperCommand` which will be removed in Symfony 3.0. diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php new file mode 100644 index 0000000000000..3b677ef1b1dcf --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php @@ -0,0 +1,86 @@ + + * + * 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 + * @author Wouter J + * @author Grégoire Pineau + */ +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)) { + // input is bundle name + + if (isset($bundles[$name])) { + $extension = $bundles[$name]->getContainerExtension(); + } + + if (!$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))); + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php new file mode 100644 index 0000000000000..df380ca07434d --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php @@ -0,0 +1,92 @@ + + * + * 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 + */ +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(<<%command.name% command dumps the current configuration for an +extension/bundle. + +Either the extension alias or bundle name can be used: + + php %command.full_name% framework + php %command.full_name% FrameworkBundle + +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)); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php index c94d167bc04c5..feb46e61e8ab6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php @@ -17,15 +17,15 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Config\Definition\ConfigurationInterface; /** * A console command for dumping available configuration reference * * @author Kevin Bond * @author Wouter J + * @author Grégoire Pineau */ -class ConfigDumpReferenceCommand extends ContainerDebugCommand +class ConfigDumpReferenceCommand extends AbstractConfigCommand { /** * {@inheritdoc} @@ -43,13 +43,16 @@ protected function configure() The %command.name% command dumps the default configuration for an extension/bundle. -The extension alias or bundle name can be used: +Either the extension alias or bundle name can be used: php %command.full_name% framework php %command.full_name% FrameworkBundle With the format option specifies the format of the configuration, -this is either yaml or xml. When the option is not provided, yaml is used. +this is either yaml or xml. +When the option is not provided, yaml is used. + + php %command.full_name% FrameworkBundle --format=xml EOF ) ; @@ -62,65 +65,24 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - $bundles = $this->getContainer()->get('kernel')->getBundles(); - $containerBuilder = $this->getContainerBuilder(); - $name = $input->getArgument('name'); if (empty($name)) { - $output->writeln('Available registered bundles with their extension alias if available:'); - - $table = $this->getHelperSet()->get('table'); - $table->setHeaders(array('Bundle name', 'Extension alias')); - foreach ($bundles as $bundle) { - $extension = $bundle->getContainerExtension(); - $table->addRow(array($bundle->getName(), $extension ? $extension->getAlias() : '')); - } - $table->render($output); + $this->listBundles($output); return; } - $extension = null; - - if (preg_match('/Bundle$/', $name)) { - // input is bundle name + $extension = $this->findExtention($name); - if (isset($bundles[$name])) { - $extension = $bundles[$name]->getContainerExtension(); - } + $configuration = $extension->getConfiguration(array(), $this->getContainerBuilder()); - if (!$extension) { - throw new \LogicException(sprintf('No extensions with configuration available for "%s"', $name)); - } + $this->validateConfiguration($extension, $configuration); - $message = 'Default configuration for "'.$name.'"'; + if ($name === $extension->getAlias()) { + $message = sprintf('Default configuration for extension with alias: "%s"', $name); } else { - foreach ($bundles as $bundle) { - $extension = $bundle->getContainerExtension(); - - if ($extension && $extension->getAlias() === $name) { - break; - } - - $extension = null; - } - - if (!$extension) { - throw new \LogicException(sprintf('No extension with alias "%s" is enabled', $name)); - } - - $message = 'Default configuration for extension with alias: "'.$name.'"'; - } - - $configuration = $extension->getConfiguration(array(), $containerBuilder); - - if (!$configuration) { - throw new \LogicException(sprintf('The extension with alias "%s" does not have it\'s 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))); + $message = sprintf('Default configuration for "%s"', $name); } switch ($input->getOption('format')) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php index fee2c6543e306..1d8ca9a4f954c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php @@ -86,8 +86,6 @@ protected function configure() /** * {@inheritdoc} - * - * @throws \LogicException */ protected function execute(InputInterface $input, OutputInterface $output) {