Drupal investigation

XmlDumper.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 ($callable = $definition->getConfigurator()) {
  191. $configurator = $this->document->createElement('configurator');
  192. if (is_array($callable) && $callable[0] instanceof Definition) {
  193. $this->addService($callable[0], null, $configurator);
  194. $configurator->setAttribute('method', $callable[1]);
  195. } elseif (is_array($callable)) {
  196. $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
  197. $configurator->setAttribute('method', $callable[1]);
  198. } else {
  199. $configurator->setAttribute('function', $callable);
  200. }
  201. $service->appendChild($configurator);
  202. }
  203. $parent->appendChild($service);
  204. }
  205. /**
  206. * Adds a service alias.
  207. *
  208. * @param string $alias
  209. * @param Alias $id
  210. * @param \DOMElement $parent
  211. */
  212. private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
  213. {
  214. $service = $this->document->createElement('service');
  215. $service->setAttribute('id', $alias);
  216. $service->setAttribute('alias', $id);
  217. if (!$id->isPublic()) {
  218. $service->setAttribute('public', 'false');
  219. }
  220. $parent->appendChild($service);
  221. }
  222. /**
  223. * Adds services.
  224. *
  225. * @param \DOMElement $parent
  226. */
  227. private function addServices(\DOMElement $parent)
  228. {
  229. $definitions = $this->container->getDefinitions();
  230. if (!$definitions) {
  231. return;
  232. }
  233. $services = $this->document->createElement('services');
  234. foreach ($definitions as $id => $definition) {
  235. $this->addService($definition, $id, $services);
  236. }
  237. $aliases = $this->container->getAliases();
  238. foreach ($aliases as $alias => $id) {
  239. while (isset($aliases[(string) $id])) {
  240. $id = $aliases[(string) $id];
  241. }
  242. $this->addServiceAlias($alias, $id, $services);
  243. }
  244. $parent->appendChild($services);
  245. }
  246. /**
  247. * Converts parameters.
  248. *
  249. * @param array $parameters
  250. * @param string $type
  251. * @param \DOMElement $parent
  252. * @param string $keyAttribute
  253. */
  254. private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
  255. {
  256. $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
  257. foreach ($parameters as $key => $value) {
  258. $element = $this->document->createElement($type);
  259. if ($withKeys) {
  260. $element->setAttribute($keyAttribute, $key);
  261. }
  262. if (is_array($value)) {
  263. $element->setAttribute('type', 'collection');
  264. $this->convertParameters($value, $type, $element, 'key');
  265. } elseif ($value instanceof Reference) {
  266. $element->setAttribute('type', 'service');
  267. $element->setAttribute('id', (string) $value);
  268. $behaviour = $value->getInvalidBehavior();
  269. if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
  270. $element->setAttribute('on-invalid', 'null');
  271. } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
  272. $element->setAttribute('on-invalid', 'ignore');
  273. }
  274. if (!$value->isStrict(false)) {
  275. $element->setAttribute('strict', 'false');
  276. }
  277. } elseif ($value instanceof Definition) {
  278. $element->setAttribute('type', 'service');
  279. $this->addService($value, null, $element);
  280. } elseif ($value instanceof Expression) {
  281. $element->setAttribute('type', 'expression');
  282. $text = $this->document->createTextNode(self::phpToXml((string) $value));
  283. $element->appendChild($text);
  284. } else {
  285. if (in_array($value, array('null', 'true', 'false'), true)) {
  286. $element->setAttribute('type', 'string');
  287. }
  288. $text = $this->document->createTextNode(self::phpToXml($value));
  289. $element->appendChild($text);
  290. }
  291. $parent->appendChild($element);
  292. }
  293. }
  294. /**
  295. * Escapes arguments.
  296. *
  297. * @param array $arguments
  298. *
  299. * @return array
  300. */
  301. private function escape(array $arguments)
  302. {
  303. $args = array();
  304. foreach ($arguments as $k => $v) {
  305. if (is_array($v)) {
  306. $args[$k] = $this->escape($v);
  307. } elseif (is_string($v)) {
  308. $args[$k] = str_replace('%', '%%', $v);
  309. } else {
  310. $args[$k] = $v;
  311. }
  312. }
  313. return $args;
  314. }
  315. /**
  316. * Converts php types to xml types.
  317. *
  318. * @param mixed $value Value to convert
  319. *
  320. * @return string
  321. *
  322. * @throws RuntimeException When trying to dump object or resource
  323. */
  324. public static function phpToXml($value)
  325. {
  326. switch (true) {
  327. case null === $value:
  328. return 'null';
  329. case true === $value:
  330. return 'true';
  331. case false === $value:
  332. return 'false';
  333. case $value instanceof Parameter:
  334. return '%'.$value.'%';
  335. case is_object($value) || is_resource($value):
  336. throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
  337. default:
  338. return (string) $value;
  339. }
  340. }
  341. }