PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/vendor_full/symfony/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php

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