ConfigDumpReferenceCommand.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Config\Definition\Dumper\XmlReferenceDumper;
  13. use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
  14. use Symfony\Component\Console\Exception\InvalidArgumentException;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
  21. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  22. use Symfony\Component\Yaml\Yaml;
  23. /**
  24. * A console command for dumping available configuration reference.
  25. *
  26. * @author Kevin Bond <kevinbond@gmail.com>
  27. * @author Wouter J <waldio.webdesign@gmail.com>
  28. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  29. *
  30. * @final
  31. */
  32. class ConfigDumpReferenceCommand extends AbstractConfigCommand
  33. {
  34. protected static $defaultName = 'config:dump-reference';
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function configure()
  39. {
  40. $this
  41. ->setDefinition([
  42. new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'),
  43. new InputArgument('path', InputArgument::OPTIONAL, 'The configuration option path'),
  44. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (yaml or xml)', 'yaml'),
  45. ])
  46. ->setDescription('Dump the default configuration for an extension')
  47. ->setHelp(<<<'EOF'
  48. The <info>%command.name%</info> command dumps the default configuration for an
  49. extension/bundle.
  50. Either the extension alias or bundle name can be used:
  51. <info>php %command.full_name% framework</info>
  52. <info>php %command.full_name% FrameworkBundle</info>
  53. With the <info>--format</info> option specifies the format of the configuration,
  54. this is either <comment>yaml</comment> or <comment>xml</comment>.
  55. When the option is not provided, <comment>yaml</comment> is used.
  56. <info>php %command.full_name% FrameworkBundle --format=xml</info>
  57. For dumping a specific option, add its path as second argument (only available for the yaml format):
  58. <info>php %command.full_name% framework profiler.matcher</info>
  59. EOF
  60. )
  61. ;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. *
  66. * @throws \LogicException
  67. */
  68. protected function execute(InputInterface $input, OutputInterface $output): int
  69. {
  70. $io = new SymfonyStyle($input, $output);
  71. $errorIo = $io->getErrorStyle();
  72. if (null === $name = $input->getArgument('name')) {
  73. $this->listBundles($errorIo);
  74. $kernel = $this->getApplication()->getKernel();
  75. if ($kernel instanceof ExtensionInterface
  76. && ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)
  77. && $kernel->getAlias()
  78. ) {
  79. $errorIo->table(['Kernel Extension'], [[$kernel->getAlias()]]);
  80. }
  81. $errorIo->comment([
  82. 'Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)',
  83. 'For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>config:dump-reference FrameworkBundle profiler.matcher</comment> to dump the <comment>framework.profiler.matcher</comment> configuration)',
  84. ]);
  85. return 0;
  86. }
  87. $extension = $this->findExtension($name);
  88. if ($extension instanceof ConfigurationInterface) {
  89. $configuration = $extension;
  90. } else {
  91. $configuration = $extension->getConfiguration([], $this->getContainerBuilder());
  92. }
  93. $this->validateConfiguration($extension, $configuration);
  94. $format = $input->getOption('format');
  95. if ('yaml' === $format && !class_exists(Yaml::class)) {
  96. $errorIo->error('Setting the "format" option to "yaml" requires the Symfony Yaml component. Try running "composer install symfony/yaml" or use "--format=xml" instead.');
  97. return 1;
  98. }
  99. $path = $input->getArgument('path');
  100. if (null !== $path && 'yaml' !== $format) {
  101. $errorIo->error('The "path" option is only available for the "yaml" format.');
  102. return 1;
  103. }
  104. if ($name === $extension->getAlias()) {
  105. $message = sprintf('Default configuration for extension with alias: "%s"', $name);
  106. } else {
  107. $message = sprintf('Default configuration for "%s"', $name);
  108. }
  109. if (null !== $path) {
  110. $message .= sprintf(' at path "%s"', $path);
  111. }
  112. switch ($format) {
  113. case 'yaml':
  114. $io->writeln(sprintf('# %s', $message));
  115. $dumper = new YamlReferenceDumper();
  116. break;
  117. case 'xml':
  118. $io->writeln(sprintf('<!-- %s -->', $message));
  119. $dumper = new XmlReferenceDumper();
  120. break;
  121. default:
  122. $io->writeln($message);
  123. throw new InvalidArgumentException('Only the yaml and xml formats are supported.');
  124. }
  125. $io->writeln(null === $path ? $dumper->dump($configuration, $extension->getNamespace()) : $dumper->dumpAtPath($configuration, $path));
  126. return 0;
  127. }
  128. }