PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php

https://github.com/nguyennamtien/TaskBoxx
PHP | 266 lines | 155 code | 36 blank | 75 comment | 29 complexity | fa568ab73338a05d3911ca5f41546524 MD5 | raw file
  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\Yaml;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\Parameter;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * YamlDumper dumps a service container as a YAML string.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class YamlDumper extends Dumper
  21. {
  22. /**
  23. * Dumps the service container as an YAML string.
  24. *
  25. * @param array $options An array of options
  26. *
  27. * @return string A YAML string representing of the service container
  28. */
  29. public function dump(array $options = array())
  30. {
  31. return $this->addParameters()."\n".$this->addServices();
  32. }
  33. /**
  34. * Adds a service
  35. *
  36. * @param string $id
  37. * @param Definition $definition
  38. * @return string
  39. */
  40. private function addService($id, $definition)
  41. {
  42. $code = " $id:\n";
  43. if ($definition->getClass()) {
  44. $code .= sprintf(" class: %s\n", $definition->getClass());
  45. }
  46. $tagsCode = '';
  47. foreach ($definition->getTags() as $name => $tags) {
  48. foreach ($tags as $attributes) {
  49. $att = array();
  50. foreach ($attributes as $key => $value) {
  51. $att[] = sprintf('%s: %s', Yaml::dump($key), Yaml::dump($value));
  52. }
  53. $att = $att ? ', '.implode(' ', $att) : '';
  54. $tagsCode .= sprintf(" - { name: %s%s }\n", Yaml::dump($name), $att);
  55. }
  56. }
  57. if ($tagsCode) {
  58. $code .= " tags:\n".$tagsCode;
  59. }
  60. if ($definition->getFile()) {
  61. $code .= sprintf(" file: %s\n", $definition->getFile());
  62. }
  63. if ($definition->getFactoryMethod()) {
  64. $code .= sprintf(" factory_method: %s\n", $definition->getFactoryMethod());
  65. }
  66. if ($definition->getFactoryService()) {
  67. $code .= sprintf(" factory_service: %s\n", $definition->getFactoryService());
  68. }
  69. if ($definition->getArguments()) {
  70. $code .= sprintf(" arguments: %s\n", Yaml::dump($this->dumpValue($definition->getArguments()), 0));
  71. }
  72. if ($definition->getProperties()) {
  73. $code .= sprintf(" properties: %s\n", Yaml::dump($this->dumpValue($definition->getProperties()), 0));
  74. }
  75. if ($definition->getMethodCalls()) {
  76. $code .= sprintf(" calls:\n %s\n", str_replace("\n", "\n ", Yaml::dump($this->dumpValue($definition->getMethodCalls()), 1)));
  77. }
  78. if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
  79. $code .= sprintf(" scope: %s\n", $scope);
  80. }
  81. if ($callable = $definition->getConfigurator()) {
  82. if (is_array($callable)) {
  83. if (is_object($callable[0]) && $callable[0] instanceof Reference) {
  84. $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
  85. } else {
  86. $callable = array($callable[0], $callable[1]);
  87. }
  88. }
  89. $code .= sprintf(" configurator: %s\n", Yaml::dump($callable, 0));
  90. }
  91. return $code;
  92. }
  93. /**
  94. * Adds a service alias
  95. *
  96. * @param string $alias
  97. * @param string $id
  98. * @return string
  99. */
  100. private function addServiceAlias($alias, $id)
  101. {
  102. if ($id->isPublic()) {
  103. return sprintf(" %s: @%s\n", $alias, $id);
  104. } else {
  105. return sprintf(" %s:\n alias: %s\n public: false", $alias, $id);
  106. }
  107. }
  108. /**
  109. * Adds services
  110. *
  111. * @return string
  112. */
  113. private function addServices()
  114. {
  115. if (!$this->container->getDefinitions()) {
  116. return '';
  117. }
  118. $code = "services:\n";
  119. foreach ($this->container->getDefinitions() as $id => $definition) {
  120. $code .= $this->addService($id, $definition);
  121. }
  122. foreach ($this->container->getAliases() as $alias => $id) {
  123. $code .= $this->addServiceAlias($alias, $id);
  124. }
  125. return $code;
  126. }
  127. /**
  128. * Adds parameters
  129. *
  130. * @return string
  131. */
  132. private function addParameters()
  133. {
  134. if (!$this->container->getParameterBag()->all()) {
  135. return '';
  136. }
  137. if ($this->container->isFrozen()) {
  138. $parameters = $this->prepareParameters($this->container->getParameterBag()->all());
  139. } else {
  140. $parameters = $this->container->getParameterBag()->all();
  141. }
  142. return Yaml::dump(array('parameters' => $parameters), 2);
  143. }
  144. /**
  145. * Dumps the value to YAML format
  146. *
  147. * @param mixed $value
  148. * @throws \RuntimeException When trying to dump object or resource
  149. */
  150. private function dumpValue($value)
  151. {
  152. if (is_array($value)) {
  153. $code = array();
  154. foreach ($value as $k => $v) {
  155. $code[$k] = $this->dumpValue($v);
  156. }
  157. return $code;
  158. } elseif (is_object($value) && $value instanceof Reference) {
  159. return $this->getServiceCall((string) $value, $value);
  160. } elseif (is_object($value) && $value instanceof Parameter) {
  161. return $this->getParameterCall((string) $value);
  162. } elseif (is_object($value) || is_resource($value)) {
  163. throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  164. }
  165. return $value;
  166. }
  167. /**
  168. * Gets the service call.
  169. *
  170. * @param string $id
  171. * @param Reference $reference
  172. * @return string
  173. */
  174. private function getServiceCall($id, Reference $reference = null)
  175. {
  176. if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
  177. return sprintf('@?%s', $id);
  178. }
  179. return sprintf('@%s', $id);
  180. }
  181. /**
  182. * Gets parameter call.
  183. *
  184. * @param string $id
  185. * @return string
  186. */
  187. private function getParameterCall($id)
  188. {
  189. return sprintf('%%%s%%', $id);
  190. }
  191. /**
  192. * Prepares parameters
  193. *
  194. * @param array $parameters
  195. * @return array
  196. */
  197. private function prepareParameters($parameters)
  198. {
  199. $filtered = array();
  200. foreach ($parameters as $key => $value) {
  201. if (is_array($value)) {
  202. $value = $this->prepareParameters($value);
  203. } elseif ($value instanceof Reference) {
  204. $value = '@'.$value;
  205. }
  206. $filtered[$key] = $value;
  207. }
  208. return $this->escape($filtered);
  209. }
  210. /**
  211. * Escapes arguments
  212. *
  213. * @param array $arguments
  214. * @return array
  215. */
  216. private function escape($arguments)
  217. {
  218. $args = array();
  219. foreach ($arguments as $k => $v) {
  220. if (is_array($v)) {
  221. $args[$k] = $this->escape($v);
  222. } elseif (is_string($v)) {
  223. $args[$k] = str_replace('%', '%%', $v);
  224. } else {
  225. $args[$k] = $v;
  226. }
  227. }
  228. return $args;
  229. }
  230. }