/library/Symfony/Components/sfServiceContainerDumperXml.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 213 lines · 163 code · 27 blank · 23 comment · 17 complexity · a8f3c940ffdfb9f4fc4d2426bd2e7be5 MD5 · raw file

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