[ Index ]

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1