ConfigDebugCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Config\Definition\Processor;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use Symfony\Component\Yaml\Yaml;
  17. /**
  18. * A console command for dumping available configuration reference.
  19. *
  20. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  21. */
  22. class ConfigDebugCommand extends AbstractConfigCommand
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('debug:config')
  31. ->setAliases(array(
  32. 'config:debug',
  33. ))
  34. ->setDefinition(array(
  35. new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
  36. ))
  37. ->setDescription('Dumps the current configuration for an extension')
  38. ->setHelp(<<<'EOF'
  39. The <info>%command.name%</info> command dumps the current configuration for an
  40. extension/bundle.
  41. Either the extension alias or bundle name can be used:
  42. <info>php %command.full_name% framework</info>
  43. <info>php %command.full_name% FrameworkBundle</info>
  44. EOF
  45. )
  46. ;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function execute(InputInterface $input, OutputInterface $output)
  52. {
  53. $io = new SymfonyStyle($input, $output);
  54. if (false !== strpos($input->getFirstArgument(), ':d')) {
  55. $io->caution('The use of "config:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:config" instead.');
  56. }
  57. $name = $input->getArgument('name');
  58. if (empty($name)) {
  59. $io->comment('Provide the name of a bundle as the first argument of this command to dump its configuration.');
  60. $io->newLine();
  61. $this->listBundles($output);
  62. return;
  63. }
  64. $extension = $this->findExtension($name);
  65. $container = $this->compileContainer();
  66. $configs = $container->getExtensionConfig($extension->getAlias());
  67. $configuration = $extension->getConfiguration($configs, $container);
  68. $this->validateConfiguration($extension, $configuration);
  69. $configs = $container->getParameterBag()->resolveValue($configs);
  70. $processor = new Processor();
  71. $config = $processor->processConfiguration($configuration, $configs);
  72. if ($name === $extension->getAlias()) {
  73. $io->title(sprintf('Current configuration for extension with alias "%s"', $name));
  74. } else {
  75. $io->title(sprintf('Current configuration for "%s"', $name));
  76. }
  77. $io->writeln(Yaml::dump(array($extension->getAlias() => $container->getParameterBag()->resolveValue($config)), 10));
  78. }
  79. private function compileContainer()
  80. {
  81. $kernel = clone $this->getContainer()->get('kernel');
  82. $kernel->boot();
  83. $method = new \ReflectionMethod($kernel, 'buildContainer');
  84. $method->setAccessible(true);
  85. $container = $method->invoke($kernel);
  86. $container->getCompiler()->compile($container);
  87. return $container;
  88. }
  89. }