[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Dumper/ -> XmlDumper.php (source)

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


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1