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

YamlDumper.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 11.41 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\ArgumentInterface;
016  use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
017  use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
018  use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
019  use Symfony\Component\DependencyInjection\ContainerInterface;
020  use Symfony\Component\DependencyInjection\Definition;
021  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
022  use Symfony\Component\DependencyInjection\Parameter;
023  use Symfony\Component\DependencyInjection\Reference;
024  use Symfony\Component\ExpressionLanguage\Expression;
025  use Symfony\Component\Yaml\Dumper as YmlDumper;
026  use Symfony\Component\Yaml\Parser;
027  use Symfony\Component\Yaml\Tag\TaggedValue;
028  use Symfony\Component\Yaml\Yaml;
029   
030  /**
031   * YamlDumper dumps a service container as a YAML string.
032   *
033   * @author Fabien Potencier <fabien@symfony.com>
034   */
035  class YamlDumper extends Dumper
036  {
037      private $dumper;
038   
039      /**
040       * Dumps the service container as an YAML string.
041       *
042       * @return string A YAML string representing of the service container
043       */
044      public function dump(array $options = [])
045      {
046          if (!class_exists('Symfony\Component\Yaml\Dumper')) {
047              throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.');
048          }
049   
050          if (null === $this->dumper) {
051              $this->dumper = new YmlDumper();
052          }
053   
054          return $this->container->resolveEnvPlaceholders($this->addParameters()."\n".$this->addServices());
055      }
056   
057      /**
058       * Adds a service.
059       *
060       * @param string $id
061       *
062       * @return string
063       */
064      private function addService($id, Definition $definition)
065      {
066          $code = "    $id:\n";
067          if ($class = $definition->getClass()) {
068              if ('\\' === substr($class, 0, 1)) {
069                  $class = substr($class, 1);
070              }
071   
072              $code .= sprintf("        class: %s\n", $this->dumper->dump($class));
073          }
074   
075          if (!$definition->isPrivate()) {
076              $code .= sprintf("        public: %s\n", $definition->isPublic() ? 'true' : 'false');
077          }
078   
079          $tagsCode = '';
080          foreach ($definition->getTags() as $name => $tags) {
081              foreach ($tags as $attributes) {
082                  $att = [];
083                  foreach ($attributes as $key => $value) {
084                      $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value));
085                  }
086                  $att = $att ? ', '.implode(', ', $att) : '';
087   
088                  $tagsCode .= sprintf("            - { name: %s%s }\n", $this->dumper->dump($name), $att);
089              }
090          }
091          if ($tagsCode) {
092              $code .= "        tags:\n".$tagsCode;
093          }
094   
095          if ($definition->getFile()) {
096              $code .= sprintf("        file: %s\n", $this->dumper->dump($definition->getFile()));
097          }
098   
099          if ($definition->isSynthetic()) {
100              $code .= "        synthetic: true\n";
101          }
102   
103          if ($definition->isDeprecated()) {
104              $code .= sprintf("        deprecated: %s\n", $this->dumper->dump($definition->getDeprecationMessage('%service_id%')));
105          }
106   
107          if ($definition->isAutowired()) {
108              $code .= "        autowire: true\n";
109          }
110   
111          $autowiringTypesCode = '';
112          foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
113              $autowiringTypesCode .= sprintf("            - %s\n", $this->dumper->dump($autowiringType));
114          }
115          if ($autowiringTypesCode) {
116              $code .= sprintf("        autowiring_types:\n%s", $autowiringTypesCode);
117          }
118   
119          if ($definition->isAutoconfigured()) {
120              $code .= "        autoconfigure: true\n";
121          }
122   
123          if ($definition->isAbstract()) {
124              $code .= "        abstract: true\n";
125          }
126   
127          if ($definition->isLazy()) {
128              $code .= "        lazy: true\n";
129          }
130   
131          if ($definition->getArguments()) {
132              $code .= sprintf("        arguments: %s\n", $this->dumper->dump($this->dumpValue($definition->getArguments()), 0));
133          }
134   
135          if ($definition->getProperties()) {
136              $code .= sprintf("        properties: %s\n", $this->dumper->dump($this->dumpValue($definition->getProperties()), 0));
137          }
138   
139          if ($definition->getMethodCalls()) {
140              $code .= sprintf("        calls:\n%s\n", $this->dumper->dump($this->dumpValue($definition->getMethodCalls()), 1, 12));
141          }
142   
143          if (!$definition->isShared()) {
144              $code .= "        shared: false\n";
145          }
146   
147          if (null !== $decorated = $definition->getDecoratedService()) {
148              list($decorated, $renamedId, $priority) = $decorated;
149              $code .= sprintf("        decorates: %s\n", $decorated);
150              if (null !== $renamedId) {
151                  $code .= sprintf("        decoration_inner_name: %s\n", $renamedId);
152              }
153              if (0 !== $priority) {
154                  $code .= sprintf("        decoration_priority: %s\n", $priority);
155              }
156          }
157   
158          if ($callable = $definition->getFactory()) {
159              $code .= sprintf("        factory: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
160          }
161   
162          if ($callable = $definition->getConfigurator()) {
163              $code .= sprintf("        configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
164          }
165   
166          return $code;
167      }
168   
169      /**
170       * Adds a service alias.
171       *
172       * @param string $alias
173       *
174       * @return string
175       */
176      private function addServiceAlias($alias, Alias $id)
177      {
178          if ($id->isPrivate()) {
179              return sprintf("    %s: '@%s'\n", $alias, $id);
180          }
181   
182          return sprintf("    %s:\n        alias: %s\n        public: %s\n", $alias, $id, $id->isPublic() ? 'true' : 'false');
183      }
184   
185      /**
186       * Adds services.
187       *
188       * @return string
189       */
190      private function addServices()
191      {
192          if (!$this->container->getDefinitions()) {
193              return '';
194          }
195   
196          $code = "services:\n";
197          foreach ($this->container->getDefinitions() as $id => $definition) {
198              $code .= $this->addService($id, $definition);
199          }
200   
201          $aliases = $this->container->getAliases();
202          foreach ($aliases as $alias => $id) {
203              while (isset($aliases[(string) $id])) {
204                  $id = $aliases[(string) $id];
205              }
206              $code .= $this->addServiceAlias($alias, $id);
207          }
208   
209          return $code;
210      }
211   
212      /**
213       * Adds parameters.
214       *
215       * @return string
216       */
217      private function addParameters()
218      {
219          if (!$this->container->getParameterBag()->all()) {
220              return '';
221          }
222   
223          $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isCompiled());
224   
225          return $this->dumper->dump(['parameters' => $parameters], 2);
226      }
227   
228      /**
229       * Dumps callable to YAML format.
230       *
231       * @param mixed $callable
232       */
233      private function dumpCallable($callable)
234      {
235          if (\is_array($callable)) {
236              if ($callable[0] instanceof Reference) {
237                  $callable = [$this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]];
238              } else {
239                  $callable = [$callable[0], $callable[1]];
240              }
241          }
242   
243          return $callable;
244      }
245   
246      /**
247       * Dumps the value to YAML format.
248       *
249       * @param mixed $value
250       *
251       * @return mixed
252       *
253       * @throws RuntimeException When trying to dump object or resource
254       */
255      private function dumpValue($value)
256      {
257          if ($value instanceof ServiceClosureArgument) {
258              $value = $value->getValues()[0];
259          }
260          if ($value instanceof ArgumentInterface) {
261              if ($value instanceof TaggedIteratorArgument) {
262                  return new TaggedValue('tagged', $value->getTag());
263              }
264              if ($value instanceof IteratorArgument) {
265                  $tag = 'iterator';
266              } else {
267                  throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', \get_class($value)));
268              }
269   
270              return new TaggedValue($tag, $this->dumpValue($value->getValues()));
271          }
272   
273          if (\is_array($value)) {
274              $code = [];
275              foreach ($value as $k => $v) {
276                  $code[$k] = $this->dumpValue($v);
277              }
278   
279              return $code;
280          } elseif ($value instanceof Reference) {
281              return $this->getServiceCall((string) $value, $value);
282          } elseif ($value instanceof Parameter) {
283              return $this->getParameterCall((string) $value);
284          } elseif ($value instanceof Expression) {
285              return $this->getExpressionCall((string) $value);
286          } elseif ($value instanceof Definition) {
287              return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
288          } elseif (\is_object($value) || \is_resource($value)) {
289              throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
290          }
291   
292          return $value;
293      }
294   
295      /**
296       * Gets the service call.
297       *
298       * @param string    $id
299       * @param Reference $reference
300       *
301       * @return string
302       */
303      private function getServiceCall($id, Reference $reference = null)
304      {
305          if (null !== $reference) {
306              switch ($reference->getInvalidBehavior()) {
307                  case ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE: break;
308                  case ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE: return sprintf('@!%s', $id);
309                  default: return sprintf('@?%s', $id);
310              }
311          }
312   
313          return sprintf('@%s', $id);
314      }
315   
316      /**
317       * Gets parameter call.
318       *
319       * @param string $id
320       *
321       * @return string
322       */
323      private function getParameterCall($id)
324      {
325          return sprintf('%%%s%%', $id);
326      }
327   
328      private function getExpressionCall($expression)
329      {
330          return sprintf('@=%s', $expression);
331      }
332   
333      /**
334       * Prepares parameters.
335       *
336       * @param bool $escape
337       *
338       * @return array
339       */
340      private function prepareParameters(array $parameters, $escape = true)
341      {
342          $filtered = [];
343          foreach ($parameters as $key => $value) {
344              if (\is_array($value)) {
345                  $value = $this->prepareParameters($value, $escape);
346              } elseif ($value instanceof Reference || \is_string($value) && 0 === strpos($value, '@')) {
347                  $value = '@'.$value;
348              }
349   
350              $filtered[$key] = $value;
351          }
352   
353          return $escape ? $this->escape($filtered) : $filtered;
354      }
355   
356      /**
357       * Escapes arguments.
358       *
359       * @return array
360       */
361      private function escape(array $arguments)
362      {
363          $args = [];
364          foreach ($arguments as $k => $v) {
365              if (\is_array($v)) {
366                  $args[$k] = $this->escape($v);
367              } elseif (\is_string($v)) {
368                  $args[$k] = str_replace('%', '%%', $v);
369              } else {
370                  $args[$k] = $v;
371              }
372          }
373   
374          return $args;
375      }
376  }
377