ConfigDebugCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\ConfigurationInterface;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
  20. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  21. use Symfony\Component\Yaml\Yaml;
  22. /**
  23. * A console command for dumping available configuration reference.
  24. *
  25. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  26. *
  27. * @final
  28. */
  29. class ConfigDebugCommand extends AbstractConfigCommand
  30. {
  31. protected static $defaultName = 'debug:config';
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function configure()
  36. {
  37. $this
  38. ->setDefinition([
  39. new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'),
  40. new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
  41. ])
  42. ->setDescription('Dump the current configuration for an extension')
  43. ->setHelp(<<<'EOF'
  44. The <info>%command.name%</info> command dumps the current configuration for an
  45. extension/bundle.
  46. Either the extension alias or bundle name can be used:
  47. <info>php %command.full_name% framework</info>
  48. <info>php %command.full_name% FrameworkBundle</info>
  49. For dumping a specific option, add its path as second argument:
  50. <info>php %command.full_name% framework serializer.enabled</info>
  51. EOF
  52. )
  53. ;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function execute(InputInterface $input, OutputInterface $output): int
  59. {
  60. $io = new SymfonyStyle($input, $output);
  61. $errorIo = $io->getErrorStyle();
  62. if (null === $name = $input->getArgument('name')) {
  63. $this->listBundles($errorIo);
  64. $kernel = $this->getApplication()->getKernel();
  65. if ($kernel instanceof ExtensionInterface
  66. && ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)
  67. && $kernel->getAlias()
  68. ) {
  69. $errorIo->table(['Kernel Extension'], [[$kernel->getAlias()]]);
  70. }
  71. $errorIo->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>)');
  72. $errorIo->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)');
  73. return 0;
  74. }
  75. $extension = $this->findExtension($name);
  76. $container = $this->compileContainer();
  77. $extensionAlias = $extension->getAlias();
  78. $extensionConfig = [];
  79. foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
  80. if ($pass instanceof ValidateEnvPlaceholdersPass) {
  81. $extensionConfig = $pass->getExtensionConfig();
  82. break;
  83. }
  84. }
  85. if (!isset($extensionConfig[$extensionAlias])) {
  86. throw new \LogicException(sprintf('The extension with alias "%s" does not have configuration.', $extensionAlias));
  87. }
  88. $config = $container->resolveEnvPlaceholders($extensionConfig[$extensionAlias]);
  89. if (null === $path = $input->getArgument('path')) {
  90. $io->title(
  91. sprintf('Current configuration for %s', ($name === $extensionAlias ? sprintf('extension with alias "%s"', $extensionAlias) : sprintf('"%s"', $name)))
  92. );
  93. $io->writeln(Yaml::dump([$extensionAlias => $config], 10));
  94. return 0;
  95. }
  96. try {
  97. $config = $this->getConfigForPath($config, $path, $extensionAlias);
  98. } catch (LogicException $e) {
  99. $errorIo->error($e->getMessage());
  100. return 1;
  101. }
  102. $io->title(sprintf('Current configuration for "%s.%s"', $extensionAlias, $path));
  103. $io->writeln(Yaml::dump($config, 10));
  104. return 0;
  105. }
  106. private function compileContainer(): ContainerBuilder
  107. {
  108. $kernel = clone $this->getApplication()->getKernel();
  109. $kernel->boot();
  110. $method = new \ReflectionMethod($kernel, 'buildContainer');
  111. $method->setAccessible(true);
  112. $container = $method->invoke($kernel);
  113. $container->getCompiler()->compile($container);
  114. return $container;
  115. }
  116. /**
  117. * Iterate over configuration until the last step of the given path.
  118. *
  119. * @throws LogicException If the configuration does not exist
  120. *
  121. * @return mixed
  122. */
  123. private function getConfigForPath(array $config, string $path, string $alias)
  124. {
  125. $steps = explode('.', $path);
  126. foreach ($steps as $step) {
  127. if (!\array_key_exists($step, $config)) {
  128. throw new LogicException(sprintf('Unable to find configuration for "%s.%s".', $alias, $path));
  129. }
  130. $config = $config[$step];
  131. }
  132. return $config;
  133. }
  134. }