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

XmlReferenceDumper.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 10.11 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   
020  /**
021   * Dumps a XML reference configuration for the given configuration/node instance.
022   *
023   * @author Wouter J <waldio.webdesign@gmail.com>
024   */
025  class XmlReferenceDumper
026  {
027      private $reference;
028   
029      public function dump(ConfigurationInterface $configuration, $namespace = null)
030      {
031          return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
032      }
033   
034      public function dumpNode(NodeInterface $node, $namespace = null)
035      {
036          $this->reference = '';
037          $this->writeNode($node, 0, true, $namespace);
038          $ref = $this->reference;
039          $this->reference = null;
040   
041          return $ref;
042      }
043   
044      /**
045       * @param int    $depth
046       * @param bool   $root      If the node is the root node
047       * @param string $namespace The namespace of the node
048       */
049      private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null)
050      {
051          $rootName = ($root ? 'config' : $node->getName());
052          $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
053   
054          // xml remapping
055          if ($node->getParent()) {
056              $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
057                  return $rootName === $mapping[1];
058              });
059   
060              if (\count($remapping)) {
061                  list($singular) = current($remapping);
062                  $rootName = $singular;
063              }
064          }
065          $rootName = str_replace('_', '-', $rootName);
066   
067          $rootAttributes = [];
068          $rootAttributeComments = [];
069          $rootChildren = [];
070          $rootComments = [];
071   
072          if ($node instanceof ArrayNode) {
073              $children = $node->getChildren();
074   
075              // comments about the root node
076              if ($rootInfo = $node->getInfo()) {
077                  $rootComments[] = $rootInfo;
078              }
079   
080              if ($rootNamespace) {
081                  $rootComments[] = 'Namespace: '.$rootNamespace;
082              }
083   
084              // render prototyped nodes
085              if ($node instanceof PrototypedArrayNode) {
086                  $prototype = $node->getPrototype();
087   
088                  $info = 'prototype';
089                  if (null !== $prototype->getInfo()) {
090                      $info .= ': '.$prototype->getInfo();
091                  }
092                  array_unshift($rootComments, $info);
093   
094                  if ($key = $node->getKeyAttribute()) {
095                      $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key;
096                  }
097   
098                  if ($prototype instanceof PrototypedArrayNode) {
099                      $prototype->setName($key);
100                      $children = [$key => $prototype];
101                  } elseif ($prototype instanceof ArrayNode) {
102                      $children = $prototype->getChildren();
103                  } else {
104                      if ($prototype->hasDefaultValue()) {
105                          $prototypeValue = $prototype->getDefaultValue();
106                      } else {
107                          switch (\get_class($prototype)) {
108                              case 'Symfony\Component\Config\Definition\ScalarNode':
109                                  $prototypeValue = 'scalar value';
110                                  break;
111   
112                              case 'Symfony\Component\Config\Definition\FloatNode':
113                              case 'Symfony\Component\Config\Definition\IntegerNode':
114                                  $prototypeValue = 'numeric value';
115                                  break;
116   
117                              case 'Symfony\Component\Config\Definition\BooleanNode':
118                                  $prototypeValue = 'true|false';
119                                  break;
120   
121                              case 'Symfony\Component\Config\Definition\EnumNode':
122                                  $prototypeValue = implode('|', array_map('json_encode', $prototype->getValues()));
123                                  break;
124   
125                              default:
126                                  $prototypeValue = 'value';
127                          }
128                      }
129                  }
130              }
131   
132              // get attributes and elements
133              foreach ($children as $child) {
134                  if (!$child instanceof ArrayNode) {
135                      // get attributes
136   
137                      // metadata
138                      $name = str_replace('_', '-', $child->getName());
139                      $value = '%%%%not_defined%%%%'; // use a string which isn't used in the normal world
140   
141                      // comments
142                      $comments = [];
143                      if ($info = $child->getInfo()) {
144                          $comments[] = $info;
145                      }
146   
147                      if ($example = $child->getExample()) {
148                          $comments[] = 'Example: '.$example;
149                      }
150   
151                      if ($child->isRequired()) {
152                          $comments[] = 'Required';
153                      }
154   
155                      if ($child->isDeprecated()) {
156                          $comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $node->getPath()));
157                      }
158   
159                      if ($child instanceof EnumNode) {
160                          $comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
161                      }
162   
163                      if (\count($comments)) {
164                          $rootAttributeComments[$name] = implode(";\n", $comments);
165                      }
166   
167                      // default values
168                      if ($child->hasDefaultValue()) {
169                          $value = $child->getDefaultValue();
170                      }
171   
172                      // append attribute
173                      $rootAttributes[$name] = $value;
174                  } else {
175                      // get elements
176                      $rootChildren[] = $child;
177                  }
178              }
179          }
180   
181          // render comments
182   
183          // root node comment
184          if (\count($rootComments)) {
185              foreach ($rootComments as $comment) {
186                  $this->writeLine('<!-- '.$comment.' -->', $depth);
187              }
188          }
189   
190          // attribute comments
191          if (\count($rootAttributeComments)) {
192              foreach ($rootAttributeComments as $attrName => $comment) {
193                  $commentDepth = $depth + 4 + \strlen($attrName) + 2;
194                  $commentLines = explode("\n", $comment);
195                  $multiline = (\count($commentLines) > 1);
196                  $comment = implode(\PHP_EOL.str_repeat(' ', $commentDepth), $commentLines);
197   
198                  if ($multiline) {
199                      $this->writeLine('<!--', $depth);
200                      $this->writeLine($attrName.': '.$comment, $depth + 4);
201                      $this->writeLine('-->', $depth);
202                  } else {
203                      $this->writeLine('<!-- '.$attrName.': '.$comment.' -->', $depth);
204                  }
205              }
206          }
207   
208          // render start tag + attributes
209          $rootIsVariablePrototype = isset($prototypeValue);
210          $rootIsEmptyTag = (0 === \count($rootChildren) && !$rootIsVariablePrototype);
211          $rootOpenTag = '<'.$rootName;
212          if (1 >= ($attributesCount = \count($rootAttributes))) {
213              if (1 === $attributesCount) {
214                  $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
215              }
216   
217              $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
218   
219              if ($rootIsVariablePrototype) {
220                  $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
221              }
222   
223              $this->writeLine($rootOpenTag, $depth);
224          } else {
225              $this->writeLine($rootOpenTag, $depth);
226   
227              $i = 1;
228   
229              foreach ($rootAttributes as $attrName => $attrValue) {
230                  $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
231   
232                  $this->writeLine($attr, $depth + 4);
233   
234                  if ($attributesCount === $i++) {
235                      $this->writeLine($rootIsEmptyTag ? '/>' : '>', $depth);
236   
237                      if ($rootIsVariablePrototype) {
238                          $rootOpenTag .= $prototypeValue.'</'.$rootName.'>';
239                      }
240                  }
241              }
242          }
243   
244          // render children tags
245          foreach ($rootChildren as $child) {
246              $this->writeLine('');
247              $this->writeNode($child, $depth + 4);
248          }
249   
250          // render end tag
251          if (!$rootIsEmptyTag && !$rootIsVariablePrototype) {
252              $this->writeLine('');
253   
254              $rootEndTag = '</'.$rootName.'>';
255              $this->writeLine($rootEndTag, $depth);
256          }
257      }
258   
259      /**
260       * Outputs a single config reference line.
261       *
262       * @param string $text
263       * @param int    $indent
264       */
265      private function writeLine($text, $indent = 0)
266      {
267          $indent = \strlen($text) + $indent;
268          $format = '%'.$indent.'s';
269   
270          $this->reference .= sprintf($format, $text).\PHP_EOL;
271      }
272   
273      /**
274       * Renders the string conversion of the value.
275       *
276       * @param mixed $value
277       *
278       * @return string
279       */
280      private function writeValue($value)
281      {
282          if ('%%%%not_defined%%%%' === $value) {
283              return '';
284          }
285   
286          if (\is_string($value) || is_numeric($value)) {
287              return $value;
288          }
289   
290          if (false === $value) {
291              return 'false';
292          }
293   
294          if (true === $value) {
295              return 'true';
296          }
297   
298          if (null === $value) {
299              return 'null';
300          }
301   
302          if (empty($value)) {
303              return '';
304          }
305   
306          if (\is_array($value)) {
307              return implode(',', $value);
308          }
309   
310          return '';
311      }
312  }
313