PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

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