PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

https://github.com/Exercise/symfony
PHP | 301 lines | 193 code | 30 blank | 78 comment | 32 complexity | c37e5ba66064d98d49c9f42842bd315c 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\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Parameter;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  16. /**
  17. * XmlDumper dumps a service container as an XML string.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Martin HasoĊˆ <martin.hason@gmail.com>
  21. *
  22. * @api
  23. */
  24. class XmlDumper extends Dumper
  25. {
  26. /**
  27. * @var \DOMDocument
  28. */
  29. private $document;
  30. /**
  31. * Dumps the service container as an XML string.
  32. *
  33. * @param array $options An array of options
  34. *
  35. * @return string An xml string representing of the service container
  36. *
  37. * @api
  38. */
  39. public function dump(array $options = array())
  40. {
  41. $this->document = new \DOMDocument('1.0', 'utf-8');
  42. $this->document->formatOutput = true;
  43. $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
  44. $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  45. $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
  46. $this->addParameters($container);
  47. $this->addServices($container);
  48. $this->document->appendChild($container);
  49. $xml = $this->document->saveXML();
  50. $this->document = null;
  51. return $xml;
  52. }
  53. /**
  54. * Adds parameters.
  55. *
  56. * @param DOMElement $parent
  57. */
  58. private function addParameters(\DOMElement $parent)
  59. {
  60. $data = $this->container->getParameterBag()->all();
  61. if (!$data) {
  62. return;
  63. }
  64. if ($this->container->isFrozen()) {
  65. $data = $this->escape($data);
  66. }
  67. $parameters = $this->document->createElement('parameters');
  68. $parent->appendChild($parameters);
  69. $this->convertParameters($data, 'parameter', $parameters);
  70. }
  71. /**
  72. * Adds method calls.
  73. *
  74. * @param array $methodcalls
  75. * @param DOMElement $parent
  76. */
  77. private function addMethodCalls(array $methodcalls, \DOMElement $parent)
  78. {
  79. foreach ($methodcalls as $methodcall) {
  80. $call = $this->document->createElement('call');
  81. $call->setAttribute('method', $methodcall[0]);
  82. if (count($methodcall[1])) {
  83. $this->convertParameters($methodcall[1], 'argument', $call);
  84. }
  85. $parent->appendChild($call);
  86. }
  87. }
  88. /**
  89. * Adds a service.
  90. *
  91. * @param Definition $definition
  92. * @param string $id
  93. * @param DOMElement $parent
  94. */
  95. private function addService($definition, $id, \DOMElement $parent)
  96. {
  97. $service = $this->document->createElement('service');
  98. if (null !== $id) {
  99. $service->setAttribute('id', $id);
  100. }
  101. if ($definition->getClass()) {
  102. $service->setAttribute('class', $definition->getClass());
  103. }
  104. if ($definition->getFactoryMethod()) {
  105. $service->setAttribute('factory-method', $definition->getFactoryMethod());
  106. }
  107. if ($definition->getFactoryService()) {
  108. $service->setAttribute('factory-service', $definition->getFactoryService());
  109. }
  110. if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
  111. $service->setAttribute('scope', $scope);
  112. }
  113. if (!$definition->isPublic()) {
  114. $service->setAttribute('public', 'false');
  115. }
  116. foreach ($definition->getTags() as $name => $tags) {
  117. foreach ($tags as $attributes) {
  118. $tag = $this->document->createElement('tag');
  119. $tag->setAttribute('name', $name);
  120. foreach ($attributes as $key => $value) {
  121. $tag->setAttribute($key, $value);
  122. }
  123. $service->appendChild($tag);
  124. }
  125. }
  126. if ($definition->getFile()) {
  127. $file = $this->document->createElement('file');
  128. $file->appendChild($this->document->createTextNode($definition->getFile()));
  129. $service->appendChild($file);
  130. }
  131. if ($parameters = $definition->getArguments()) {
  132. $this->convertParameters($parameters, 'argument', $service);
  133. }
  134. if ($parameters = $definition->getProperties()) {
  135. $this->convertParameters($parameters, 'property', $service, 'name');
  136. }
  137. $this->addMethodCalls($definition->getMethodCalls(), $service);
  138. if ($callable = $definition->getConfigurator()) {
  139. $configurator = $this->document->createElement('configurator');
  140. if (is_array($callable)) {
  141. $configurator->setAttribute((is_object($callable[0]) && $callable[0] instanceof Reference ? 'service' : 'class'), $callable[0]);
  142. $configurator->setAttribute('method', $callable[1]);
  143. } else {
  144. $configurator->setAttribute('function', $callable);
  145. }
  146. $service->appendChild($configurator);
  147. }
  148. $parent->appendChild($service);
  149. }
  150. /**
  151. * Adds a service alias.
  152. *
  153. * @param string $alias
  154. * @param string $id
  155. * @param DOMElement $parent
  156. */
  157. private function addServiceAlias($alias, $id, \DOMElement $parent)
  158. {
  159. $service = $this->document->createElement('service');
  160. $service->setAttribute('id', $alias);
  161. $service->setAttribute('alias', $id);
  162. if (!$id->isPublic()) {
  163. $service->setAttribute('public', 'false');
  164. }
  165. $parent->appendChild($service);
  166. }
  167. /**
  168. * Adds services.
  169. *
  170. * @param DOMElement $parent
  171. */
  172. private function addServices(\DOMElement $parent)
  173. {
  174. $definitions = $this->container->getDefinitions();
  175. if (!$definitions) {
  176. return;
  177. }
  178. $services = $this->document->createElement('services');
  179. foreach ($definitions as $id => $definition) {
  180. $this->addService($definition, $id, $services);
  181. }
  182. foreach ($this->container->getAliases() as $alias => $id) {
  183. $this->addServiceAlias($alias, $id, $services);
  184. }
  185. $parent->appendChild($services);
  186. }
  187. /**
  188. * Converts parameters.
  189. *
  190. * @param array $parameters
  191. * @param string $type
  192. * @param DOMElement $parent
  193. * @param string $keyAttribute
  194. */
  195. private function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
  196. {
  197. $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
  198. foreach ($parameters as $key => $value) {
  199. $element = $this->document->createElement($type);
  200. if ($withKeys) {
  201. $element->setAttribute($keyAttribute, $key);
  202. }
  203. if (is_array($value)) {
  204. $element->setAttribute('type', 'collection');
  205. $this->convertParameters($value, $type, $element, 'key');
  206. } elseif (is_object($value) && $value instanceof Reference) {
  207. $element->setAttribute('type', 'service');
  208. $element->setAttribute('id', (string) $value);
  209. $behaviour = $value->getInvalidBehavior();
  210. if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
  211. $element->setAttribute('on-invalid', 'null');
  212. } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
  213. $element->setAttribute('on-invalid', 'ignore');
  214. }
  215. } elseif (is_object($value) && $value instanceof Definition) {
  216. $element->setAttribute('type', 'service');
  217. $this->addService($value, null, $element);
  218. } else {
  219. if (in_array($value, array('null', 'true', 'false'), true)) {
  220. $element->setAttribute('type', 'string');
  221. }
  222. $text = $this->document->createTextNode(self::phpToXml($value));
  223. $element->appendChild($text);
  224. }
  225. $parent->appendChild($element);
  226. }
  227. }
  228. /**
  229. * Escapes arguments
  230. *
  231. * @param array $arguments
  232. *
  233. * @return array
  234. */
  235. private function escape($arguments)
  236. {
  237. $args = array();
  238. foreach ($arguments as $k => $v) {
  239. if (is_array($v)) {
  240. $args[$k] = $this->escape($v);
  241. } elseif (is_string($v)) {
  242. $args[$k] = str_replace('%', '%%', $v);
  243. } else {
  244. $args[$k] = $v;
  245. }
  246. }
  247. return $args;
  248. }
  249. /**
  250. * Converts php types to xml types.
  251. *
  252. * @param mixed $value Value to convert
  253. */
  254. static public function phpToXml($value)
  255. {
  256. switch (true) {
  257. case null === $value:
  258. return 'null';
  259. case true === $value:
  260. return 'true';
  261. case false === $value:
  262. return 'false';
  263. case is_object($value) && $value instanceof Parameter:
  264. return '%'.$value.'%';
  265. case is_object($value) || is_resource($value):
  266. throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  267. default:
  268. return (string) $value;
  269. }
  270. }
  271. }