Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

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: 02.04.2025, 15:03 - Dateigröße: 13.19 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\Alias;
015  use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
016  use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
017  use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
018  use Symfony\Component\DependencyInjection\ContainerInterface;
019  use Symfony\Component\DependencyInjection\Definition;
020  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
021  use Symfony\Component\DependencyInjection\Parameter;
022  use Symfony\Component\DependencyInjection\Reference;
023  use Symfony\Component\ExpressionLanguage\Expression;
024   
025  /**
026   * XmlDumper dumps a service container as an XML string.
027   *
028   * @author Fabien Potencier <fabien@symfony.com>
029   * @author Martin Hasoň <martin.hason@gmail.com>
030   */
031  class XmlDumper extends Dumper
032  {
033      /**
034       * @var \DOMDocument
035       */
036      private $document;
037   
038      /**
039       * Dumps the service container as an XML string.
040       *
041       * @return string An xml string representing of the service container
042       */
043      public function dump(array $options = [])
044      {
045          $this->document = new \DOMDocument('1.0', 'utf-8');
046          $this->document->formatOutput = true;
047   
048          $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
049          $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
050          $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd');
051   
052          $this->addParameters($container);
053          $this->addServices($container);
054   
055          $this->document->appendChild($container);
056          $xml = $this->document->saveXML();
057          $this->document = null;
058   
059          return $this->container->resolveEnvPlaceholders($xml);
060      }
061   
062      private function addParameters(\DOMElement $parent)
063      {
064          $data = $this->container->getParameterBag()->all();
065          if (!$data) {
066              return;
067          }
068   
069          if ($this->container->isCompiled()) {
070              $data = $this->escape($data);
071          }
072   
073          $parameters = $this->document->createElement('parameters');
074          $parent->appendChild($parameters);
075          $this->convertParameters($data, 'parameter', $parameters);
076      }
077   
078      private function addMethodCalls(array $methodcalls, \DOMElement $parent)
079      {
080          foreach ($methodcalls as $methodcall) {
081              $call = $this->document->createElement('call');
082              $call->setAttribute('method', $methodcall[0]);
083              if (\count($methodcall[1])) {
084                  $this->convertParameters($methodcall[1], 'argument', $call);
085              }
086              $parent->appendChild($call);
087          }
088      }
089   
090      /**
091       * Adds a service.
092       *
093       * @param Definition $definition
094       * @param string     $id
095       */
096      private function addService($definition, $id, \DOMElement $parent)
097      {
098          $service = $this->document->createElement('service');
099          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  }
367