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

YamlDumper.php

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