annotate vendor/symfony/dependency-injection/Dumper/XmlDumper.php @ 8:50b0d041100e

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