PageRenderTime 67ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://gitlab.com/Isaki/le331.fr
PHP | 220 lines | 145 code | 39 blank | 36 comment | 16 complexity | 3ec53844f41e0ff97950de73582d6041 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\DependencyInjection\Loader\XmlFileLoader;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\Config\FileLocator;
  19. use Symfony\Component\Console\Question\ChoiceQuestion;
  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 his 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. if (false !== strpos($input->getFirstArgument(), ':d')) {
  78. $output->writeln('<comment>The use of "container:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:container" instead.</comment>');
  79. }
  80. $this->validateInput($input);
  81. if ($input->getOption('parameters')) {
  82. $object = $this->getContainerBuilder()->getParameterBag();
  83. $options = array();
  84. } elseif ($parameter = $input->getOption('parameter')) {
  85. $object = $this->getContainerBuilder();
  86. $options = array('parameter' => $parameter);
  87. } elseif ($input->getOption('tags')) {
  88. $object = $this->getContainerBuilder();
  89. $options = array('group_by' => 'tags', 'show_private' => $input->getOption('show-private'));
  90. } elseif ($tag = $input->getOption('tag')) {
  91. $object = $this->getContainerBuilder();
  92. $options = array('tag' => $tag, 'show_private' => $input->getOption('show-private'));
  93. } elseif ($name = $input->getArgument('name')) {
  94. $object = $this->getContainerBuilder();
  95. $name = $this->findProperServiceName($input, $output, $object, $name);
  96. $options = array('id' => $name);
  97. } else {
  98. $object = $this->getContainerBuilder();
  99. $options = array('show_private' => $input->getOption('show-private'));
  100. }
  101. $helper = new DescriptorHelper();
  102. $options['format'] = $input->getOption('format');
  103. $options['raw_text'] = $input->getOption('raw');
  104. $helper->describe($output, $object, $options);
  105. if (!$input->getArgument('name') && $input->isInteractive()) {
  106. $output->writeln('To search for a service, re-run this command with a search term. <comment>debug:container log</comment>');
  107. }
  108. }
  109. /**
  110. * Validates input arguments and options.
  111. *
  112. * @param InputInterface $input
  113. *
  114. * @throws \InvalidArgumentException
  115. */
  116. protected function validateInput(InputInterface $input)
  117. {
  118. $options = array('tags', 'tag', 'parameters', 'parameter');
  119. $optionsCount = 0;
  120. foreach ($options as $option) {
  121. if ($input->getOption($option)) {
  122. ++$optionsCount;
  123. }
  124. }
  125. $name = $input->getArgument('name');
  126. if ((null !== $name) && ($optionsCount > 0)) {
  127. throw new \InvalidArgumentException('The options tags, tag, parameters & parameter can not be combined with the service name argument.');
  128. } elseif ((null === $name) && $optionsCount > 1) {
  129. throw new \InvalidArgumentException('The options tags, tag, parameters & parameter can not be combined together.');
  130. }
  131. }
  132. /**
  133. * Loads the ContainerBuilder from the cache.
  134. *
  135. * @return ContainerBuilder
  136. *
  137. * @throws \LogicException
  138. */
  139. protected function getContainerBuilder()
  140. {
  141. if ($this->containerBuilder) {
  142. return $this->containerBuilder;
  143. }
  144. if (!$this->getApplication()->getKernel()->isDebug()) {
  145. throw new \LogicException(sprintf('Debug information about the container is only available in debug mode.'));
  146. }
  147. if (!is_file($cachedFile = $this->getContainer()->getParameter('debug.container.dump'))) {
  148. throw new \LogicException(sprintf('Debug information about the container could not be found. Please clear the cache and try again.'));
  149. }
  150. $container = new ContainerBuilder();
  151. $loader = new XmlFileLoader($container, new FileLocator());
  152. $loader->load($cachedFile);
  153. return $this->containerBuilder = $container;
  154. }
  155. private function findProperServiceName(InputInterface $input, OutputInterface $output, ContainerBuilder $builder, $name)
  156. {
  157. if ($builder->has($name) || !$input->isInteractive()) {
  158. return $name;
  159. }
  160. $matchingServices = $this->findServiceIdsContaining($builder, $name);
  161. if (empty($matchingServices)) {
  162. throw new \InvalidArgumentException(sprintf('No services found that match "%s".', $name));
  163. }
  164. $question = new ChoiceQuestion('Choose a number for more information on the service', $matchingServices);
  165. $question->setErrorMessage('Service %s is invalid.');
  166. return $this->getHelper('question')->ask($input, $output, $question);
  167. }
  168. private function findServiceIdsContaining(ContainerBuilder $builder, $name)
  169. {
  170. $serviceIds = $builder->getServiceIds();
  171. $foundServiceIds = array();
  172. $name = strtolower($name);
  173. foreach ($serviceIds as $serviceId) {
  174. if (false === strpos($serviceId, $name)) {
  175. continue;
  176. }
  177. $foundServiceIds[] = $serviceId;
  178. }
  179. return $foundServiceIds;
  180. }
  181. }