Drupal investigation

YamlDumper.php 11KB

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