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

MethodGenerator.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 8.38 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\MethodReflection;
 013   
 014  class MethodGenerator extends AbstractMemberGenerator
 015  {
 016      /**
 017       * @var DocBlockGenerator
 018       */
 019      protected $docBlock = null;
 020   
 021      /**
 022       * @var ParameterGenerator[]
 023       */
 024      protected $parameters = array();
 025   
 026      /**
 027       * @var string
 028       */
 029      protected $body = null;
 030   
 031      /**
 032       * @param  MethodReflection $reflectionMethod
 033       * @return MethodGenerator
 034       */
 035      public static function fromReflection(MethodReflection $reflectionMethod)
 036      {
 037          $method = new static();
 038   
 039          $method->setSourceContent($reflectionMethod->getContents(false));
 040          $method->setSourceDirty(false);
 041   
 042          if ($reflectionMethod->getDocComment() != '') {
 043              $method->setDocBlock(DocBlockGenerator::fromReflection($reflectionMethod->getDocBlock()));
 044          }
 045   
 046          $method->setFinal($reflectionMethod->isFinal());
 047   
 048          if ($reflectionMethod->isPrivate()) {
 049              $method->setVisibility(self::VISIBILITY_PRIVATE);
 050          } elseif ($reflectionMethod->isProtected()) {
 051              $method->setVisibility(self::VISIBILITY_PROTECTED);
 052          } else {
 053              $method->setVisibility(self::VISIBILITY_PUBLIC);
 054          }
 055   
 056          $method->setStatic($reflectionMethod->isStatic());
 057   
 058          $method->setName($reflectionMethod->getName());
 059   
 060          foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
 061              $method->setParameter(ParameterGenerator::fromReflection($reflectionParameter));
 062          }
 063   
 064          $method->setBody(static::clearBodyIndention($reflectionMethod->getBody()));
 065   
 066          return $method;
 067      }
 068   
 069      /**
 070       * Identify the space indention from the first line and remove this indention
 071       * from all lines
 072       *
 073       * @param string $body
 074       *
 075       * @return string
 076       */
 077      protected static function clearBodyIndention($body)
 078      {
 079          if (empty($body)) {
 080              return $body;
 081          }
 082   
 083          $lines = explode(PHP_EOL, $body);
 084   
 085          $indention = str_replace(trim($lines[1]), '', $lines[1]);
 086   
 087          foreach ($lines as $key => $line) {
 088              if (substr($line, 0, strlen($indention)) == $indention) {
 089                  $lines[$key] = substr($line, strlen($indention));
 090              }
 091          }
 092   
 093          $body = implode(PHP_EOL, $lines);
 094   
 095          return $body;
 096      }
 097   
 098      /**
 099       * Generate from array
 100       *
 101       * @configkey name           string        [required] Class Name
 102       * @configkey docblock       string        The docblock information
 103       * @configkey flags          int           Flags, one of MethodGenerator::FLAG_ABSTRACT MethodGenerator::FLAG_FINAL
 104       * @configkey parameters     string        Class which this class is extending
 105       * @configkey body           string
 106       * @configkey abstract       bool
 107       * @configkey final          bool
 108       * @configkey static         bool
 109       * @configkey visibility     string
 110       *
 111       * @throws Exception\InvalidArgumentException
 112       * @param  array $array
 113       * @return MethodGenerator
 114       */
 115      public static function fromArray(array $array)
 116      {
 117          if (!isset($array['name'])) {
 118              throw new Exception\InvalidArgumentException(
 119                  'Method generator requires that a name is provided for this object'
 120              );
 121          }
 122   
 123          $method = new static($array['name']);
 124          foreach ($array as $name => $value) {
 125              // normalize key
 126              switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
 127                  case 'docblock':
 128                      $docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
 129                      $method->setDocBlock($docBlock);
 130                      break;
 131                  case 'flags':
 132                      $method->setFlags($value);
 133                      break;
 134                  case 'parameters':
 135                      $method->setParameters($value);
 136                      break;
 137                  case 'body':
 138                      $method->setBody($value);
 139                      break;
 140                  case 'abstract':
 141                      $method->setAbstract($value);
 142                      break;
 143                  case 'final':
 144                      $method->setFinal($value);
 145                      break;
 146                  case 'static':
 147                      $method->setStatic($value);
 148                      break;
 149                  case 'visibility':
 150                      $method->setVisibility($value);
 151                      break;
 152              }
 153          }
 154   
 155          return $method;
 156      }
 157   
 158      /**
 159       * @param  string $name
 160       * @param  array $parameters
 161       * @param  int $flags
 162       * @param  string $body
 163       * @param  DocBlockGenerator|string $docBlock
 164       */
 165      public function __construct(
 166          $name = null,
 167          array $parameters = array(),
 168          $flags = self::FLAG_PUBLIC,
 169          $body = null,
 170          $docBlock = null
 171      ) {
 172          if ($name) {
 173              $this->setName($name);
 174          }
 175          if ($parameters) {
 176              $this->setParameters($parameters);
 177          }
 178          if ($flags !== self::FLAG_PUBLIC) {
 179              $this->setFlags($flags);
 180          }
 181          if ($body) {
 182              $this->setBody($body);
 183          }
 184          if ($docBlock) {
 185              $this->setDocBlock($docBlock);
 186          }
 187      }
 188   
 189      /**
 190       * @param  array $parameters
 191       * @return MethodGenerator
 192       */
 193      public function setParameters(array $parameters)
 194      {
 195          foreach ($parameters as $parameter) {
 196              $this->setParameter($parameter);
 197          }
 198   
 199          return $this;
 200      }
 201   
 202      /**
 203       * @param  ParameterGenerator|array|string $parameter
 204       * @throws Exception\InvalidArgumentException
 205       * @return MethodGenerator
 206       */
 207      public function setParameter($parameter)
 208      {
 209          if (is_string($parameter)) {
 210              $parameter = new ParameterGenerator($parameter);
 211          }
 212   
 213          if (is_array($parameter)) {
 214              $parameter = ParameterGenerator::fromArray($parameter);
 215          }
 216   
 217          if (!$parameter instanceof ParameterGenerator) {
 218              throw new Exception\InvalidArgumentException(sprintf(
 219                  '%s is expecting either a string, array or an instance of %s\ParameterGenerator',
 220                  __METHOD__,
 221                  __NAMESPACE__
 222              ));
 223          }
 224   
 225          $this->parameters[$parameter->getName()] = $parameter;
 226   
 227          return $this;
 228      }
 229   
 230      /**
 231       * @return ParameterGenerator[]
 232       */
 233      public function getParameters()
 234      {
 235          return $this->parameters;
 236      }
 237   
 238      /**
 239       * @param  string $body
 240       * @return MethodGenerator
 241       */
 242      public function setBody($body)
 243      {
 244          $this->body = $body;
 245          return $this;
 246      }
 247   
 248      /**
 249       * @return string
 250       */
 251      public function getBody()
 252      {
 253          return $this->body;
 254      }
 255   
 256      /**
 257       * @return string
 258       */
 259      public function generate()
 260      {
 261          $output = '';
 262   
 263          $indent = $this->getIndentation();
 264   
 265          if (($docBlock = $this->getDocBlock()) !== null) {
 266              $docBlock->setIndentation($indent);
 267              $output .= $docBlock->generate();
 268          }
 269   
 270          $output .= $indent;
 271   
 272          if ($this->isAbstract()) {
 273              $output .= 'abstract ';
 274          } else {
 275              $output .= (($this->isFinal()) ? 'final ' : '');
 276          }
 277   
 278          $output .= $this->getVisibility()
 279              . (($this->isStatic()) ? ' static' : '')
 280              . ' function ' . $this->getName() . '(';
 281   
 282          $parameters = $this->getParameters();
 283          if (!empty($parameters)) {
 284              foreach ($parameters as $parameter) {
 285                  $parameterOutput[] = $parameter->generate();
 286              }
 287   
 288              $output .= implode(', ', $parameterOutput);
 289          }
 290   
 291          $output .= ')';
 292   
 293          if ($this->isAbstract()) {
 294              return $output . ';';
 295          }
 296   
 297          $output .= self::LINE_FEED . $indent . '{' . self::LINE_FEED;
 298   
 299          if ($this->body) {
 300              $output .= preg_replace('#^((?![a-zA-Z0-9_-]+;).+?)$#m', $indent . $indent . '$1', trim($this->body))
 301                  . self::LINE_FEED;
 302          }
 303   
 304          $output .= $indent . '}' . self::LINE_FEED;
 305   
 306          return $output;
 307      }
 308   
 309      public function __toString()
 310      {
 311          return $this->generate();
 312      }
 313  }
 314