Descriptor.php 9.0 KB

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