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

ValueGenerator.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 10.95 KiB


 001  <?php
 002  /**
 003   * Zend Framework (http://framework.zend.com/)
 004   *
 005   * @link      http://github.com/zendframework/zf2 for the canonical source repository
 006   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 007   * @license   http://framework.zend.com/license/new-bsd New BSD License
 008   */
 009   
 010  namespace Zend\Code\Generator;
 011   
 012  use Zend\Stdlib\ArrayObject;
 013   
 014  class ValueGenerator extends AbstractGenerator
 015  {
 016      /**#@+
 017       * Constant values
 018       */
 019      const TYPE_AUTO     = 'auto';
 020      const TYPE_BOOLEAN  = 'boolean';
 021      const TYPE_BOOL     = 'bool';
 022      const TYPE_NUMBER   = 'number';
 023      const TYPE_INTEGER  = 'integer';
 024      const TYPE_INT      = 'int';
 025      const TYPE_FLOAT    = 'float';
 026      const TYPE_DOUBLE   = 'double';
 027      const TYPE_STRING   = 'string';
 028      const TYPE_ARRAY    = 'array';
 029      const TYPE_CONSTANT = 'constant';
 030      const TYPE_NULL     = 'null';
 031      const TYPE_OBJECT   = 'object';
 032      const TYPE_OTHER    = 'other';
 033      /**#@-*/
 034   
 035      const OUTPUT_MULTIPLE_LINE = 'multipleLine';
 036      const OUTPUT_SINGLE_LINE   = 'singleLine';
 037   
 038      /**
 039       * @var mixed
 040       */
 041      protected $value = null;
 042   
 043      /**
 044       * @var string
 045       */
 046      protected $type = self::TYPE_AUTO;
 047   
 048      /**
 049       * @var int
 050       */
 051      protected $arrayDepth = 0;
 052   
 053      /**
 054       * @var string
 055       */
 056      protected $outputMode = self::OUTPUT_MULTIPLE_LINE;
 057   
 058      /**
 059       * @var array
 060       */
 061      protected $allowedTypes = null;
 062      /**
 063       * Autodetectable constants
 064       * @var ArrayObject
 065       */
 066      protected $constants = null;
 067   
 068      /**
 069       * @param mixed       $value
 070       * @param string      $type
 071       * @param string      $outputMode
 072       * @param ArrayObject $constants
 073       */
 074      public function __construct($value = null, $type = self::TYPE_AUTO, $outputMode = self::OUTPUT_MULTIPLE_LINE, ArrayObject $constants = null)
 075      {
 076          // strict check is important here if $type = AUTO
 077          if ($value !== null) {
 078              $this->setValue($value);
 079          }
 080          if ($type !== self::TYPE_AUTO) {
 081              $this->setType($type);
 082          }
 083          if ($outputMode !== self::OUTPUT_MULTIPLE_LINE) {
 084              $this->setOutputMode($outputMode);
 085          }
 086          if ($constants !== null) {
 087              $this->constants = $constants;
 088          } else {
 089              $this->constants = new ArrayObject();
 090          }
 091      }
 092   
 093      /**
 094       * Init constant list by defined and magic constants
 095       */
 096      public function initEnvironmentConstants()
 097      {
 098          $constants   = array(
 099              '__DIR__',
 100              '__FILE__',
 101              '__LINE__',
 102              '__CLASS__',
 103              '__TRAIT__',
 104              '__METHOD__',
 105              '__FUNCTION__',
 106              '__NAMESPACE__',
 107              '::'
 108          );
 109          $constants = array_merge($constants, array_keys(get_defined_constants()), $this->constants->getArrayCopy());
 110          $this->constants->exchangeArray($constants);
 111      }
 112   
 113      /**
 114       * Add constant to list
 115       *
 116       * @param string $constant
 117       *
 118       * @return $this
 119       */
 120      public function addConstant($constant)
 121      {
 122          $this->constants->append($constant);
 123   
 124          return $this;
 125      }
 126   
 127      /**
 128       * Delete constant from constant list
 129       *
 130       * @param string $constant
 131       *
 132       * @return bool
 133       */
 134      public function deleteConstant($constant)
 135      {
 136          if (($index = array_search($constant, $this->constants->getArrayCopy())) !== false) {
 137              $this->constants->offsetUnset($index);
 138          }
 139   
 140          return $index !== false;
 141      }
 142   
 143      /**
 144       * Return constant list
 145       *
 146       * @return ArrayObject
 147       */
 148      public function getConstants()
 149      {
 150          return $this->constants;
 151      }
 152   
 153      /**
 154       * @return bool
 155       */
 156      public function isValidConstantType()
 157      {
 158          if ($this->type == self::TYPE_AUTO) {
 159              $type = $this->getAutoDeterminedType($this->value);
 160          } else {
 161              $type = $this->type;
 162          }
 163   
 164          // valid types for constants
 165          $scalarTypes = array(
 166              self::TYPE_BOOLEAN,
 167              self::TYPE_BOOL,
 168              self::TYPE_NUMBER,
 169              self::TYPE_INTEGER,
 170              self::TYPE_INT,
 171              self::TYPE_FLOAT,
 172              self::TYPE_DOUBLE,
 173              self::TYPE_STRING,
 174              self::TYPE_CONSTANT,
 175              self::TYPE_NULL
 176          );
 177   
 178          return in_array($type, $scalarTypes);
 179      }
 180   
 181      /**
 182       * @param  mixed $value
 183       * @return ValueGenerator
 184       */
 185      public function setValue($value)
 186      {
 187          $this->value = $value;
 188          return $this;
 189      }
 190   
 191      /**
 192       * @return mixed
 193       */
 194      public function getValue()
 195      {
 196          return $this->value;
 197      }
 198   
 199      /**
 200       * @param  string $type
 201       * @return ValueGenerator
 202       */
 203      public function setType($type)
 204      {
 205          $this->type = (string) $type;
 206          return $this;
 207      }
 208   
 209      /**
 210       * @return string
 211       */
 212      public function getType()
 213      {
 214          return $this->type;
 215      }
 216   
 217      /**
 218       * @param  int $arrayDepth
 219       * @return ValueGenerator
 220       */
 221      public function setArrayDepth($arrayDepth)
 222      {
 223          $this->arrayDepth = (int) $arrayDepth;
 224          return $this;
 225      }
 226   
 227      /**
 228       * @return int
 229       */
 230      public function getArrayDepth()
 231      {
 232          return $this->arrayDepth;
 233      }
 234   
 235      /**
 236       * @param  string $type
 237       * @return string
 238       */
 239      protected function getValidatedType($type)
 240      {
 241          $types = array(
 242              self::TYPE_AUTO,
 243              self::TYPE_BOOLEAN,
 244              self::TYPE_BOOL,
 245              self::TYPE_NUMBER,
 246              self::TYPE_INTEGER,
 247              self::TYPE_INT,
 248              self::TYPE_FLOAT,
 249              self::TYPE_DOUBLE,
 250              self::TYPE_STRING,
 251              self::TYPE_ARRAY,
 252              self::TYPE_CONSTANT,
 253              self::TYPE_NULL,
 254              self::TYPE_OBJECT,
 255              self::TYPE_OTHER
 256          );
 257   
 258          if (in_array($type, $types)) {
 259              return $type;
 260          }
 261   
 262          return self::TYPE_AUTO;
 263      }
 264   
 265      /**
 266       * @param  mixed $value
 267       * @return string
 268       */
 269      public function getAutoDeterminedType($value)
 270      {
 271          switch (gettype($value)) {
 272              case 'boolean':
 273                  return self::TYPE_BOOLEAN;
 274              case 'string':
 275                  foreach ($this->constants as $constant) {
 276                      if (strpos($value, $constant) !== false) {
 277                          return self::TYPE_CONSTANT;
 278                      }
 279                  }
 280                  return self::TYPE_STRING;
 281              case 'double':
 282              case 'float':
 283              case 'integer':
 284                  return self::TYPE_NUMBER;
 285              case 'array':
 286                  return self::TYPE_ARRAY;
 287              case 'NULL':
 288                  return self::TYPE_NULL;
 289              case 'object':
 290              case 'resource':
 291              case 'unknown type':
 292              default:
 293                  return self::TYPE_OTHER;
 294          }
 295      }
 296   
 297      /**
 298       * @throws Exception\RuntimeException
 299       * @return string
 300       */
 301      public function generate()
 302      {
 303          $type = $this->type;
 304   
 305          if ($type != self::TYPE_AUTO) {
 306              $type = $this->getValidatedType($type);
 307          }
 308   
 309          $value = $this->value;
 310   
 311          if ($type == self::TYPE_AUTO) {
 312              $type = $this->getAutoDeterminedType($value);
 313          }
 314   
 315          if ($type == self::TYPE_ARRAY) {
 316              foreach ($value as &$curValue) {
 317                  if ($curValue instanceof self) {
 318                      continue;
 319                  }
 320                  $curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
 321              }
 322          }
 323   
 324          $output = '';
 325   
 326          switch ($type) {
 327              case self::TYPE_BOOLEAN:
 328              case self::TYPE_BOOL:
 329                  $output .= ($value ? 'true' : 'false');
 330                  break;
 331              case self::TYPE_STRING:
 332                  $output .= self::escape($value);
 333                  break;
 334              case self::TYPE_NULL:
 335                  $output .= 'null';
 336                  break;
 337              case self::TYPE_NUMBER:
 338              case self::TYPE_INTEGER:
 339              case self::TYPE_INT:
 340              case self::TYPE_FLOAT:
 341              case self::TYPE_DOUBLE:
 342              case self::TYPE_CONSTANT:
 343                  $output .= $value;
 344                  break;
 345              case self::TYPE_ARRAY:
 346                  $output .= 'array(';
 347                  if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
 348                      $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
 349                  }
 350                  $outputParts = array();
 351                  $noKeyIndex  = 0;
 352                  foreach ($value as $n => $v) {
 353                      /* @var $v ValueGenerator */
 354                      $v->setArrayDepth($this->arrayDepth + 1);
 355                      $partV = $v->generate();
 356                      $short = false;
 357                      if (is_int($n)) {
 358                          if ($n === $noKeyIndex) {
 359                              $short = true;
 360                              $noKeyIndex++;
 361                          } else {
 362                              $noKeyIndex = max($n + 1, $noKeyIndex);
 363                          }
 364                      }
 365   
 366                      if ($short) {
 367                          $outputParts[] = $partV;
 368                      } else {
 369                          $outputParts[] = (is_int($n) ? $n : self::escape($n)) . ' => ' . $partV;
 370                      }
 371                  }
 372                  $padding = ($this->outputMode == self::OUTPUT_MULTIPLE_LINE)
 373                      ? self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1)
 374                      : ' ';
 375                  $output .= implode(',' . $padding, $outputParts);
 376                  if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
 377                      if (count($outputParts) > 0) {
 378                          $output .= ',';
 379                      }
 380                      $output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth);
 381                  }
 382                  $output .= ')';
 383                  break;
 384              case self::TYPE_OTHER:
 385              default:
 386                  throw new Exception\RuntimeException(
 387                      sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
 388                  );
 389          }
 390   
 391          return $output;
 392      }
 393   
 394      /**
 395       * Quotes value for PHP code.
 396       *
 397       * @param  string $input Raw string.
 398       * @param  bool $quote Whether add surrounding quotes or not.
 399       * @return string PHP-ready code.
 400       */
 401      public static function escape($input, $quote = true)
 402      {
 403          $output = addcslashes($input, "\\'");
 404   
 405          // adds quoting strings
 406          if ($quote) {
 407              $output = "'" . $output . "'";
 408          }
 409   
 410          return $output;
 411      }
 412   
 413      /**
 414       * @param  string $outputMode
 415       * @return ValueGenerator
 416       */
 417      public function setOutputMode($outputMode)
 418      {
 419          $this->outputMode = (string) $outputMode;
 420          return $this;
 421      }
 422   
 423      /**
 424       * @return string
 425       */
 426      public function getOutputMode()
 427      {
 428          return $this->outputMode;
 429      }
 430   
 431      public function __toString()
 432      {
 433          return $this->generate();
 434      }
 435  }
 436