XmlDumper.php 13 KB

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