YamlDumper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Symfony\Components\DependencyInjection\Dumper;
  3. use Symfony\Components\YAML\YAML;
  4. use Symfony\Components\DependencyInjection\Container;
  5. use Symfony\Components\DependencyInjection\Parameter;
  6. use Symfony\Components\DependencyInjection\Reference;
  7. /*
  8. * This file is part of the symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * YamlDumper dumps a service container as a YAML string.
  17. *
  18. * @package symfony
  19. * @subpackage dependency_injection
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class YamlDumper extends Dumper
  23. {
  24. /**
  25. * Dumps the service container as an YAML string.
  26. *
  27. * @param array $options An array of options
  28. *
  29. * @return string A YAML string representing of the service container
  30. */
  31. public function dump(array $options = array())
  32. {
  33. return $this->addParameters()."\n".$this->addServices();
  34. }
  35. protected function addService($id, $definition)
  36. {
  37. $code = " $id:\n";
  38. $code .= sprintf(" class: %s\n", $definition->getClass());
  39. if ($definition->getFile())
  40. {
  41. $code .= sprintf(" file: %s\n", $definition->getFile());
  42. }
  43. if ($definition->getConstructor())
  44. {
  45. $code .= sprintf(" constructor: %s\n", $definition->getConstructor());
  46. }
  47. if ($definition->getArguments())
  48. {
  49. $code .= sprintf(" arguments: %s\n", YAML::dump($this->dumpValue($definition->getArguments()), 0));
  50. }
  51. if ($definition->getMethodCalls())
  52. {
  53. $code .= sprintf(" calls:\n %s\n", str_replace("\n", "\n ", YAML::dump($this->dumpValue($definition->getMethodCalls()), 1)));
  54. }
  55. if (!$definition->isShared())
  56. {
  57. $code .= " shared: false\n";
  58. }
  59. if ($callable = $definition->getConfigurator())
  60. {
  61. if (is_array($callable))
  62. {
  63. if (is_object($callable[0]) && $callable[0] instanceof Reference)
  64. {
  65. $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]);
  66. }
  67. else
  68. {
  69. $callable = array($callable[0], $callable[1]);
  70. }
  71. }
  72. $code .= sprintf(" configurator: %s\n", YAML::dump($callable, 0));
  73. }
  74. return $code;
  75. }
  76. protected function addServiceAlias($alias, $id)
  77. {
  78. return sprintf(" %s: @%s\n", $alias, $id);
  79. }
  80. protected function addServices()
  81. {
  82. if (!$this->container->getDefinitions())
  83. {
  84. return '';
  85. }
  86. $code = "services:\n";
  87. foreach ($this->container->getDefinitions() as $id => $definition)
  88. {
  89. $code .= $this->addService($id, $definition);
  90. }
  91. foreach ($this->container->getAliases() as $alias => $id)
  92. {
  93. $code .= $this->addServiceAlias($alias, $id);
  94. }
  95. return $code;
  96. }
  97. protected function addParameters()
  98. {
  99. if (!$this->container->getParameters())
  100. {
  101. return '';
  102. }
  103. return YAML::dump(array('parameters' => $this->prepareParameters($this->container->getParameters())), 2);
  104. }
  105. protected function dumpValue($value)
  106. {
  107. if (is_array($value))
  108. {
  109. $code = array();
  110. foreach ($value as $k => $v)
  111. {
  112. $code[$k] = $this->dumpValue($v);
  113. }
  114. return $code;
  115. }
  116. elseif (is_object($value) && $value instanceof Reference)
  117. {
  118. return $this->getServiceCall((string) $value, $value);
  119. }
  120. elseif (is_object($value) && $value instanceof Parameter)
  121. {
  122. return $this->getParameterCall((string) $value);
  123. }
  124. elseif (is_object($value) || is_resource($value))
  125. {
  126. throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  127. }
  128. else
  129. {
  130. return $value;
  131. }
  132. }
  133. protected function getServiceCall($id, Reference $reference = null)
  134. {
  135. if (null !== $reference && Container::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior())
  136. {
  137. return sprintf('@@%s', $id);
  138. }
  139. else
  140. {
  141. return sprintf('@%s', $id);
  142. }
  143. }
  144. protected function getParameterCall($id)
  145. {
  146. return sprintf('%%%s%%', $id);
  147. }
  148. protected function prepareParameters($parameters)
  149. {
  150. $filtered = array();
  151. foreach ($parameters as $key => $value)
  152. {
  153. if (is_array($value))
  154. {
  155. $value = $this->prepareParameters($value);
  156. }
  157. elseif ($value instanceof Reference)
  158. {
  159. $value = '@'.$value;
  160. }
  161. $filtered[$key] = $value;
  162. }
  163. return $this->escape($filtered);
  164. }
  165. protected function escape($arguments)
  166. {
  167. $args = array();
  168. foreach ($arguments as $k => $v)
  169. {
  170. if (is_array($v))
  171. {
  172. $args[$k] = $this->escape($v);
  173. }
  174. elseif (is_string($v))
  175. {
  176. $args[$k] = str_replace('%', '%%', $v);
  177. }
  178. else
  179. {
  180. $args[$k] = $v;
  181. }
  182. }
  183. return $args;
  184. }
  185. }