XmlDumper.php 10 KB

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