ContainerDebugCommand.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
  12. use Symfony\Component\Config\FileLocator;
  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. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  20. /**
  21. * A console command for retrieving information about services.
  22. *
  23. * @author Ryan Weaver <ryan@thatsquality.com>
  24. */
  25. class ContainerDebugCommand extends ContainerAwareCommand
  26. {
  27. /**
  28. * @var ContainerBuilder|null
  29. */
  30. protected $containerBuilder;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function configure()
  35. {
  36. $this
  37. ->setName('debug:container')
  38. ->setAliases(array(
  39. 'container:debug',
  40. ))
  41. ->setDefinition(array(
  42. new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
  43. new InputOption('show-private', null, InputOption::VALUE_NONE, 'Used to show public *and* private services'),
  44. new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Shows all services with a specific tag'),
  45. new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application'),
  46. new InputOption('parameter', null, InputOption::VALUE_REQUIRED, 'Displays a specific parameter for an application'),
  47. new InputOption('parameters', null, InputOption::VALUE_NONE, 'Displays parameters for an application'),
  48. new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
  49. new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw description'),
  50. ))
  51. ->setDescription('Displays current services for an application')
  52. ->setHelp(<<<'EOF'
  53. The <info>%command.name%</info> command displays all configured <comment>public</comment> services:
  54. <info>php %command.full_name%</info>
  55. To get specific information about a service, specify its name:
  56. <info>php %command.full_name% validator</info>
  57. By default, private services are hidden. You can display all services by
  58. using the <info>--show-private</info> flag:
  59. <info>php %command.full_name% --show-private</info>
  60. Use the --tags option to display tagged <comment>public</comment> services grouped by tag:
  61. <info>php %command.full_name% --tags</info>
  62. Find all services with a specific tag by specifying the tag name with the <info>--tag</info> option:
  63. <info>php %command.full_name% --tag=form.type</info>
  64. Use the <info>--parameters</info> option to display all parameters:
  65. <info>php %command.full_name% --parameters</info>
  66. Display a specific parameter by specifying its name with the <info>--parameter</info> option:
  67. <info>php %command.full_name% --parameter=kernel.debug</info>
  68. EOF
  69. )
  70. ;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function execute(InputInterface $input, OutputInterface $output)
  76. {
  77. $io = new SymfonyStyle($input, $output);
  78. if (false !== strpos($input->getFirstArgument(), ':d')) {
  79. $io->caution('The use of "container:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:container" instead.');
  80. }
  81. $this->validateInput($input);
  82. $object = $this->getContainerBuilder();
  83. if ($input->getOption('parameters')) {
  84. $object = $object->getParameterBag();
  85. $options = array();
  86. } elseif ($parameter = $input->getOption('parameter')) {
  87. $options = array('parameter' => $parameter);
  88. } elseif ($input->getOption('tags')) {
  89. $options = array('group_by' => 'tags', 'show_private' => $input->getOption('show-private'));
  90. } elseif ($tag = $input->getOption('tag')) {
  91. $options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
  92. } elseif ($name = $input->getArgument('name')) {
  93. $name = $this->findProperServiceName($input, $io, $object, $name);
  94. $options = array('id' => $name);
  95. } else {
  96. $options = array('show_private' => $input->getOption('show-private'));
  97. }
  98. $helper = new DescriptorHelper();
  99. $options['format'] = $input->getOption('format');
  100. $options['raw_text'] = $input->getOption('raw');
  101. $options['output'] = $io;
  102. $helper->describe($output, $object, $options);
  103. if (!$input->getArgument('name') && $input->isInteractive()) {
  104. $io->comment('To search for a specific service, re-run this command with a search term. (e.g. <comment>debug:container log</comment>)');
  105. }
  106. }
  107. /**
  108. * Validates input arguments and options.
  109. *
  110. * @throws \InvalidArgumentException
  111. */
  112. protected function validateInput(InputInterface $input)
  113. {
  114. $options = array('tags', 'tag', 'parameters', 'parameter');
  115. $optionsCount = 0;
  116. foreach ($options as $option) {
  117. if ($input->getOption($option)) {
  118. ++$optionsCount;
  119. }
  120. }
  121. $name = $input->getArgument('name');
  122. if ((null !== $name) && ($optionsCount > 0)) {
  123. throw new \InvalidArgumentException('The options tags, tag, parameters & parameter can not be combined with the service name argument.');
  124. } elseif ((null === $name) && $optionsCount > 1) {
  125. throw new \InvalidArgumentException('The options tags, tag, parameters & parameter can not be combined together.');
  126. }
  127. }
  128. /**
  129. * Loads the ContainerBuilder from the cache.
  130. *
  131. * @return ContainerBuilder
  132. *
  133. * @throws \LogicException
  134. */
  135. protected function getContainerBuilder()
  136. {
  137. if ($this->containerBuilder) {
  138. return $this->containerBuilder;
  139. }
  140. if (!$this->getApplication()->getKernel()->isDebug()) {
  141. throw new \LogicException('Debug information about the container is only available in debug mode.');
  142. }
  143. if (!is_file($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) {
  144. throw new \LogicException('Debug information about the container could not be found. Please clear the cache and try again.');
  145. }
  146. $container = new ContainerBuilder();
  147. $loader = new XmlFileLoader($container, new FileLocator());
  148. $loader->load($cachedFile);
  149. return $this->containerBuilder = $container;
  150. }
  151. private function findProperServiceName(InputInterface $input, SymfonyStyle $io, ContainerBuilder $builder, $name)
  152. {
  153. if ($builder->has($name) || !$input->isInteractive()) {
  154. return $name;
  155. }
  156. $matchingServices = $this->findServiceIdsContaining($builder, $name);
  157. if (empty($matchingServices)) {
  158. throw new \InvalidArgumentException(sprintf('No services found that match "%s".', $name));
  159. }
  160. return $io->choice('Select one of the following services to display its information', $matchingServices);
  161. }
  162. private function findServiceIdsContaining(ContainerBuilder $builder, $name)
  163. {
  164. $serviceIds = $builder->getServiceIds();
  165. $foundServiceIds = array();
  166. $name = strtolower($name);
  167. foreach ($serviceIds as $serviceId) {
  168. if (false === strpos($serviceId, $name)) {
  169. continue;
  170. }
  171. $foundServiceIds[] = $serviceId;
  172. }
  173. return $foundServiceIds;
  174. }
  175. }