[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/symfony/config/Definition/Dumper/ -> YamlReferenceDumper.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  use Symfony\Component\Config\Definition\ScalarNode;
  20  use Symfony\Component\Yaml\Inline;
  21  
  22  /**
  23   * Dumps a Yaml reference configuration for the given configuration/node instance.
  24   *
  25   * @author Kevin Bond <kevinbond@gmail.com>
  26   */
  27  class YamlReferenceDumper
  28  {
  29      private $reference;
  30  
  31      public function dump(ConfigurationInterface $configuration)
  32      {
  33          return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
  34      }
  35  
  36      public function dumpAtPath(ConfigurationInterface $configuration, $path)
  37      {
  38          $rootNode = $node = $configuration->getConfigTreeBuilder()->buildTree();
  39  
  40          foreach (explode('.', $path) as $step) {
  41              if (!$node instanceof ArrayNode) {
  42                  throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s".', $rootNode->getName(), $path));
  43              }
  44  
  45              /** @var NodeInterface[] $children */
  46              $children = $node instanceof PrototypedArrayNode ? $this->getPrototypeChildren($node) : $node->getChildren();
  47  
  48              foreach ($children as $child) {
  49                  if ($child->getName() === $step) {
  50                      $node = $child;
  51  
  52                      continue 2;
  53                  }
  54              }
  55  
  56              throw new \UnexpectedValueException(sprintf('Unable to find node at path "%s.%s".', $rootNode->getName(), $path));
  57          }
  58  
  59          return $this->dumpNode($node);
  60      }
  61  
  62      public function dumpNode(NodeInterface $node)
  63      {
  64          $this->reference = '';
  65          $this->writeNode($node);
  66          $ref = $this->reference;
  67          $this->reference = null;
  68  
  69          return $ref;
  70      }
  71  
  72      /**
  73       * @param int  $depth
  74       * @param bool $prototypedArray
  75       */
  76      private function writeNode(NodeInterface $node, NodeInterface $parentNode = null, $depth = 0, $prototypedArray = false)
  77      {
  78          $comments = [];
  79          $default = '';
  80          $defaultArray = null;
  81          $children = null;
  82          $example = $node->getExample();
  83  
  84          // defaults
  85          if ($node instanceof ArrayNode) {
  86              $children = $node->getChildren();
  87  
  88              if ($node instanceof PrototypedArrayNode) {
  89                  $children = $this->getPrototypeChildren($node);
  90              }
  91  
  92              if (!$children) {
  93                  if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
  94                      $default = '';
  95                  } elseif (!\is_array($example)) {
  96                      $default = '[]';
  97                  }
  98              }
  99          } 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  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1