PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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