[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Generator/ -> TraitUsageGenerator.php (source)

   1  <?php
   2  /**
   3   * Zend Framework (http://framework.zend.com/)
   4   *
   5   * @link      http://github.com/zendframework/zf2 for the canonical source repository
   6   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   7   * @license   http://framework.zend.com/license/new-bsd New BSD License
   8   */
   9  namespace Zend\Code\Generator;
  10  
  11  use Reflection;
  12  use ReflectionMethod;
  13  
  14  class TraitUsageGenerator extends AbstractGenerator
  15  {
  16      /**
  17       * @var ClassGenerator
  18       */
  19      protected $classGenerator;
  20  
  21      /**
  22       * @var array Array of trait names
  23       */
  24      protected $traits = array();
  25  
  26      /**
  27       * @var array Array of trait aliases
  28       */
  29      protected $traitAliases = array();
  30  
  31      /**
  32       * @var array Array of trait overrides
  33       */
  34      protected $traitOverrides = array();
  35  
  36      /**
  37       * @var array Array of string names
  38       */
  39      protected $uses = array();
  40  
  41      public function __construct(ClassGenerator $classGenerator)
  42      {
  43          $this->classGenerator = $classGenerator;
  44      }
  45  
  46      /**
  47       * @inherit Zend\Code\Generator\TraitUsageInterface
  48       */
  49      public function addUse($use, $useAlias = null)
  50      {
  51          if (! empty($useAlias)) {
  52              $use .= ' as ' . $useAlias;
  53          }
  54  
  55          $this->uses[$use] = $use;
  56          return $this;
  57      }
  58  
  59      /**
  60       * @inherit Zend\Code\Generator\TraitUsageInterface
  61       */
  62      public function getUses()
  63      {
  64          return array_values($this->uses);
  65      }
  66  
  67      /**
  68       * @inherit Zend\Code\Generator\TraitUsageInterface
  69       */
  70      public function addTrait($trait)
  71      {
  72          $traitName = $trait;
  73          if (is_array($trait)) {
  74              if (! array_key_exists('traitName', $trait)) {
  75                  throw new Exception\InvalidArgumentException('Missing required value for traitName');
  76              }
  77              $traitName = $trait['traitName'];
  78  
  79              if (array_key_exists('aliases', $trait)) {
  80                  foreach ($trait['aliases'] as $alias) {
  81                      $this->addAlias($alias);
  82                  }
  83              }
  84  
  85              if (array_key_exists('insteadof', $trait)) {
  86                  foreach ($trait['insteadof'] as $insteadof) {
  87                      $this->addTraitOverride($insteadof);
  88                  }
  89              }
  90          }
  91  
  92          if (! $this->hasTrait($traitName)) {
  93              $this->traits[] = $traitName;
  94          }
  95  
  96          return $this;
  97      }
  98  
  99      /**
 100       * @inherit Zend\Code\Generator\TraitUsageInterface
 101       */
 102      public function addTraits(array $traits)
 103      {
 104          foreach ($traits as $trait) {
 105              $this->addTrait($trait);
 106          }
 107  
 108          return $this;
 109      }
 110  
 111      /**
 112       * @inherit Zend\Code\Generator\TraitUsageInterface
 113       */
 114      public function hasTrait($traitName)
 115      {
 116          return in_array($traitName, $this->traits);
 117      }
 118  
 119      /**
 120       * @inherit Zend\Code\Generator\TraitUsageInterface
 121       */
 122      public function getTraits()
 123      {
 124          return $this->traits;
 125      }
 126  
 127      /**
 128       * @inherit Zend\Code\Generator\TraitUsageInterface
 129       */
 130      public function removeTrait($traitName)
 131      {
 132          $key = array_search($traitName, $this->traits);
 133          if (false !== $key) {
 134              unset($this->traits[$key]);
 135          }
 136  
 137          return $this;
 138      }
 139  
 140      /**
 141       * @inherit Zend\Code\Generator\TraitUsageInterface
 142       */
 143      public function addTraitAlias($method, $alias, $visibility = null)
 144      {
 145          $traitAndMethod = $method;
 146          if (is_array($method)) {
 147              if (! array_key_exists('traitName', $method)) {
 148                  throw new Exception\InvalidArgumentException('Missing required argument "traitName" for $method');
 149              }
 150  
 151              if (! array_key_exists('method', $method)) {
 152                  throw new Exception\InvalidArgumentException('Missing required argument "method" for $method');
 153              }
 154  
 155              $traitAndMethod = $method['traitName'] . '::' . $method['method'];
 156          }
 157  
 158          // Validations
 159          if (false === strpos($traitAndMethod, "::")) {
 160              throw new Exception\InvalidArgumentException(
 161                  'Invalid Format: $method must be in the format of trait::method'
 162              );
 163          }
 164          if (! is_string($alias)) {
 165              throw new Exception\InvalidArgumentException('Invalid Alias: $alias must be a string or array.');
 166          }
 167          if ($this->classGenerator->hasMethod($alias)) {
 168              throw new Exception\InvalidArgumentException('Invalid Alias: Method name already exists on this class.');
 169          }
 170          if (null !== $visibility
 171              && $visibility !== ReflectionMethod::IS_PUBLIC
 172              && $visibility !== ReflectionMethod::IS_PRIVATE
 173              && $visibility !== ReflectionMethod::IS_PROTECTED
 174          ) {
 175              throw new Exception\InvalidArgumentException(
 176                  'Invalid Type: $visibility must of ReflectionMethod::IS_PUBLIC,'
 177                  . ' ReflectionMethod::IS_PRIVATE or ReflectionMethod::IS_PROTECTED'
 178              );
 179          }
 180  
 181          list($trait, $method) = explode('::', $traitAndMethod);
 182          if (! $this->hasTrait($trait)) {
 183              throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class');
 184          }
 185  
 186          $this->traitAliases[$traitAndMethod] = array(
 187              'alias'      => $alias,
 188              'visibility' => $visibility
 189          );
 190  
 191          return $this;
 192      }
 193  
 194      /**
 195       * @inherit Zend\Code\Generator\TraitUsageInterface
 196       */
 197      public function getTraitAliases()
 198      {
 199          return $this->traitAliases;
 200      }
 201  
 202      /**
 203       * @inherit Zend\Code\Generator\TraitUsageInterface
 204       */
 205      public function addTraitOverride($method, $traitsToReplace)
 206      {
 207          if (false === is_array($traitsToReplace)) {
 208              $traitsToReplace = array($traitsToReplace);
 209          }
 210  
 211          $traitAndMethod = $method;
 212          if (is_array($method)) {
 213              if (! array_key_exists('traitName', $method)) {
 214                  throw new Exception\InvalidArgumentException('Missing required argument "traitName" for $method');
 215              }
 216  
 217              if (! array_key_exists('method', $method)) {
 218                  throw new Exception\InvalidArgumentException('Missing required argument "method" for $method');
 219              }
 220  
 221              $traitAndMethod = (string) $method['traitName'] . '::' . (string) $method['method'];
 222          }
 223  
 224          // Validations
 225          if (false === strpos($traitAndMethod, "::")) {
 226              throw new Exception\InvalidArgumentException(
 227                  'Invalid Format: $method must be in the format of trait::method'
 228              );
 229          }
 230  
 231          list($trait, $method) = explode("::", $traitAndMethod);
 232          if (! $this->hasTrait($trait)) {
 233              throw new Exception\InvalidArgumentException('Invalid trait: Trait does not exists on this class');
 234          }
 235  
 236          if (! array_key_exists($traitAndMethod, $this->traitOverrides)) {
 237              $this->traitOverrides[$traitAndMethod] = array();
 238          }
 239  
 240          foreach ($traitsToReplace as $traitToReplace) {
 241              if (! is_string($traitToReplace)) {
 242                  throw new Exception\InvalidArgumentException(
 243                      'Invalid Argument: $traitToReplace must be a string or array of strings'
 244                  );
 245              }
 246  
 247              if (! in_array($traitToReplace, $this->traitOverrides[$traitAndMethod])) {
 248                  $this->traitOverrides[$traitAndMethod][] = $traitToReplace;
 249              }
 250          }
 251  
 252          return $this;
 253      }
 254  
 255      /**
 256       * @inherit Zend\Code\Generator\TraitUsageInterface
 257       */
 258      public function removeTraitOverride($method, $overridesToRemove = null)
 259      {
 260          if (! array_key_exists($method, $this->traitOverrides)) {
 261              return $this;
 262          }
 263  
 264          if (null === $overridesToRemove) {
 265              unset($this->traitOverrides[$method]);
 266              return $this;
 267          }
 268  
 269          $overridesToRemove = (! is_array($overridesToRemove))
 270              ? array($overridesToRemove)
 271              : $overridesToRemove;
 272          foreach ($overridesToRemove as $traitToRemove) {
 273              $key = array_search($traitToRemove, $this->traitOverrides[$method]);
 274              if (false !== $key) {
 275                  unset($this->traitOverrides[$method][$key]);
 276              }
 277          }
 278          return $this;
 279      }
 280  
 281      /**
 282       * @inherit Zend\Code\Generator\TraitUsageInterface
 283       */
 284      public function getTraitOverrides()
 285      {
 286          return $this->traitOverrides;
 287      }
 288  
 289      /**
 290       * @inherit Zend\Code\Generator\GeneratorInterface
 291       */
 292      public function generate()
 293      {
 294          $output = '';
 295          $indent = $this->getIndentation();
 296          $traits = $this->getTraits();
 297  
 298          if (empty($traits)) {
 299              return $output;
 300          }
 301  
 302          $output .= $indent . 'use ' . implode(', ', $traits);
 303  
 304          $aliases   = $this->getTraitAliases();
 305          $overrides = $this->getTraitOverrides();
 306          if (empty($aliases) && empty($overrides)) {
 307              $output .= ";" . self::LINE_FEED . self::LINE_FEED;
 308              return $output;
 309          }
 310  
 311          $output .= ' {' . self::LINE_FEED;
 312          foreach ($aliases as $method => $alias) {
 313              $visibility = (null !== $alias['visibility'])
 314                  ? current(Reflection::getModifierNames($alias['visibility'])) . ' '
 315                  : '';
 316  
 317              // validation check
 318              if ($this->classGenerator->hasMethod($alias['alias'])) {
 319                  throw new Exception\RuntimeException(sprintf(
 320                      'Generation Error: Aliased method %s already exists on this class',
 321                      $alias['alias']
 322                  ));
 323              }
 324  
 325              $output .=
 326                  $indent
 327                  . $indent
 328                  . $method
 329                  . ' as '
 330                  . $visibility
 331                  . $alias['alias']
 332                  . ';'
 333                  . self::LINE_FEED;
 334          }
 335  
 336          foreach ($overrides as $method => $insteadofTraits) {
 337              foreach ($insteadofTraits as $insteadofTrait) {
 338                  $output .=
 339                      $indent
 340                      . $indent
 341                      . $method
 342                      . ' insteadof '
 343                      . $insteadofTrait
 344                      . ';'
 345                      . self::LINE_FEED;
 346              }
 347          }
 348  
 349          $output .= self::LINE_FEED . $indent . '}' . self::LINE_FEED . self::LINE_FEED;
 350  
 351          return $output;
 352      }
 353  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1