Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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:58 - Dateigröße: 10.49 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   
 021  /**
 022   * XmlDumper dumps a service container as an XML string.
 023   *
 024   * @author Fabien Potencier <fabien@symfony.com>
 025   * @author Martin Hasoň <martin.hason@gmail.com>
 026   *
 027   * @api
 028   */
 029  class XmlDumper extends Dumper
 030  {
 031      /**
 032       * @var \DOMDocument
 033       */
 034      private $document;
 035   
 036      /**
 037       * Dumps the service container as an XML string.
 038       *
 039       * @param array $options An array of options
 040       *
 041       * @return string An xml string representing of the service container
 042       *
 043       * @api
 044       */
 045      public function dump(array $options = array())
 046      {
 047          $this->document = new \DOMDocument('1.0', 'utf-8');
 048          $this->document->formatOutput = true;
 049   
 050          $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
 051          $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
 052          $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
 053   
 054          $this->addParameters($container);
 055          $this->addServices($container);
 056   
 057          $this->document->appendChild($container);
 058          $xml = $this->document->saveXML();
 059          $this->document = null;
 060   
 061          return $xml;
 062      }
 063   
 064      /**
 065       * Adds parameters.
 066       *
 067       * @param \DOMElement $parent
 068       */
 069      private function addParameters(\DOMElement $parent)
 070      {
 071          $data = $this->container->getParameterBag()->all();
 072          if (!$data) {
 073              return;
 074          }
 075   
 076          if ($this->container->isFrozen()) {
 077              $data = $this->escape($data);
 078          }
 079   
 080          $parameters = $this->document->createElement('parameters');
 081          $parent->appendChild($parameters);
 082          $this->convertParameters($data, 'parameter', $parameters);
 083      }
 084   
 085      /**
 086       * Adds method calls.
 087       *
 088       * @param array       $methodcalls
 089       * @param \DOMElement $parent
 090       */
 091      private function addMethodCalls(array $methodcalls, \DOMElement $parent)
 092      {
 093          foreach ($methodcalls as $methodcall) {
 094              $call = $this->document->createElement('call');
 095              $call->setAttribute('method', $methodcall[0]);
 096              if (count($methodcall[1])) {
 097                  $this->convertParameters($methodcall[1], 'argument', $call);
 098              }
 099              $parent->appendChild($call);
 100          }
 101      }
 102   
 103      /**
 104       * Adds a service.
 105       *
 106       * @param Definition  $definition
 107       * @param string      $id
 108       * @param \DOMElement $parent
 109       */
 110      private function addService($definition, $id, \DOMElement $parent)
 111      {
 112          $service = $this->document->createElement('service');
 113          if (null !== $id) {
 114              $service->setAttribute('id', $id);
 115          }
 116          if ($definition->getClass()) {
 117              $service->setAttribute('class', $definition->getClass());
 118          }
 119          if ($definition->getFactoryMethod()) {
 120              $service->setAttribute('factory-method', $definition->getFactoryMethod());
 121          }
 122          if ($definition->getFactoryClass()) {
 123              $service->setAttribute('factory-class', $definition->getFactoryClass());
 124          }
 125          if ($definition->getFactoryService()) {
 126              $service->setAttribute('factory-service', $definition->getFactoryService());
 127          }
 128          if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) {
 129              $service->setAttribute('scope', $scope);
 130          }
 131          if (!$definition->isPublic()) {
 132              $service->setAttribute('public', 'false');
 133          }
 134          if ($definition->isSynthetic()) {
 135              $service->setAttribute('synthetic', 'true');
 136          }
 137          if ($definition->isSynchronized()) {
 138              $service->setAttribute('synchronized', 'true');
 139          }
 140          if ($definition->isLazy()) {
 141              $service->setAttribute('lazy', 'true');
 142          }
 143   
 144          foreach ($definition->getTags() as $name => $tags) {
 145              foreach ($tags as $attributes) {
 146                  $tag = $this->document->createElement('tag');
 147                  $tag->setAttribute('name', $name);
 148                  foreach ($attributes as $key => $value) {
 149                      $tag->setAttribute($key, $value);
 150                  }
 151                  $service->appendChild($tag);
 152              }
 153          }
 154   
 155          if ($definition->getFile()) {
 156              $file = $this->document->createElement('file');
 157              $file->appendChild($this->document->createTextNode($definition->getFile()));
 158              $service->appendChild($file);
 159          }
 160   
 161          if ($parameters = $definition->getArguments()) {
 162              $this->convertParameters($parameters, 'argument', $service);
 163          }
 164   
 165          if ($parameters = $definition->getProperties()) {
 166              $this->convertParameters($parameters, 'property', $service, 'name');
 167          }
 168   
 169          $this->addMethodCalls($definition->getMethodCalls(), $service);
 170   
 171          if ($callable = $definition->getConfigurator()) {
 172              $configurator = $this->document->createElement('configurator');
 173              if (is_array($callable)) {
 174                  $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
 175                  $configurator->setAttribute('method', $callable[1]);
 176              } else {
 177                  $configurator->setAttribute('function', $callable);
 178              }
 179              $service->appendChild($configurator);
 180          }
 181   
 182          $parent->appendChild($service);
 183      }
 184   
 185      /**
 186       * Adds a service alias.
 187       *
 188       * @param string      $alias
 189       * @param Alias       $id
 190       * @param \DOMElement $parent
 191       */
 192      private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
 193      {
 194          $service = $this->document->createElement('service');
 195          $service->setAttribute('id', $alias);
 196          $service->setAttribute('alias', $id);
 197          if (!$id->isPublic()) {
 198              $service->setAttribute('public', 'false');
 199          }
 200          $parent->appendChild($service);
 201      }
 202   
 203      /**
 204       * Adds services.
 205       *
 206       * @param \DOMElement $parent
 207       */
 208      private function addServices(\DOMElement $parent)
 209      {
 210          $definitions = $this->container->getDefinitions();
 211          if (!$definitions) {
 212              return;
 213          }
 214   
 215          $services = $this->document->createElement('services');
 216          foreach ($definitions as $id => $definition) {
 217              $this->addService($definition, $id, $services);
 218          }
 219   
 220          $aliases = $this->container->getAliases();
 221          foreach ($aliases as $alias => $id) {
 222              while (isset($aliases[(string) $id])) {
 223                  $id = $aliases[(string) $id];
 224              }
 225              $this->addServiceAlias($alias, $id, $services);
 226          }
 227          $parent->appendChild($services);
 228      }
 229   
 230      /**
 231       * Converts parameters.
 232       *
 233       * @param array       $parameters
 234       * @param string      $type
 235       * @param \DOMElement $parent
 236       * @param string      $keyAttribute
 237       */
 238      private function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
 239      {
 240          $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
 241          foreach ($parameters as $key => $value) {
 242              $element = $this->document->createElement($type);
 243              if ($withKeys) {
 244                  $element->setAttribute($keyAttribute, $key);
 245              }
 246   
 247              if (is_array($value)) {
 248                  $element->setAttribute('type', 'collection');
 249                  $this->convertParameters($value, $type, $element, 'key');
 250              } elseif ($value instanceof Reference) {
 251                  $element->setAttribute('type', 'service');
 252                  $element->setAttribute('id', (string) $value);
 253                  $behaviour = $value->getInvalidBehavior();
 254                  if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
 255                      $element->setAttribute('on-invalid', 'null');
 256                  } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
 257                      $element->setAttribute('on-invalid', 'ignore');
 258                  }
 259                  if (!$value->isStrict()) {
 260                      $element->setAttribute('strict', 'false');
 261                  }
 262              } elseif ($value instanceof Definition) {
 263                  $element->setAttribute('type', 'service');
 264                  $this->addService($value, null, $element);
 265              } else {
 266                  if (in_array($value, array('null', 'true', 'false'), true)) {
 267                      $element->setAttribute('type', 'string');
 268                  }
 269                  $text = $this->document->createTextNode(self::phpToXml($value));
 270                  $element->appendChild($text);
 271              }
 272              $parent->appendChild($element);
 273          }
 274      }
 275   
 276      /**
 277       * Escapes arguments
 278       *
 279       * @param array $arguments
 280       *
 281       * @return array
 282       */
 283      private function escape($arguments)
 284      {
 285          $args = array();
 286          foreach ($arguments as $k => $v) {
 287              if (is_array($v)) {
 288                  $args[$k] = $this->escape($v);
 289              } elseif (is_string($v)) {
 290                  $args[$k] = str_replace('%', '%%', $v);
 291              } else {
 292                  $args[$k] = $v;
 293              }
 294          }
 295   
 296          return $args;
 297      }
 298   
 299      /**
 300       * Converts php types to xml types.
 301       *
 302       * @param mixed $value Value to convert
 303       *
 304       * @return string
 305       *
 306       * @throws RuntimeException When trying to dump object or resource
 307       */
 308      public static function phpToXml($value)
 309      {
 310          switch (true) {
 311              case null === $value:
 312                  return 'null';
 313              case true === $value:
 314                  return 'true';
 315              case false === $value:
 316                  return 'false';
 317              case $value instanceof Parameter:
 318                  return '%'.$value.'%';
 319              case is_object($value) || is_resource($value):
 320                  throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
 321              default:
 322                  return (string) $value;
 323          }
 324      }
 325  }
 326