Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

XmlDumper.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 13.08 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\DependencyInjection\Dumper;
 013   
 014  use Symfony\Component\DependencyInjection\ContainerInterface;
 015  use Symfony\Component\DependencyInjection\Parameter;
 016  use Symfony\Component\DependencyInjection\Reference;
 017  use Symfony\Component\DependencyInjection\Definition;
 018  use Symfony\Component\DependencyInjection\Alias;
 019  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
 020  use Symfony\Component\ExpressionLanguage\Expression;
 021   
 022  /**
 023   * XmlDumper dumps a service container as an XML string.
 024   *
 025   * @author Fabien Potencier <fabien@symfony.com>
 026   * @author Martin Hasoň <martin.hason@gmail.com>
 027   */
 028  class XmlDumper extends Dumper
 029  {
 030      /**
 031       * @var \DOMDocument
 032       */
 033      private $document;
 034   
 035      /**
 036       * Dumps the service container as an XML string.
 037       *
 038       * @param array $options An array of options
 039       *
 040       * @return string An xml string representing of the service container
 041       */
 042      public function dump(array $options = array())
 043      {
 044          $this->document = new \DOMDocument('1.0', 'utf-8');
 045          $this->document->formatOutput = true;
 046   
 047          $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
 048          $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
 049          $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
 050   
 051          $this->addParameters($container);
 052          $this->addServices($container);
 053   
 054          $this->document->appendChild($container);
 055          $xml = $this->document->saveXML();
 056          $this->document = null;
 057   
 058          return $xml;
 059      }
 060   
 061      /**
 062       * Adds parameters.
 063       *
 064       * @param \DOMElement $parent
 065       */
 066      private function addParameters(\DOMElement $parent)
 067      {
 068          $data = $this->container->getParameterBag()->all();
 069          if (!$data) {
 070              return;
 071          }
 072   
 073          if ($this->container->isFrozen()) {
 074              $data = $this->escape($data);
 075          }
 076   
 077          $parameters = $this->document->createElement('parameters');
 078          $parent->appendChild($parameters);
 079          $this->convertParameters($data, 'parameter', $parameters);
 080      }
 081   
 082      /**
 083       * Adds method calls.
 084       *
 085       * @param array       $methodcalls
 086       * @param \DOMElement $parent
 087       */
 088      private function addMethodCalls(array $methodcalls, \DOMElement $parent)
 089      {
 090          foreach ($methodcalls as $methodcall) {
 091              $call = $this->document->createElement('call');
 092              $call->setAttribute('method', $methodcall[0]);
 093              if (count($methodcall[1])) {
 094                  $this->convertParameters($methodcall[1], 'argument', $call);
 095              }
 096              $parent->appendChild($call);
 097          }
 098      }
 099   
 100      /**
 101       * Adds a service.
 102       *
 103       * @param Definition  $definition
 104       * @param string      $id
 105       * @param \DOMElement $parent
 106       */
 107      private function addService($definition, $id, \DOMElement $parent)
 108      {
 109          $service = $this->document->createElement('service');
 110          if (null !== $id) {
 111              $service->setAttribute('id', $id);
 112          }
 113          if ($class = $definition->getClass()) {
 114              if ('\\' === substr($class, 0, 1)) {
 115                  $class = substr($class, 1);
 116              }
 117   
 118              $service->setAttribute('class', $class);
 119          }
 120          if ($definition->getFactoryMethod(false)) {
 121              $service->setAttribute('factory-method', $definition->getFactoryMethod(false));
 122          }
 123          if ($definition->getFactoryClass(false)) {
 124              $service->setAttribute('factory-class', $definition->getFactoryClass(false));
 125          }
 126          if ($definition->getFactoryService(false)) {
 127              $service->setAttribute('factory-service', $definition->getFactoryService(false));
 128          }
 129          if (!$definition->isShared()) {
 130              $service->setAttribute('shared', 'false');
 131          }
 132          if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope(false)) {
 133              $service->setAttribute('scope', $scope);
 134          }
 135          if (!$definition->isPublic()) {
 136              $service->setAttribute('public', 'false');
 137          }
 138          if ($definition->isSynthetic()) {
 139              $service->setAttribute('synthetic', 'true');
 140          }
 141          if ($definition->isSynchronized(false)) {
 142              $service->setAttribute('synchronized', 'true');
 143          }
 144          if ($definition->isLazy()) {
 145              $service->setAttribute('lazy', 'true');
 146          }
 147          if (null !== $decorated = $definition->getDecoratedService()) {
 148              list($decorated, $renamedId, $priority) = $decorated;
 149              $service->setAttribute('decorates', $decorated);
 150              if (null !== $renamedId) {
 151                  $service->setAttribute('decoration-inner-name', $renamedId);
 152              }
 153              if (0 !== $priority) {
 154                  $service->setAttribute('decoration-priority', $priority);
 155              }
 156          }
 157   
 158          foreach ($definition->getTags() as $name => $tags) {
 159              foreach ($tags as $attributes) {
 160                  $tag = $this->document->createElement('tag');
 161                  $tag->setAttribute('name', $name);
 162                  foreach ($attributes as $key => $value) {
 163                      $tag->setAttribute($key, $value);
 164                  }
 165                  $service->appendChild($tag);
 166              }
 167          }
 168   
 169          if ($definition->getFile()) {
 170              $file = $this->document->createElement('file');
 171              $file->appendChild($this->document->createTextNode($definition->getFile()));
 172              $service->appendChild($file);
 173          }
 174   
 175          if ($parameters = $definition->getArguments()) {
 176              $this->convertParameters($parameters, 'argument', $service);
 177          }
 178   
 179          if ($parameters = $definition->getProperties()) {
 180              $this->convertParameters($parameters, 'property', $service, 'name');
 181          }
 182   
 183          $this->addMethodCalls($definition->getMethodCalls(), $service);
 184   
 185          if ($callable = $definition->getFactory()) {
 186              $factory = $this->document->createElement('factory');
 187   
 188              if (is_array($callable) && $callable[0] instanceof Definition) {
 189                  $this->addService($callable[0], null, $factory);
 190                  $factory->setAttribute('method', $callable[1]);
 191              } elseif (is_array($callable)) {
 192                  $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
 193                  $factory->setAttribute('method', $callable[1]);
 194              } else {
 195                  $factory->setAttribute('function', $callable);
 196              }
 197              $service->appendChild($factory);
 198          }
 199   
 200          if ($definition->isDeprecated()) {
 201              $deprecated = $this->document->createElement('deprecated');
 202              $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
 203   
 204              $service->appendChild($deprecated);
 205          }
 206   
 207          if ($definition->isAutowired()) {
 208              $service->setAttribute('autowire', 'true');
 209          }
 210   
 211          foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
 212              $autowiringType = $this->document->createElement('autowiring-type');
 213              $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
 214   
 215              $service->appendChild($autowiringType);
 216          }
 217   
 218          if ($callable = $definition->getConfigurator()) {
 219              $configurator = $this->document->createElement('configurator');
 220   
 221              if (is_array($callable) && $callable[0] instanceof Definition) {
 222                  $this->addService($callable[0], null, $configurator);
 223                  $configurator->setAttribute('method', $callable[1]);
 224              } elseif (is_array($callable)) {
 225                  $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
 226                  $configurator->setAttribute('method', $callable[1]);
 227              } else {
 228                  $configurator->setAttribute('function', $callable);
 229              }
 230              $service->appendChild($configurator);
 231          }
 232   
 233          $parent->appendChild($service);
 234      }
 235   
 236      /**
 237       * Adds a service alias.
 238       *
 239       * @param string      $alias
 240       * @param Alias       $id
 241       * @param \DOMElement $parent
 242       */
 243      private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
 244      {
 245          $service = $this->document->createElement('service');
 246          $service->setAttribute('id', $alias);
 247          $service->setAttribute('alias', $id);
 248          if (!$id->isPublic()) {
 249              $service->setAttribute('public', 'false');
 250          }
 251          $parent->appendChild($service);
 252      }
 253   
 254      /**
 255       * Adds services.
 256       *
 257       * @param \DOMElement $parent
 258       */
 259      private function addServices(\DOMElement $parent)
 260      {
 261          $definitions = $this->container->getDefinitions();
 262          if (!$definitions) {
 263              return;
 264          }
 265   
 266          $services = $this->document->createElement('services');
 267          foreach ($definitions as $id => $definition) {
 268              $this->addService($definition, $id, $services);
 269          }
 270   
 271          $aliases = $this->container->getAliases();
 272          foreach ($aliases as $alias => $id) {
 273              while (isset($aliases[(string) $id])) {
 274                  $id = $aliases[(string) $id];
 275              }
 276              $this->addServiceAlias($alias, $id, $services);
 277          }
 278          $parent->appendChild($services);
 279      }
 280   
 281      /**
 282       * Converts parameters.
 283       *
 284       * @param array       $parameters
 285       * @param string      $type
 286       * @param \DOMElement $parent
 287       * @param string      $keyAttribute
 288       */
 289      private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
 290      {
 291          $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
 292          foreach ($parameters as $key => $value) {
 293              $element = $this->document->createElement($type);
 294              if ($withKeys) {
 295                  $element->setAttribute($keyAttribute, $key);
 296              }
 297   
 298              if (is_array($value)) {
 299                  $element->setAttribute('type', 'collection');
 300                  $this->convertParameters($value, $type, $element, 'key');
 301              } elseif ($value instanceof Reference) {
 302                  $element->setAttribute('type', 'service');
 303                  $element->setAttribute('id', (string) $value);
 304                  $behaviour = $value->getInvalidBehavior();
 305                  if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
 306                      $element->setAttribute('on-invalid', 'null');
 307                  } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
 308                      $element->setAttribute('on-invalid', 'ignore');
 309                  }
 310                  if (!$value->isStrict(false)) {
 311                      $element->setAttribute('strict', 'false');
 312                  }
 313              } elseif ($value instanceof Definition) {
 314                  $element->setAttribute('type', 'service');
 315                  $this->addService($value, null, $element);
 316              } elseif ($value instanceof Expression) {
 317                  $element->setAttribute('type', 'expression');
 318                  $text = $this->document->createTextNode(self::phpToXml((string) $value));
 319                  $element->appendChild($text);
 320              } else {
 321                  if (in_array($value, array('null', 'true', 'false'), true)) {
 322                      $element->setAttribute('type', 'string');
 323                  }
 324                  $text = $this->document->createTextNode(self::phpToXml($value));
 325                  $element->appendChild($text);
 326              }
 327              $parent->appendChild($element);
 328          }
 329      }
 330   
 331      /**
 332       * Escapes arguments.
 333       *
 334       * @param array $arguments
 335       *
 336       * @return array
 337       */
 338      private function escape(array $arguments)
 339      {
 340          $args = array();
 341          foreach ($arguments as $k => $v) {
 342              if (is_array($v)) {
 343                  $args[$k] = $this->escape($v);
 344              } elseif (is_string($v)) {
 345                  $args[$k] = str_replace('%', '%%', $v);
 346              } else {
 347                  $args[$k] = $v;
 348              }
 349          }
 350   
 351          return $args;
 352      }
 353   
 354      /**
 355       * Converts php types to xml types.
 356       *
 357       * @param mixed $value Value to convert
 358       *
 359       * @return string
 360       *
 361       * @throws RuntimeException When trying to dump object or resource
 362       */
 363      public static function phpToXml($value)
 364      {
 365          switch (true) {
 366              case null === $value:
 367                  return 'null';
 368              case true === $value:
 369                  return 'true';
 370              case false === $value:
 371                  return 'false';
 372              case $value instanceof Parameter:
 373                  return '%'.$value.'%';
 374              case is_object($value) || is_resource($value):
 375                  throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
 376              default:
 377                  return (string) $value;
 378          }
 379      }
 380  }
 381