ConfigDumpReferenceCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Dumper\XmlReferenceDumper;
  12. use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. /**
  19. * A console command for dumping available configuration reference.
  20. *
  21. * @author Kevin Bond <kevinbond@gmail.com>
  22. * @author Wouter J <waldio.webdesign@gmail.com>
  23. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  24. */
  25. class ConfigDumpReferenceCommand extends AbstractConfigCommand
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('config:dump-reference')
  34. ->setDefinition(array(
  35. new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'),
  36. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (yaml or xml)', 'yaml'),
  37. ))
  38. ->setDescription('Dumps the default configuration for an extension')
  39. ->setHelp(<<<'EOF'
  40. The <info>%command.name%</info> command dumps the default configuration for an
  41. extension/bundle.
  42. Either the extension alias or bundle name can be used:
  43. <info>php %command.full_name% framework</info>
  44. <info>php %command.full_name% FrameworkBundle</info>
  45. With the <info>--format</info> option specifies the format of the configuration,
  46. this is either <comment>yaml</comment> or <comment>xml</comment>.
  47. When the option is not provided, <comment>yaml</comment> is used.
  48. <info>php %command.full_name% FrameworkBundle --format=xml</info>
  49. EOF
  50. )
  51. ;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. *
  56. * @throws \LogicException
  57. */
  58. protected function execute(InputInterface $input, OutputInterface $output)
  59. {
  60. $io = new SymfonyStyle($input, $output);
  61. $name = $input->getArgument('name');
  62. if (empty($name)) {
  63. $io->comment('Provide the name of a bundle as the first argument of this command to dump its default configuration.');
  64. $io->newLine();
  65. $this->listBundles($output);
  66. return;
  67. }
  68. $extension = $this->findExtension($name);
  69. $configuration = $extension->getConfiguration(array(), $this->getContainerBuilder());
  70. $this->validateConfiguration($extension, $configuration);
  71. if ($name === $extension->getAlias()) {
  72. $message = sprintf('Default configuration for extension with alias: "%s"', $name);
  73. } else {
  74. $message = sprintf('Default configuration for "%s"', $name);
  75. }
  76. switch ($input->getOption('format')) {
  77. case 'yaml':
  78. $io->writeln(sprintf('# %s', $message));
  79. $dumper = new YamlReferenceDumper();
  80. break;
  81. case 'xml':
  82. $io->writeln(sprintf('<!-- %s -->', $message));
  83. $dumper = new XmlReferenceDumper();
  84. break;
  85. default:
  86. $io->writeln($message);
  87. throw new \InvalidArgumentException('Only the yaml and xml formats are supported.');
  88. }
  89. $io->writeln($dumper->dump($configuration, $extension->getNamespace()));
  90. }
  91. }