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

PropertyGenerator.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 6.84 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\Code\Reflection\PropertyReflection;
 013   
 014  class PropertyGenerator extends AbstractMemberGenerator
 015  {
 016      const FLAG_CONSTANT = 0x08;
 017   
 018      /**
 019       * @var bool
 020       */
 021      protected $isConst = null;
 022   
 023      /**
 024       * @var PropertyValueGenerator
 025       */
 026      protected $defaultValue = null;
 027   
 028      /**
 029       * @param  PropertyReflection $reflectionProperty
 030       * @return PropertyGenerator
 031       */
 032      public static function fromReflection(PropertyReflection $reflectionProperty)
 033      {
 034          $property = new static();
 035   
 036          $property->setName($reflectionProperty->getName());
 037   
 038          $allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
 039   
 040          $property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
 041   
 042          if ($reflectionProperty->getDocComment() != '') {
 043              $property->setDocBlock(DocBlockGenerator::fromReflection($reflectionProperty->getDocBlock()));
 044          }
 045   
 046          if ($reflectionProperty->isStatic()) {
 047              $property->setStatic(true);
 048          }
 049   
 050          if ($reflectionProperty->isPrivate()) {
 051              $property->setVisibility(self::VISIBILITY_PRIVATE);
 052          } elseif ($reflectionProperty->isProtected()) {
 053              $property->setVisibility(self::VISIBILITY_PROTECTED);
 054          } else {
 055              $property->setVisibility(self::VISIBILITY_PUBLIC);
 056          }
 057   
 058          $property->setSourceDirty(false);
 059   
 060          return $property;
 061      }
 062   
 063      /**
 064       * Generate from array
 065       *
 066       * @configkey name         string                                          [required] Class Name
 067       * @configkey const        bool
 068       * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
 069       * @configkey flags        int
 070       * @configkey abstract     bool
 071       * @configkey final        bool
 072       * @configkey static       bool
 073       * @configkey visibility   string
 074       *
 075       * @throws Exception\InvalidArgumentException
 076       * @param  array $array
 077       * @return PropertyGenerator
 078       */
 079      public static function fromArray(array $array)
 080      {
 081          if (!isset($array['name'])) {
 082              throw new Exception\InvalidArgumentException(
 083                  'Property generator requires that a name is provided for this object'
 084              );
 085          }
 086   
 087          $property = new static($array['name']);
 088          foreach ($array as $name => $value) {
 089              // normalize key
 090              switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
 091                  case 'const':
 092                      $property->setConst($value);
 093                      break;
 094                  case 'defaultvalue':
 095                      $property->setDefaultValue($value);
 096                      break;
 097                  case 'docblock':
 098                      $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
 099                      $property->setDocBlock($docBlock);
 100                      break;
 101                  case 'flags':
 102                      $property->setFlags($value);
 103                      break;
 104                  case 'abstract':
 105                      $property->setAbstract($value);
 106                      break;
 107                  case 'final':
 108                      $property->setFinal($value);
 109                      break;
 110                  case 'static':
 111                      $property->setStatic($value);
 112                      break;
 113                  case 'visibility':
 114                      $property->setVisibility($value);
 115                      break;
 116              }
 117          }
 118   
 119          return $property;
 120      }
 121   
 122      /**
 123       * @param string $name
 124       * @param PropertyValueGenerator|string|array $defaultValue
 125       * @param int $flags
 126       */
 127      public function __construct($name = null, $defaultValue = null, $flags = self::FLAG_PUBLIC)
 128      {
 129          if (null !== $name) {
 130              $this->setName($name);
 131          }
 132          if (null !== $defaultValue) {
 133              $this->setDefaultValue($defaultValue);
 134          }
 135          if ($flags !== self::FLAG_PUBLIC) {
 136              $this->setFlags($flags);
 137          }
 138      }
 139   
 140      /**
 141       * @param  bool $const
 142       * @return PropertyGenerator
 143       */
 144      public function setConst($const)
 145      {
 146          if ($const) {
 147              $this->removeFlag(self::FLAG_PUBLIC | self::FLAG_PRIVATE | self::FLAG_PROTECTED);
 148              $this->setFlags(self::FLAG_CONSTANT);
 149          } else {
 150              $this->removeFlag(self::FLAG_CONSTANT);
 151          }
 152   
 153          return $this;
 154      }
 155   
 156      /**
 157       * @return bool
 158       */
 159      public function isConst()
 160      {
 161          return (bool) ($this->flags & self::FLAG_CONSTANT);
 162      }
 163   
 164      /**
 165       * @param PropertyValueGenerator|mixed $defaultValue
 166       * @param string                       $defaultValueType
 167       * @param string                       $defaultValueOutputMode
 168       *
 169       * @return PropertyGenerator
 170       */
 171      public function setDefaultValue($defaultValue, $defaultValueType = PropertyValueGenerator::TYPE_AUTO, $defaultValueOutputMode = PropertyValueGenerator::OUTPUT_MULTIPLE_LINE)
 172      {
 173          if (!($defaultValue instanceof PropertyValueGenerator)) {
 174              $defaultValue = new PropertyValueGenerator($defaultValue, $defaultValueType, $defaultValueOutputMode);
 175          }
 176   
 177          $this->defaultValue = $defaultValue;
 178   
 179          return $this;
 180      }
 181   
 182      /**
 183       * @return PropertyValueGenerator
 184       */
 185      public function getDefaultValue()
 186      {
 187          return $this->defaultValue;
 188      }
 189   
 190      /**
 191       * @throws Exception\RuntimeException
 192       * @return string
 193       */
 194      public function generate()
 195      {
 196          $name         = $this->getName();
 197          $defaultValue = $this->getDefaultValue();
 198   
 199          $output = '';
 200   
 201          if (($docBlock = $this->getDocBlock()) !== null) {
 202              $docBlock->setIndentation('    ');
 203              $output .= $docBlock->generate();
 204          }
 205   
 206          if ($this->isConst()) {
 207              if ($defaultValue !== null && !$defaultValue->isValidConstantType()) {
 208                  throw new Exception\RuntimeException(sprintf(
 209                      'The property %s is said to be '
 210                      . 'constant but does not have a valid constant value.',
 211                      $this->name
 212                  ));
 213              }
 214              $output .= $this->indentation . 'const ' . $name . ' = '
 215                  . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
 216          } else {
 217              $output .= $this->indentation
 218                  . $this->getVisibility()
 219                  . (($this->isStatic()) ? ' static' : '')
 220                  . ' $' . $name . ' = '
 221                  . (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
 222          }
 223   
 224          return $output;
 225      }
 226  }
 227