XmlDumper.php 13 KB

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