/sites/all/modules/service_container/lib/Drupal/Core/DependencyInjection/Dumper/PhpArrayDumper.php

https://gitlab.com/leoplanxxi/dr7-web-buap-2016 · PHP · 359 lines · 200 code · 40 blank · 119 comment · 27 complexity · acde83469630333cc6d021ff96dc39ce MD5 · raw file

  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\DependencyInjection\Dumper\PhpArrayDumper
  5. */
  6. namespace Drupal\Core\DependencyInjection\Dumper;
  7. use Symfony\Component\DependencyInjection\Alias;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\DependencyInjection\Definition;
  10. use Symfony\Component\DependencyInjection\Parameter;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  13. use Symfony\Component\DependencyInjection\Dumper\Dumper;
  14. /**
  15. * PhpArrayDumper dumps a service container as a serialized PHP array.
  16. */
  17. class PhpArrayDumper extends Dumper
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function dump(array $options = array())
  23. {
  24. return serialize($this->getArray());
  25. }
  26. /**
  27. * Returns the service container as a PHP array.
  28. *
  29. * @return array
  30. * A PHP array represention of the service container
  31. */
  32. public function getArray()
  33. {
  34. $definition = array();
  35. $definition['parameters'] = $this->getParameters();
  36. $definition['services'] = $this->getServiceDefinitions();
  37. return $definition;
  38. }
  39. /**
  40. * Returns parameters of the container as a PHP Array.
  41. *
  42. * @return array
  43. * The escaped and prepared parameters of the container.
  44. */
  45. protected function getParameters()
  46. {
  47. if (!$this->container->getParameterBag()->all()) {
  48. return array();
  49. }
  50. $parameters = $this->container->getParameterBag()->all();
  51. $is_frozen = $this->container->isFrozen();
  52. return $this->prepareParameters($parameters, $is_frozen);
  53. }
  54. /**
  55. * Returns services of the container as a PHP Array.
  56. *
  57. * @return array
  58. * The service definitions.
  59. */
  60. protected function getServiceDefinitions()
  61. {
  62. if (!$this->container->getDefinitions()) {
  63. return array();
  64. }
  65. $services = array();
  66. foreach ($this->container->getDefinitions() as $id => $definition) {
  67. $services[$id] = $this->getServiceDefinition($definition);
  68. }
  69. $aliases = $this->container->getAliases();
  70. foreach ($aliases as $alias => $id) {
  71. while (isset($aliases[(string) $id])) {
  72. $id = $aliases[(string) $id];
  73. }
  74. $services[$alias] = $this->getServiceAliasDefinition($id);
  75. }
  76. return $services;
  77. }
  78. /**
  79. * Prepares parameters.
  80. *
  81. * @param array $parameters
  82. * @param bool $escape
  83. *
  84. * @return array
  85. */
  86. protected function prepareParameters($parameters, $escape = true)
  87. {
  88. $filtered = array();
  89. foreach ($parameters as $key => $value) {
  90. if (is_array($value)) {
  91. $value = $this->prepareParameters($value, $escape);
  92. }
  93. elseif ($value instanceof Reference) {
  94. $value = '@'.$value;
  95. }
  96. $filtered[$key] = $value;
  97. }
  98. return $escape ? $this->escape($filtered) : $filtered;
  99. }
  100. /**
  101. * Escapes arguments.
  102. *
  103. * @param array $arguments
  104. * The arguments to escape.
  105. *
  106. * @return array
  107. * The escaped arguments.
  108. */
  109. protected function escape($arguments)
  110. {
  111. $args = array();
  112. foreach ($arguments as $k => $v) {
  113. if (is_array($v)) {
  114. $args[$k] = $this->escape($v);
  115. }
  116. elseif (is_string($v)) {
  117. $args[$k] = str_replace('%', '%%', $v);
  118. }
  119. else {
  120. $args[$k] = $v;
  121. }
  122. }
  123. return $args;
  124. }
  125. /**
  126. * Gets a service definition as PHP array.
  127. *
  128. * @param \Symfony\Component\DependencyInjection\Definition $definition
  129. * The definition to process.
  130. *
  131. * @return array
  132. * The service definition as PHP array.
  133. */
  134. protected function getServiceDefinition($definition)
  135. {
  136. $service = array();
  137. if ($definition->getClass()) {
  138. $service['class'] = $definition->getClass();
  139. }
  140. if (!$definition->isPublic()) {
  141. $service['public'] = FALSE;
  142. }
  143. /*
  144. $tagsCode = '';
  145. foreach ($definition->getTags() as $name => $tags) {
  146. foreach ($tags as $attributes) {
  147. $att = array();
  148. foreach ($attributes as $key => $value) {
  149. $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
  150. }
  151. $att = $att ? ', '.implode(', ', $att) : '';
  152. $tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper->dump($name), $att);
  153. }
  154. }
  155. if ($tagsCode) {
  156. $code .= " tags:\n".$tagsCode;
  157. }
  158. */
  159. if ($definition->getFile()) {
  160. $service['file'] = $definition->getFile();
  161. }
  162. if ($definition->isSynthetic()) {
  163. $service['synthetic'] = TRUE;
  164. }
  165. if ($definition->isLazy()) {
  166. $service['lazy'] = TRUE;
  167. }
  168. if ($definition->getArguments()) {
  169. $service['arguments'] = $this->dumpValue($definition->getArguments());
  170. }
  171. if ($definition->getProperties()) {
  172. $service['properties'] = $this->dumpValue($definition->getProperties());
  173. }
  174. if ($definition->getMethodCalls()) {
  175. $service['calls'] = $this->dumpValue($definition->getMethodCalls());
  176. }
  177. if (($scope = $definition->getScope()) !== ContainerInterface::SCOPE_CONTAINER) {
  178. $service['scope'] = $scope;
  179. }
  180. if (($decorated = $definition->getDecoratedService()) !== NULL) {
  181. $service['decorates'] = $decorated;
  182. }
  183. if ($callable = $definition->getFactory()) {
  184. $service['factory'] = $this->dumpCallable($callable);
  185. }
  186. if ($callable = $definition->getConfigurator()) {
  187. $service['configurator'] = $this->dumpCallable($callable);
  188. }
  189. return $service;
  190. }
  191. /**
  192. * Returns a service alias definiton.
  193. *
  194. * @param string $alias
  195. * @param Alias $id
  196. *
  197. * @return string
  198. */
  199. protected function getServiceAliasDefinition($id)
  200. {
  201. if ($id->isPublic()) {
  202. return array(
  203. 'alias' => (string) $id,
  204. );
  205. } else {
  206. return array(
  207. 'alias' => (string) $id,
  208. 'public' => FALSE,
  209. );
  210. }
  211. }
  212. /**
  213. * Dumps callable to YAML format
  214. *
  215. * @param callable $callable
  216. *
  217. * @return callable
  218. */
  219. protected function dumpCallable($callable)
  220. {
  221. if (is_array($callable)) {
  222. if ($callable[0] instanceof Reference) {
  223. $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
  224. }
  225. elseif ($callable[0] instanceof Definition) {
  226. $callable[0] = $this->getPrivateService($callable[0]);
  227. $callable = array($callable[0], $callable[1]);
  228. }
  229. else {
  230. $callable = array($callable[0], $callable[1]);
  231. }
  232. }
  233. return $callable;
  234. }
  235. /**
  236. * Returns a private service definition in a suitable format.
  237. *
  238. * @param \Symfony\Component\DependencyInjection\Definition $definition
  239. * The definition to process.
  240. *
  241. * @return \stdClass
  242. * A very lightweight private service value object.
  243. */
  244. protected function getPrivateService(Definition $definition) {
  245. $service_definition = $this->getServiceDefinition($definition);
  246. $hash = sha1(serialize($service_definition));
  247. return (object) array(
  248. 'type' => 'service',
  249. 'id' => 'private__' . $hash,
  250. 'value' => $service_definition,
  251. );
  252. }
  253. /**
  254. * Dumps the value to YAML format.
  255. *
  256. * @param mixed $value
  257. *
  258. * @return mixed
  259. *
  260. * @throws RuntimeException When trying to dump object or resource
  261. */
  262. protected function dumpValue($value)
  263. {
  264. if (is_array($value)) {
  265. $code = array();
  266. foreach ($value as $k => $v) {
  267. $code[$k] = $this->dumpValue($v);
  268. }
  269. return $code;
  270. } elseif ($value instanceof Reference) {
  271. return $this->getServiceCall((string) $value, $value);
  272. } elseif ($value instanceof Definition) {
  273. return $this->getPrivateService($value);
  274. } elseif ($value instanceof Parameter) {
  275. return $this->getParameterCall((string) $value);
  276. } elseif ($value instanceof Expression) {
  277. return $this->getExpressionCall((string) $value);
  278. } elseif (is_object($value)) {
  279. if (isset($value->_serviceId)) {
  280. return '@' . $value->_serviceId;
  281. }
  282. throw new RuntimeException('Unable to dump a service container if a parameter is an object without _serviceId.');
  283. } elseif (is_resource($value)) {
  284. throw new RuntimeException('Unable to dump a service container if a parameter is a resource.');
  285. }
  286. return $value;
  287. }
  288. /**
  289. * Gets the service call.
  290. *
  291. * @param string $id
  292. * @param Reference $reference
  293. *
  294. * @return string
  295. */
  296. protected function getServiceCall($id, Reference $reference = null)
  297. {
  298. if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
  299. return sprintf('@?%s', $id);
  300. }
  301. return sprintf('@%s', $id);
  302. }
  303. /**
  304. * Gets parameter call.
  305. *
  306. * @param string $id
  307. *
  308. * @return string
  309. */
  310. protected function getParameterCall($id)
  311. {
  312. return sprintf('%%%s%%', $id);
  313. }
  314. protected function getExpressionCall($expression)
  315. {
  316. return sprintf('@=%s', $expression);
  317. }
  318. }