PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php

https://gitlab.com/pr0055/symfonypizza
PHP | 312 lines | 139 code | 40 blank | 133 comment | 18 complexity | 79ea951965dc1217e3b1a2f5f11093d8 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\Console\Descriptor;
  11. use Symfony\Component\Console\Descriptor\DescriptorInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\DependencyInjection\Alias;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\Routing\Route;
  19. use Symfony\Component\Routing\RouteCollection;
  20. /**
  21. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  22. *
  23. * @internal
  24. */
  25. abstract class Descriptor implements DescriptorInterface
  26. {
  27. /**
  28. * @var OutputInterface
  29. */
  30. protected $output;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function describe(OutputInterface $output, $object, array $options = array())
  35. {
  36. $this->output = $output;
  37. switch (true) {
  38. case $object instanceof RouteCollection:
  39. $this->describeRouteCollection($object, $options);
  40. break;
  41. case $object instanceof Route:
  42. $this->describeRoute($object, $options);
  43. break;
  44. case $object instanceof ParameterBag:
  45. $this->describeContainerParameters($object, $options);
  46. break;
  47. case $object instanceof ContainerBuilder && isset($options['group_by']) && 'tags' === $options['group_by']:
  48. $this->describeContainerTags($object, $options);
  49. break;
  50. case $object instanceof ContainerBuilder && isset($options['id']):
  51. $this->describeContainerService($this->resolveServiceDefinition($object, $options['id']), $options);
  52. break;
  53. case $object instanceof ContainerBuilder && isset($options['parameter']):
  54. $this->describeContainerParameter($object->getParameter($options['parameter']), $options);
  55. break;
  56. case $object instanceof ContainerBuilder:
  57. $this->describeContainerServices($object, $options);
  58. break;
  59. case $object instanceof Definition:
  60. $this->describeContainerDefinition($object, $options);
  61. break;
  62. case $object instanceof Alias:
  63. $this->describeContainerAlias($object, $options);
  64. break;
  65. case $object instanceof EventDispatcherInterface:
  66. $this->describeEventDispatcherListeners($object, $options);
  67. break;
  68. case is_callable($object):
  69. $this->describeCallable($object, $options);
  70. break;
  71. default:
  72. throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
  73. }
  74. }
  75. /**
  76. * Returns the output.
  77. *
  78. * @return OutputInterface The output
  79. */
  80. protected function getOutput()
  81. {
  82. return $this->output;
  83. }
  84. /**
  85. * Writes content to output.
  86. *
  87. * @param string $content
  88. * @param bool $decorated
  89. */
  90. protected function write($content, $decorated = false)
  91. {
  92. $this->output->write($content, false, $decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW);
  93. }
  94. /**
  95. * Describes an InputArgument instance.
  96. *
  97. * @param RouteCollection $routes
  98. * @param array $options
  99. */
  100. abstract protected function describeRouteCollection(RouteCollection $routes, array $options = array());
  101. /**
  102. * Describes an InputOption instance.
  103. *
  104. * @param Route $route
  105. * @param array $options
  106. */
  107. abstract protected function describeRoute(Route $route, array $options = array());
  108. /**
  109. * Describes container parameters.
  110. *
  111. * @param ParameterBag $parameters
  112. * @param array $options
  113. */
  114. abstract protected function describeContainerParameters(ParameterBag $parameters, array $options = array());
  115. /**
  116. * Describes container tags.
  117. *
  118. * @param ContainerBuilder $builder
  119. * @param array $options
  120. */
  121. abstract protected function describeContainerTags(ContainerBuilder $builder, array $options = array());
  122. /**
  123. * Describes a container service by its name.
  124. *
  125. * Common options are:
  126. * * name: name of described service
  127. *
  128. * @param Definition|Alias|object $service
  129. * @param array $options
  130. */
  131. abstract protected function describeContainerService($service, array $options = array());
  132. /**
  133. * Describes container services.
  134. *
  135. * Common options are:
  136. * * tag: filters described services by given tag
  137. *
  138. * @param ContainerBuilder $builder
  139. * @param array $options
  140. */
  141. abstract protected function describeContainerServices(ContainerBuilder $builder, array $options = array());
  142. /**
  143. * Describes a service definition.
  144. *
  145. * @param Definition $definition
  146. * @param array $options
  147. */
  148. abstract protected function describeContainerDefinition(Definition $definition, array $options = array());
  149. /**
  150. * Describes a service alias.
  151. *
  152. * @param Alias $alias
  153. * @param array $options
  154. */
  155. abstract protected function describeContainerAlias(Alias $alias, array $options = array());
  156. /**
  157. * Describes a container parameter.
  158. *
  159. * @param string $parameter
  160. * @param array $options
  161. */
  162. abstract protected function describeContainerParameter($parameter, array $options = array());
  163. /**
  164. * Describes event dispatcher listeners.
  165. *
  166. * Common options are:
  167. * * name: name of listened event
  168. *
  169. * @param EventDispatcherInterface $eventDispatcher
  170. * @param array $options
  171. */
  172. abstract protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = array());
  173. /**
  174. * Describes a callable.
  175. *
  176. * @param callable $callable
  177. * @param array $options
  178. */
  179. abstract protected function describeCallable($callable, array $options = array());
  180. /**
  181. * Formats a value as string.
  182. *
  183. * @param mixed $value
  184. *
  185. * @return string
  186. */
  187. protected function formatValue($value)
  188. {
  189. if (is_object($value)) {
  190. return sprintf('object(%s)', get_class($value));
  191. }
  192. if (is_string($value)) {
  193. return $value;
  194. }
  195. return preg_replace("/\n\s*/s", '', var_export($value, true));
  196. }
  197. /**
  198. * Formats a parameter.
  199. *
  200. * @param mixed $value
  201. *
  202. * @return string
  203. */
  204. protected function formatParameter($value)
  205. {
  206. if (is_bool($value) || is_array($value) || (null === $value)) {
  207. $jsonString = json_encode($value);
  208. if (preg_match('/^(.{60})./us', $jsonString, $matches)) {
  209. return $matches[1].'...';
  210. }
  211. return $jsonString;
  212. }
  213. return (string) $value;
  214. }
  215. /**
  216. * @param ContainerBuilder $builder
  217. * @param string $serviceId
  218. *
  219. * @return mixed
  220. */
  221. protected function resolveServiceDefinition(ContainerBuilder $builder, $serviceId)
  222. {
  223. if ($builder->hasDefinition($serviceId)) {
  224. return $builder->getDefinition($serviceId);
  225. }
  226. // Some service IDs don't have a Definition, they're simply an Alias
  227. if ($builder->hasAlias($serviceId)) {
  228. return $builder->getAlias($serviceId);
  229. }
  230. if ('service_container' === $serviceId) {
  231. return $builder;
  232. }
  233. // the service has been injected in some special way, just return the service
  234. return $builder->get($serviceId);
  235. }
  236. /**
  237. * @param ContainerBuilder $builder
  238. * @param bool $showPrivate
  239. *
  240. * @return array
  241. */
  242. protected function findDefinitionsByTag(ContainerBuilder $builder, $showPrivate)
  243. {
  244. $definitions = array();
  245. $tags = $builder->findTags();
  246. asort($tags);
  247. foreach ($tags as $tag) {
  248. foreach ($builder->findTaggedServiceIds($tag) as $serviceId => $attributes) {
  249. $definition = $this->resolveServiceDefinition($builder, $serviceId);
  250. if (!$definition instanceof Definition || !$showPrivate && !$definition->isPublic()) {
  251. continue;
  252. }
  253. if (!isset($definitions[$tag])) {
  254. $definitions[$tag] = array();
  255. }
  256. $definitions[$tag][$serviceId] = $definition;
  257. }
  258. }
  259. return $definitions;
  260. }
  261. protected function sortParameters(ParameterBag $parameters)
  262. {
  263. $parameters = $parameters->all();
  264. ksort($parameters);
  265. return $parameters;
  266. }
  267. protected function sortServiceIds(array $serviceIds)
  268. {
  269. asort($serviceIds);
  270. return $serviceIds;
  271. }
  272. }