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

XmlReferenceDumper.php

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