[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/symfony/config/Definition/Dumper/ -> XmlReferenceDumper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Config\Definition\Dumper;
  13  
  14  use Symfony\Component\Config\Definition\ArrayNode;
  15  use Symfony\Component\Config\Definition\ConfigurationInterface;
  16  use Symfony\Component\Config\Definition\EnumNode;
  17  use Symfony\Component\Config\Definition\NodeInterface;
  18  use Symfony\Component\Config\Definition\PrototypedArrayNode;
  19  
  20  /**
  21   * Dumps a XML reference configuration for the given configuration/node instance.
  22   *
  23   * @author Wouter J <waldio.webdesign@gmail.com>
  24   */
  25  class XmlReferenceDumper
  26  {
  27      private $reference;
  28  
  29      public function dump(ConfigurationInterface $configuration, $namespace = null)
  30      {
  31          return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree(), $namespace);
  32      }
  33  
  34      public function dumpNode(NodeInterface $node, $namespace = null)
  35      {
  36          $this->reference = '';
  37          $this->writeNode($node, 0, true, $namespace);
  38          $ref = $this->reference;
  39          $this->reference = null;
  40  
  41          return $ref;
  42      }
  43  
  44      /**
  45       * @param int    $depth
  46       * @param bool   $root      If the node is the root node
  47       * @param string $namespace The namespace of the node
  48       */
  49      private function writeNode(NodeInterface $node, $depth = 0, $root = false, $namespace = null)
  50      {
  51          $rootName = ($root ? 'config' : $node->getName());
  52          $rootNamespace = ($namespace ?: ($root ? 'http://example.org/schema/dic/'.$node->getName() : null));
  53  
  54          // xml remapping
  55          if ($node->getParent()) {
  56              $remapping = array_filter($node->getParent()->getXmlRemappings(), function ($mapping) use ($rootName) {
  57                  return $rootName === $mapping[1];
  58              });
  59  
  60              if (\count($remapping)) {
  61                  list($singular) = current($remapping);
  62                  $rootName = $singular;
  63              }
  64          }
  65          $rootName = str_replace('_', '-', $rootName);
  66  
  67          $rootAttributes = [];
  68          $rootAttributeComments = [];
  69          $rootChildren = [];
  70          $rootComments = [];
  71  
  72          if ($node instanceof ArrayNode) {
  73              $children = $node->getChildren();
  74  
  75              // comments about the root node
  76              if ($rootInfo = $node->getInfo()) {
  77                  $rootComments[] = $rootInfo;
  78              }
  79  
  80              if ($rootNamespace) {
  81                  $rootComments[] = 'Namespace: '.$rootNamespace;
  82              }
  83  
  84              // render prototyped nodes
  85              if ($node instanceof PrototypedArrayNode) {
  86                  $prototype = $node->getPrototype();
  87  
  88                  $info = 'prototype';
  89                  if (null !== $prototype->getInfo()) {
  90                      $info .= ': '.$prototype->getInfo();
  91                  }
  92                  array_unshift($rootComments, $info);
  93  
  94                  if ($key = $node->getKeyAttribute()) {
  95                      $rootAttributes[$key] = str_replace('-', ' ', $rootName).' '.$key;
  96                  }
  97  
  98                  if ($prototype instanceof PrototypedArrayNode) {
  99                      $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  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1