XmlDumper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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\Argument\IteratorArgument;
  12. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Parameter;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Alias;
  18. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  19. use Symfony\Component\ExpressionLanguage\Expression;
  20. /**
  21. * XmlDumper dumps a service container as an XML string.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. * @author Martin Hasoň <martin.hason@gmail.com>
  25. */
  26. class XmlDumper extends Dumper
  27. {
  28. /**
  29. * @var \DOMDocument
  30. */
  31. private $document;
  32. /**
  33. * Dumps the service container as an XML string.
  34. *
  35. * @return string An xml string representing of the service container
  36. */
  37. public function dump(array $options = array())
  38. {
  39. $this->document = new \DOMDocument('1.0', 'utf-8');
  40. $this->document->formatOutput = true;
  41. $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
  42. $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  43. $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
  44. $this->addParameters($container);
  45. $this->addServices($container);
  46. $this->document->appendChild($container);
  47. $xml = $this->document->saveXML();
  48. $this->document = null;
  49. return $this->container->resolveEnvPlaceholders($xml);
  50. }
  51. private function addParameters(\DOMElement $parent)
  52. {
  53. $data = $this->container->getParameterBag()->all();
  54. if (!$data) {
  55. return;
  56. }
  57. if ($this->container->isCompiled()) {
  58. $data = $this->escape($data);
  59. }
  60. $parameters = $this->document->createElement('parameters');
  61. $parent->appendChild($parameters);
  62. $this->convertParameters($data, 'parameter', $parameters);
  63. }
  64. private function addMethodCalls(array $methodcalls, \DOMElement $parent)
  65. {
  66. foreach ($methodcalls as $methodcall) {
  67. $call = $this->document->createElement('call');
  68. $call->setAttribute('method', $methodcall[0]);
  69. if (count($methodcall[1])) {
  70. $this->convertParameters($methodcall[1], 'argument', $call);
  71. }
  72. $parent->appendChild($call);
  73. }
  74. }
  75. /**
  76. * Adds a service.
  77. *
  78. * @param Definition $definition
  79. * @param string $id
  80. * @param \DOMElement $parent
  81. */
  82. private function addService($definition, $id, \DOMElement $parent)
  83. {
  84. $service = $this->document->createElement('service');
  85. if (null !== $id) {
  86. $service->setAttribute('id', $id);
  87. }
  88. if ($class = $definition->getClass()) {
  89. if ('\\' === substr($class, 0, 1)) {
  90. $class = substr($class, 1);
  91. }
  92. $service->setAttribute('class', $class);
  93. }
  94. if (!$definition->isShared()) {
  95. $service->setAttribute('shared', 'false');
  96. }
  97. if (!$definition->isPublic()) {
  98. $service->setAttribute('public', 'false');
  99. }
  100. if ($definition->isSynthetic()) {
  101. $service->setAttribute('synthetic', 'true');
  102. }
  103. if ($definition->isLazy()) {
  104. $service->setAttribute('lazy', 'true');
  105. }
  106. if (null !== $decorated = $definition->getDecoratedService()) {
  107. list($decorated, $renamedId, $priority) = $decorated;
  108. $service->setAttribute('decorates', $decorated);
  109. if (null !== $renamedId) {
  110. $service->setAttribute('decoration-inner-name', $renamedId);
  111. }
  112. if (0 !== $priority) {
  113. $service->setAttribute('decoration-priority', $priority);
  114. }
  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->getFactory()) {
  139. $factory = $this->document->createElement('factory');
  140. if (is_array($callable) && $callable[0] instanceof Definition) {
  141. $this->addService($callable[0], null, $factory);
  142. $factory->setAttribute('method', $callable[1]);
  143. } elseif (is_array($callable)) {
  144. if (null !== $callable[0]) {
  145. $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
  146. }
  147. $factory->setAttribute('method', $callable[1]);
  148. } else {
  149. $factory->setAttribute('function', $callable);
  150. }
  151. $service->appendChild($factory);
  152. }
  153. if ($definition->isDeprecated()) {
  154. $deprecated = $this->document->createElement('deprecated');
  155. $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
  156. $service->appendChild($deprecated);
  157. }
  158. if ($definition->isAutowired()) {
  159. $service->setAttribute('autowire', 'true');
  160. }
  161. foreach ($definition->getAutowiringTypes(false) as $autowiringTypeValue) {
  162. $autowiringType = $this->document->createElement('autowiring-type');
  163. $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
  164. $service->appendChild($autowiringType);
  165. }
  166. if ($definition->isAutoconfigured()) {
  167. $service->setAttribute('autoconfigure', 'true');
  168. }
  169. if ($definition->isAbstract()) {
  170. $service->setAttribute('abstract', 'true');
  171. }
  172. if ($callable = $definition->getConfigurator()) {
  173. $configurator = $this->document->createElement('configurator');
  174. if (is_array($callable) && $callable[0] instanceof Definition) {
  175. $this->addService($callable[0], null, $configurator);
  176. $configurator->setAttribute('method', $callable[1]);
  177. } elseif (is_array($callable)) {
  178. $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
  179. $configurator->setAttribute('method', $callable[1]);
  180. } else {
  181. $configurator->setAttribute('function', $callable);
  182. }
  183. $service->appendChild($configurator);
  184. }
  185. $parent->appendChild($service);
  186. }
  187. /**
  188. * Adds a service alias.
  189. *
  190. * @param string $alias
  191. * @param Alias $id
  192. * @param \DOMElement $parent
  193. */
  194. private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
  195. {
  196. $service = $this->document->createElement('service');
  197. $service->setAttribute('id', $alias);
  198. $service->setAttribute('alias', $id);
  199. if (!$id->isPublic()) {
  200. $service->setAttribute('public', 'false');
  201. }
  202. $parent->appendChild($service);
  203. }
  204. private function addServices(\DOMElement $parent)
  205. {
  206. $definitions = $this->container->getDefinitions();
  207. if (!$definitions) {
  208. return;
  209. }
  210. $services = $this->document->createElement('services');
  211. foreach ($definitions as $id => $definition) {
  212. $this->addService($definition, $id, $services);
  213. }
  214. $aliases = $this->container->getAliases();
  215. foreach ($aliases as $alias => $id) {
  216. while (isset($aliases[(string) $id])) {
  217. $id = $aliases[(string) $id];
  218. }
  219. $this->addServiceAlias($alias, $id, $services);
  220. }
  221. $parent->appendChild($services);
  222. }
  223. /**
  224. * Converts parameters.
  225. *
  226. * @param array $parameters
  227. * @param string $type
  228. * @param \DOMElement $parent
  229. * @param string $keyAttribute
  230. */
  231. private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
  232. {
  233. $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
  234. foreach ($parameters as $key => $value) {
  235. $element = $this->document->createElement($type);
  236. if ($withKeys) {
  237. $element->setAttribute($keyAttribute, $key);
  238. }
  239. if ($value instanceof ServiceClosureArgument) {
  240. $value = $value->getValues()[0];
  241. }
  242. if (is_array($value)) {
  243. $element->setAttribute('type', 'collection');
  244. $this->convertParameters($value, $type, $element, 'key');
  245. } elseif ($value instanceof IteratorArgument) {
  246. $element->setAttribute('type', 'iterator');
  247. $this->convertParameters($value->getValues(), $type, $element, 'key');
  248. } elseif ($value instanceof Reference) {
  249. $element->setAttribute('type', 'service');
  250. $element->setAttribute('id', (string) $value);
  251. $behaviour = $value->getInvalidBehavior();
  252. if (ContainerInterface::NULL_ON_INVALID_REFERENCE == $behaviour) {
  253. $element->setAttribute('on-invalid', 'null');
  254. } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE == $behaviour) {
  255. $element->setAttribute('on-invalid', 'ignore');
  256. }
  257. } elseif ($value instanceof Definition) {
  258. $element->setAttribute('type', 'service');
  259. $this->addService($value, null, $element);
  260. } elseif ($value instanceof Expression) {
  261. $element->setAttribute('type', 'expression');
  262. $text = $this->document->createTextNode(self::phpToXml((string) $value));
  263. $element->appendChild($text);
  264. } else {
  265. if (in_array($value, array('null', 'true', 'false'), true)) {
  266. $element->setAttribute('type', 'string');
  267. }
  268. $text = $this->document->createTextNode(self::phpToXml($value));
  269. $element->appendChild($text);
  270. }
  271. $parent->appendChild($element);
  272. }
  273. }
  274. /**
  275. * Escapes arguments.
  276. *
  277. * @return array
  278. */
  279. private function escape(array $arguments)
  280. {
  281. $args = array();
  282. foreach ($arguments as $k => $v) {
  283. if (is_array($v)) {
  284. $args[$k] = $this->escape($v);
  285. } elseif (is_string($v)) {
  286. $args[$k] = str_replace('%', '%%', $v);
  287. } else {
  288. $args[$k] = $v;
  289. }
  290. }
  291. return $args;
  292. }
  293. /**
  294. * Converts php types to xml types.
  295. *
  296. * @param mixed $value Value to convert
  297. *
  298. * @return string
  299. *
  300. * @throws RuntimeException When trying to dump object or resource
  301. */
  302. public static function phpToXml($value)
  303. {
  304. switch (true) {
  305. case null === $value:
  306. return 'null';
  307. case true === $value:
  308. return 'true';
  309. case false === $value:
  310. return 'false';
  311. case $value instanceof Parameter:
  312. return '%'.$value.'%';
  313. case is_object($value) || is_resource($value):
  314. throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  315. default:
  316. return (string) $value;
  317. }
  318. }
  319. }