annotate vendor/symfony/dependency-injection/Dumper/XmlDumper.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 7a779792577d
children 1fec387a4317
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@12 203 if ($definition->isAbstract()) {
Chris@12 204 $service->setAttribute('abstract', 'true');
Chris@12 205 }
Chris@12 206
Chris@0 207 if ($callable = $definition->getConfigurator()) {
Chris@0 208 $configurator = $this->document->createElement('configurator');
Chris@0 209
Chris@0 210 if (is_array($callable) && $callable[0] instanceof Definition) {
Chris@0 211 $this->addService($callable[0], null, $configurator);
Chris@0 212 $configurator->setAttribute('method', $callable[1]);
Chris@0 213 } elseif (is_array($callable)) {
Chris@0 214 $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
Chris@0 215 $configurator->setAttribute('method', $callable[1]);
Chris@0 216 } else {
Chris@0 217 $configurator->setAttribute('function', $callable);
Chris@0 218 }
Chris@0 219 $service->appendChild($configurator);
Chris@0 220 }
Chris@0 221
Chris@0 222 $parent->appendChild($service);
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * Adds a service alias.
Chris@0 227 *
Chris@0 228 * @param string $alias
Chris@0 229 * @param Alias $id
Chris@0 230 * @param \DOMElement $parent
Chris@0 231 */
Chris@0 232 private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
Chris@0 233 {
Chris@0 234 $service = $this->document->createElement('service');
Chris@0 235 $service->setAttribute('id', $alias);
Chris@0 236 $service->setAttribute('alias', $id);
Chris@0 237 if (!$id->isPublic()) {
Chris@0 238 $service->setAttribute('public', 'false');
Chris@0 239 }
Chris@0 240 $parent->appendChild($service);
Chris@0 241 }
Chris@0 242
Chris@0 243 /**
Chris@0 244 * Adds services.
Chris@0 245 *
Chris@0 246 * @param \DOMElement $parent
Chris@0 247 */
Chris@0 248 private function addServices(\DOMElement $parent)
Chris@0 249 {
Chris@0 250 $definitions = $this->container->getDefinitions();
Chris@0 251 if (!$definitions) {
Chris@0 252 return;
Chris@0 253 }
Chris@0 254
Chris@0 255 $services = $this->document->createElement('services');
Chris@0 256 foreach ($definitions as $id => $definition) {
Chris@0 257 $this->addService($definition, $id, $services);
Chris@0 258 }
Chris@0 259
Chris@0 260 $aliases = $this->container->getAliases();
Chris@0 261 foreach ($aliases as $alias => $id) {
Chris@0 262 while (isset($aliases[(string) $id])) {
Chris@0 263 $id = $aliases[(string) $id];
Chris@0 264 }
Chris@0 265 $this->addServiceAlias($alias, $id, $services);
Chris@0 266 }
Chris@0 267 $parent->appendChild($services);
Chris@0 268 }
Chris@0 269
Chris@0 270 /**
Chris@0 271 * Converts parameters.
Chris@0 272 *
Chris@0 273 * @param array $parameters
Chris@0 274 * @param string $type
Chris@0 275 * @param \DOMElement $parent
Chris@0 276 * @param string $keyAttribute
Chris@0 277 */
Chris@0 278 private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
Chris@0 279 {
Chris@0 280 $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
Chris@0 281 foreach ($parameters as $key => $value) {
Chris@0 282 $element = $this->document->createElement($type);
Chris@0 283 if ($withKeys) {
Chris@0 284 $element->setAttribute($keyAttribute, $key);
Chris@0 285 }
Chris@0 286
Chris@0 287 if (is_array($value)) {
Chris@0 288 $element->setAttribute('type', 'collection');
Chris@0 289 $this->convertParameters($value, $type, $element, 'key');
Chris@0 290 } elseif ($value instanceof Reference) {
Chris@0 291 $element->setAttribute('type', 'service');
Chris@0 292 $element->setAttribute('id', (string) $value);
Chris@0 293 $behaviour = $value->getInvalidBehavior();
Chris@0 294 if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
Chris@0 295 $element->setAttribute('on-invalid', 'null');
Chris@0 296 } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
Chris@0 297 $element->setAttribute('on-invalid', 'ignore');
Chris@0 298 }
Chris@0 299 } elseif ($value instanceof Definition) {
Chris@0 300 $element->setAttribute('type', 'service');
Chris@0 301 $this->addService($value, null, $element);
Chris@0 302 } elseif ($value instanceof Expression) {
Chris@0 303 $element->setAttribute('type', 'expression');
Chris@0 304 $text = $this->document->createTextNode(self::phpToXml((string) $value));
Chris@0 305 $element->appendChild($text);
Chris@0 306 } else {
Chris@0 307 if (in_array($value, array('null', 'true', 'false'), true)) {
Chris@0 308 $element->setAttribute('type', 'string');
Chris@0 309 }
Chris@0 310 $text = $this->document->createTextNode(self::phpToXml($value));
Chris@0 311 $element->appendChild($text);
Chris@0 312 }
Chris@0 313 $parent->appendChild($element);
Chris@0 314 }
Chris@0 315 }
Chris@0 316
Chris@0 317 /**
Chris@0 318 * Escapes arguments.
Chris@0 319 *
Chris@0 320 * @param array $arguments
Chris@0 321 *
Chris@0 322 * @return array
Chris@0 323 */
Chris@0 324 private function escape(array $arguments)
Chris@0 325 {
Chris@0 326 $args = array();
Chris@0 327 foreach ($arguments as $k => $v) {
Chris@0 328 if (is_array($v)) {
Chris@0 329 $args[$k] = $this->escape($v);
Chris@0 330 } elseif (is_string($v)) {
Chris@0 331 $args[$k] = str_replace('%', '%%', $v);
Chris@0 332 } else {
Chris@0 333 $args[$k] = $v;
Chris@0 334 }
Chris@0 335 }
Chris@0 336
Chris@0 337 return $args;
Chris@0 338 }
Chris@0 339
Chris@0 340 /**
Chris@0 341 * Converts php types to xml types.
Chris@0 342 *
Chris@0 343 * @param mixed $value Value to convert
Chris@0 344 *
Chris@0 345 * @return string
Chris@0 346 *
Chris@0 347 * @throws RuntimeException When trying to dump object or resource
Chris@0 348 */
Chris@0 349 public static function phpToXml($value)
Chris@0 350 {
Chris@0 351 switch (true) {
Chris@0 352 case null === $value:
Chris@0 353 return 'null';
Chris@0 354 case true === $value:
Chris@0 355 return 'true';
Chris@0 356 case false === $value:
Chris@0 357 return 'false';
Chris@0 358 case $value instanceof Parameter:
Chris@0 359 return '%'.$value.'%';
Chris@0 360 case is_object($value) || is_resource($value):
Chris@0 361 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
Chris@0 362 default:
Chris@0 363 return (string) $value;
Chris@0 364 }
Chris@0 365 }
Chris@0 366 }