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

YamlReferenceDumper.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 7.63 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\Config\Definition\Dumper;
013   
014  use Symfony\Component\Config\Definition\ArrayNode;
015  use Symfony\Component\Config\Definition\ConfigurationInterface;
016  use Symfony\Component\Config\Definition\EnumNode;
017  use Symfony\Component\Config\Definition\NodeInterface;
018  use Symfony\Component\Config\Definition\PrototypedArrayNode;
019  use Symfony\Component\Config\Definition\ScalarNode;
020  use Symfony\Component\Yaml\Inline;
021   
022  /**
023   * Dumps a Yaml reference configuration for the given configuration/node instance.
024   *
025   * @author Kevin Bond <kevinbond@gmail.com>
026   */
027  class YamlReferenceDumper
028  {
029      private $reference;
030   
031      public function dump(ConfigurationInterface $configuration)
032      {
033          return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
034      }
035   
036      public function dumpAtPath(ConfigurationInterface $configuration, $path)
037      {
038          $rootNode = $node = $configuration->getConfigTreeBuilder()->buildTree();
039   
040          foreach (explode('.', $path) as $step) {
041              if (!$node instanceof ArrayNode) {
042                  throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s".', $rootNode->getName(), $path));
043              }
044   
045              /** @var NodeInterface[] $children */
046              $children = $node instanceof PrototypedArrayNode ? $this->getPrototypeChildren($node) : $node->getChildren();
047   
048              foreach ($children as $child) {
049                  if ($child->getName() === $step) {
050                      $node = $child;
051   
052                      continue 2;
053                  }
054              }
055   
056              throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s".', $rootNode->getName(), $path));
057          }
058   
059          return $this->dumpNode($node);
060      }
061   
062      public function dumpNode(NodeInterface $node)
063      {
064          $this->reference = '';
065          $this->writeNode($node);
066          $ref = $this->reference;
067          $this->reference = null;
068   
069          return $ref;
070      }
071   
072      /**
073       * @param int  $depth
074       * @param bool $prototypedArray
075       */
076      private function writeNode(NodeInterface $node, NodeInterface $parentNode = null, $depth = 0, $prototypedArray = false)
077      {
078          $comments = [];
079          $default = '';
080          $defaultArray = null;
081          $children = null;
082          $example = $node->getExample();
083   
084          // defaults
085          if ($node instanceof ArrayNode) {
086              $children = $node->getChildren();
087   
088              if ($node instanceof PrototypedArrayNode) {
089                  $children = $this->getPrototypeChildren($node);
090              }
091   
092              if (!$children) {
093                  if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
094                      $default = '';
095                  } elseif (!\is_array($example)) {
096                      $default = '[]';
097                  }
098              }
099          } elseif ($node instanceof EnumNode) {
100              $comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
101              $default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
102          } else {
103              $default = '~';
104   
105              if ($node->hasDefaultValue()) {
106                  $default = $node->getDefaultValue();
107   
108                  if (\is_array($default)) {
109                      if (\count($defaultArray = $node->getDefaultValue())) {
110                          $default = '';
111                      } elseif (!\is_array($example)) {
112                          $default = '[]';
113                      }
114                  } else {
115                      $default = Inline::dump($default);
116                  }
117              }
118          }
119   
120          // required?
121          if ($node->isRequired()) {
122              $comments[] = 'Required';
123          }
124   
125          // deprecated?
126          if ($node->isDeprecated()) {
127              $comments[] = sprintf('Deprecated (%s)', $node->getDeprecationMessage($node->getName(), $parentNode ? $parentNode->getPath() : $node->getPath()));
128          }
129   
130          // example
131          if ($example && !\is_array($example)) {
132              $comments[] = 'Example: '.$example;
133          }
134   
135          $default = '' != (string) $default ? ' '.$default : '';
136          $comments = \count($comments) ? '# '.implode(', ', $comments) : '';
137   
138          $key = $prototypedArray ? '-' : $node->getName().':';
139          $text = rtrim(sprintf('%-21s%s %s', $key, $default, $comments), ' ');
140   
141          if ($info = $node->getInfo()) {
142              $this->writeLine('');
143              // indenting multi-line info
144              $info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info);
145              $this->writeLine('# '.$info, $depth * 4);
146          }
147   
148          $this->writeLine($text, $depth * 4);
149   
150          // output defaults
151          if ($defaultArray) {
152              $this->writeLine('');
153   
154              $message = \count($defaultArray) > 1 ? 'Defaults' : 'Default';
155   
156              $this->writeLine('# '.$message.':', $depth * 4 + 4);
157   
158              $this->writeArray($defaultArray, $depth + 1);
159          }
160   
161          if (\is_array($example)) {
162              $this->writeLine('');
163   
164              $message = \count($example) > 1 ? 'Examples' : 'Example';
165   
166              $this->writeLine('# '.$message.':', $depth * 4 + 4);
167   
168              $this->writeArray($example, $depth + 1);
169          }
170   
171          if ($children) {
172              foreach ($children as $childNode) {
173                  $this->writeNode($childNode, $node, $depth + 1, $node instanceof PrototypedArrayNode && !$node->getKeyAttribute());
174              }
175          }
176      }
177   
178      /**
179       * Outputs a single config reference line.
180       *
181       * @param string $text
182       * @param int    $indent
183       */
184      private function writeLine($text, $indent = 0)
185      {
186          $indent = \strlen($text) + $indent;
187          $format = '%'.$indent.'s';
188   
189          $this->reference .= sprintf($format, $text)."\n";
190      }
191   
192      private function writeArray(array $array, $depth)
193      {
194          $isIndexed = array_values($array) === $array;
195   
196          foreach ($array as $key => $value) {
197              if (\is_array($value)) {
198                  $val = '';
199              } else {
200                  $val = $value;
201              }
202   
203              if ($isIndexed) {
204                  $this->writeLine('- '.$val, $depth * 4);
205              } else {
206                  $this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
207              }
208   
209              if (\is_array($value)) {
210                  $this->writeArray($value, $depth + 1);
211              }
212          }
213      }
214   
215      /**
216       * @return array
217       */
218      private function getPrototypeChildren(PrototypedArrayNode $node)
219      {
220          $prototype = $node->getPrototype();
221          $key = $node->getKeyAttribute();
222   
223          // Do not expand prototype if it isn't an array node nor uses attribute as key
224          if (!$key && !$prototype instanceof ArrayNode) {
225              return $node->getChildren();
226          }
227   
228          if ($prototype instanceof ArrayNode) {
229              $keyNode = new ArrayNode($key, $node);
230              $children = $prototype->getChildren();
231   
232              if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) {
233                  $children = $this->getPrototypeChildren($prototype);
234              }
235   
236              // add children
237              foreach ($children as $childNode) {
238                  $keyNode->addChild($childNode);
239              }
240          } else {
241              $keyNode = new ScalarNode($key, $node);
242          }
243   
244          $info = 'Prototype';
245          if (null !== $prototype->getInfo()) {
246              $info .= ': '.$prototype->getInfo();
247          }
248          $keyNode->setInfo($info);
249   
250          return [$key => $keyNode];
251      }
252  }
253