[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/config/Symfony/Component/Config/Definition/ -> ReferenceDumper.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;
  13  
  14  /**
  15   * Dumps a reference configuration for the given configuration/node instance.
  16   *
  17   * Currently, only YML format is supported.
  18   *
  19   * @author Kevin Bond <kevinbond@gmail.com>
  20   */
  21  class ReferenceDumper
  22  {
  23      private $reference;
  24  
  25      public function dump(ConfigurationInterface $configuration)
  26      {
  27          return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
  28      }
  29  
  30      public function dumpNode(NodeInterface $node)
  31      {
  32          $this->reference = '';
  33          $this->writeNode($node);
  34          $ref = $this->reference;
  35          $this->reference = null;
  36  
  37          return $ref;
  38      }
  39  
  40      /**
  41       * @param NodeInterface $node
  42       * @param int           $depth
  43       */
  44      private function writeNode(NodeInterface $node, $depth = 0)
  45      {
  46          $comments = array();
  47          $default = '';
  48          $defaultArray = null;
  49          $children = null;
  50          $example = $node->getExample();
  51  
  52          // defaults
  53          if ($node instanceof ArrayNode) {
  54              $children = $node->getChildren();
  55  
  56              if ($node instanceof PrototypedArrayNode) {
  57                  $prototype = $node->getPrototype();
  58  
  59                  if ($prototype instanceof ArrayNode) {
  60                      $children = $prototype->getChildren();
  61                  }
  62  
  63                  // check for attribute as key
  64                  if ($key = $node->getKeyAttribute()) {
  65                      $keyNode = new ArrayNode($key, $node);
  66                      $keyNode->setInfo('Prototype');
  67  
  68                      // add children
  69                      foreach ($children as $childNode) {
  70                          $keyNode->addChild($childNode);
  71                      }
  72                      $children = array($key => $keyNode);
  73                  }
  74              }
  75  
  76              if (!$children) {
  77                  if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
  78                      $default = '';
  79                  } elseif (!is_array($example)) {
  80                      $default = '[]';
  81                  }
  82              }
  83          } else {
  84              $default = '~';
  85  
  86              if ($node->hasDefaultValue()) {
  87                  $default = $node->getDefaultValue();
  88  
  89                  if (true === $default) {
  90                      $default = 'true';
  91                  } elseif (false === $default) {
  92                      $default = 'false';
  93                  } elseif (null === $default) {
  94                      $default = '~';
  95                  } elseif (is_array($default)) {
  96                      if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
  97                          $default = '';
  98                      } elseif (!is_array($example)) {
  99                          $default = '[]';
 100                      }
 101                  }
 102              }
 103          }
 104  
 105          // required?
 106          if ($node->isRequired()) {
 107              $comments[] = 'Required';
 108          }
 109  
 110          // example
 111          if ($example && !is_array($example)) {
 112              $comments[] = 'Example: '.$example;
 113          }
 114  
 115          $default = (string) $default != '' ? ' '.$default : '';
 116          $comments = count($comments) ? '# '.implode(', ', $comments) : '';
 117  
 118          $text = rtrim(sprintf('%-20s %s %s', $node->getName().':', $default, $comments), ' ');
 119  
 120          if ($info = $node->getInfo()) {
 121              $this->writeLine('');
 122              // indenting multi-line info
 123              $info = str_replace("\n", sprintf("\n%".($depth * 4).'s# ', ' '), $info);
 124              $this->writeLine('# '.$info, $depth * 4);
 125          }
 126  
 127          $this->writeLine($text, $depth * 4);
 128  
 129          // output defaults
 130          if ($defaultArray) {
 131              $this->writeLine('');
 132  
 133              $message = count($defaultArray) > 1 ? 'Defaults' : 'Default';
 134  
 135              $this->writeLine('# '.$message.':', $depth * 4 + 4);
 136  
 137              $this->writeArray($defaultArray, $depth + 1);
 138          }
 139  
 140          if (is_array($example)) {
 141              $this->writeLine('');
 142  
 143              $message = count($example) > 1 ? 'Examples' : 'Example';
 144  
 145              $this->writeLine('# '.$message.':', $depth * 4 + 4);
 146  
 147              $this->writeArray($example, $depth + 1);
 148          }
 149  
 150          if ($children) {
 151              foreach ($children as $childNode) {
 152                  $this->writeNode($childNode, $depth + 1);
 153              }
 154          }
 155      }
 156  
 157      /**
 158       * Outputs a single config reference line.
 159       *
 160       * @param string $text
 161       * @param int    $indent
 162       */
 163      private function writeLine($text, $indent = 0)
 164      {
 165          $indent = strlen($text) + $indent;
 166          $format = '%'.$indent.'s';
 167  
 168          $this->reference .= sprintf($format, $text)."\n";
 169      }
 170  
 171      private function writeArray(array $array, $depth)
 172      {
 173          $isIndexed = array_values($array) === $array;
 174  
 175          foreach ($array as $key => $value) {
 176              if (is_array($value)) {
 177                  $val = '';
 178              } else {
 179                  $val = $value;
 180              }
 181  
 182              if ($isIndexed) {
 183                  $this->writeLine('- '.$val, $depth * 4);
 184              } else {
 185                  $this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
 186              }
 187  
 188              if (is_array($value)) {
 189                  $this->writeArray($value, $depth + 1);
 190              }
 191          }
 192      }
 193  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1