YamlDumper.php 11 KB

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