YamlDumper.php 11 KB

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