JsonDescriptor.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\Routing\Route;
  18. use Symfony\Component\Routing\RouteCollection;
  19. /**
  20. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  21. *
  22. * @internal
  23. */
  24. class JsonDescriptor extends Descriptor
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function describeRouteCollection(RouteCollection $routes, array $options = array())
  30. {
  31. $data = array();
  32. foreach ($routes->all() as $name => $route) {
  33. $data[$name] = $this->getRouteData($route);
  34. }
  35. $this->writeData($data, $options);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function describeRoute(Route $route, array $options = array())
  41. {
  42. $this->writeData($this->getRouteData($route), $options);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function describeContainerParameters(ParameterBag $parameters, array $options = array())
  48. {
  49. $this->writeData($this->sortParameters($parameters), $options);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function describeContainerTags(ContainerBuilder $builder, array $options = array())
  55. {
  56. $showPrivate = isset($options['show_private']) && $options['show_private'];
  57. $data = array();
  58. foreach ($this->findDefinitionsByTag($builder, $showPrivate) as $tag => $definitions) {
  59. $data[$tag] = array();
  60. foreach ($definitions as $definition) {
  61. $data[$tag][] = $this->getContainerDefinitionData($definition, true);
  62. }
  63. }
  64. $this->writeData($data, $options);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function describeContainerService($service, array $options = array())
  70. {
  71. if (!isset($options['id'])) {
  72. throw new \InvalidArgumentException('An "id" option must be provided.');
  73. }
  74. if ($service instanceof Alias) {
  75. $this->writeData($this->getContainerAliasData($service), $options);
  76. } elseif ($service instanceof Definition) {
  77. $this->writeData($this->getContainerDefinitionData($service), $options);
  78. } else {
  79. $this->writeData(\get_class($service), $options);
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
  86. {
  87. $serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
  88. $showPrivate = isset($options['show_private']) && $options['show_private'];
  89. $data = array('definitions' => array(), 'aliases' => array(), 'services' => array());
  90. foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
  91. $service = $this->resolveServiceDefinition($builder, $serviceId);
  92. if ($service instanceof Alias) {
  93. $data['aliases'][$serviceId] = $this->getContainerAliasData($service);
  94. } elseif ($service instanceof Definition) {
  95. if (($showPrivate || $service->isPublic())) {
  96. $data['definitions'][$serviceId] = $this->getContainerDefinitionData($service);
  97. }
  98. } else {
  99. $data['services'][$serviceId] = \get_class($service);
  100. }
  101. }
  102. $this->writeData($data, $options);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function describeContainerDefinition(Definition $definition, array $options = array())
  108. {
  109. $this->writeData($this->getContainerDefinitionData($definition, isset($options['omit_tags']) && $options['omit_tags']), $options);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function describeContainerAlias(Alias $alias, array $options = array())
  115. {
  116. $this->writeData($this->getContainerAliasData($alias), $options);
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. protected function describeEventDispatcherListeners(EventDispatcherInterface $eventDispatcher, array $options = array())
  122. {
  123. $this->writeData($this->getEventDispatcherListenersData($eventDispatcher, array_key_exists('event', $options) ? $options['event'] : null), $options);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. protected function describeCallable($callable, array $options = array())
  129. {
  130. $this->writeData($this->getCallableData($callable, $options), $options);
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. protected function describeContainerParameter($parameter, array $options = array())
  136. {
  137. $key = isset($options['parameter']) ? $options['parameter'] : '';
  138. $this->writeData(array($key => $parameter), $options);
  139. }
  140. /**
  141. * Writes data as json.
  142. *
  143. * @return array|string
  144. */
  145. private function writeData(array $data, array $options)
  146. {
  147. $flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
  148. if (\defined('JSON_PRETTY_PRINT')) {
  149. $flags |= JSON_PRETTY_PRINT;
  150. }
  151. $this->write(json_encode($data, $flags)."\n");
  152. }
  153. /**
  154. * @return array
  155. */
  156. protected function getRouteData(Route $route)
  157. {
  158. $requirements = $route->getRequirements();
  159. unset($requirements['_scheme'], $requirements['_method']);
  160. return array(
  161. 'path' => $route->getPath(),
  162. 'pathRegex' => $route->compile()->getRegex(),
  163. 'host' => '' !== $route->getHost() ? $route->getHost() : 'ANY',
  164. 'hostRegex' => '' !== $route->getHost() ? $route->compile()->getHostRegex() : '',
  165. 'scheme' => $route->getSchemes() ? implode('|', $route->getSchemes()) : 'ANY',
  166. 'method' => $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY',
  167. 'class' => \get_class($route),
  168. 'defaults' => $route->getDefaults(),
  169. 'requirements' => $requirements ?: 'NO CUSTOM',
  170. 'options' => $route->getOptions(),
  171. );
  172. }
  173. /**
  174. * @param Definition $definition
  175. * @param bool $omitTags
  176. *
  177. * @return array
  178. */
  179. private function getContainerDefinitionData(Definition $definition, $omitTags = false)
  180. {
  181. $data = array(
  182. 'class' => (string) $definition->getClass(),
  183. 'scope' => $definition->getScope(false),
  184. 'public' => $definition->isPublic(),
  185. 'synthetic' => $definition->isSynthetic(),
  186. 'lazy' => $definition->isLazy(),
  187. 'shared' => $definition->isShared(),
  188. 'synchronized' => $definition->isSynchronized(false),
  189. 'abstract' => $definition->isAbstract(),
  190. 'autowire' => $definition->isAutowired(),
  191. 'autowiring_types' => array(),
  192. 'file' => $definition->getFile(),
  193. );
  194. foreach ($definition->getAutowiringTypes() as $autowiringType) {
  195. $data['autowiring_types'][] = $autowiringType;
  196. }
  197. if ($definition->getFactoryClass(false)) {
  198. $data['factory_class'] = $definition->getFactoryClass(false);
  199. }
  200. if ($definition->getFactoryService(false)) {
  201. $data['factory_service'] = $definition->getFactoryService(false);
  202. }
  203. if ($definition->getFactoryMethod(false)) {
  204. $data['factory_method'] = $definition->getFactoryMethod(false);
  205. }
  206. if ($factory = $definition->getFactory()) {
  207. if (\is_array($factory)) {
  208. if ($factory[0] instanceof Reference) {
  209. $data['factory_service'] = (string) $factory[0];
  210. } elseif ($factory[0] instanceof Definition) {
  211. throw new \InvalidArgumentException('Factory is not describable.');
  212. } else {
  213. $data['factory_class'] = $factory[0];
  214. }
  215. $data['factory_method'] = $factory[1];
  216. } else {
  217. $data['factory_function'] = $factory;
  218. }
  219. }
  220. if (!$omitTags) {
  221. $data['tags'] = array();
  222. foreach ($definition->getTags() as $tagName => $tagData) {
  223. foreach ($tagData as $parameters) {
  224. $data['tags'][] = array('name' => $tagName, 'parameters' => $parameters);
  225. }
  226. }
  227. }
  228. return $data;
  229. }
  230. /**
  231. * @return array
  232. */
  233. private function getContainerAliasData(Alias $alias)
  234. {
  235. return array(
  236. 'service' => (string) $alias,
  237. 'public' => $alias->isPublic(),
  238. );
  239. }
  240. /**
  241. * @param EventDispatcherInterface $eventDispatcher
  242. * @param string|null $event
  243. *
  244. * @return array
  245. */
  246. private function getEventDispatcherListenersData(EventDispatcherInterface $eventDispatcher, $event = null)
  247. {
  248. $data = array();
  249. $registeredListeners = $eventDispatcher->getListeners($event);
  250. if (null !== $event) {
  251. foreach ($registeredListeners as $listener) {
  252. $l = $this->getCallableData($listener);
  253. $l['priority'] = $eventDispatcher->getListenerPriority($event, $listener);
  254. $data[] = $l;
  255. }
  256. } else {
  257. ksort($registeredListeners);
  258. foreach ($registeredListeners as $eventListened => $eventListeners) {
  259. foreach ($eventListeners as $eventListener) {
  260. $l = $this->getCallableData($eventListener);
  261. $l['priority'] = $eventDispatcher->getListenerPriority($eventListened, $eventListener);
  262. $data[$eventListened][] = $l;
  263. }
  264. }
  265. }
  266. return $data;
  267. }
  268. /**
  269. * @param callable $callable
  270. * @param array $options
  271. *
  272. * @return array
  273. */
  274. private function getCallableData($callable, array $options = array())
  275. {
  276. $data = array();
  277. if (\is_array($callable)) {
  278. $data['type'] = 'function';
  279. if (\is_object($callable[0])) {
  280. $data['name'] = $callable[1];
  281. $data['class'] = \get_class($callable[0]);
  282. } else {
  283. if (0 !== strpos($callable[1], 'parent::')) {
  284. $data['name'] = $callable[1];
  285. $data['class'] = $callable[0];
  286. $data['static'] = true;
  287. } else {
  288. $data['name'] = substr($callable[1], 8);
  289. $data['class'] = $callable[0];
  290. $data['static'] = true;
  291. $data['parent'] = true;
  292. }
  293. }
  294. return $data;
  295. }
  296. if (\is_string($callable)) {
  297. $data['type'] = 'function';
  298. if (false === strpos($callable, '::')) {
  299. $data['name'] = $callable;
  300. } else {
  301. $callableParts = explode('::', $callable);
  302. $data['name'] = $callableParts[1];
  303. $data['class'] = $callableParts[0];
  304. $data['static'] = true;
  305. }
  306. return $data;
  307. }
  308. if ($callable instanceof \Closure) {
  309. $data['type'] = 'closure';
  310. return $data;
  311. }
  312. if (method_exists($callable, '__invoke')) {
  313. $data['type'] = 'object';
  314. $data['name'] = \get_class($callable);
  315. return $data;
  316. }
  317. throw new \InvalidArgumentException('Callable is not describable.');
  318. }
  319. }