[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-code/src/Annotation/Parser/ -> GenericAnnotationParser.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  
  10  namespace Zend\Code\Annotation\Parser;
  11  
  12  use Traversable;
  13  use Zend\Code\Annotation\AnnotationInterface;
  14  use Zend\Code\Exception;
  15  use Zend\EventManager\EventInterface;
  16  
  17  /**
  18   * Generic annotation parser
  19   *
  20   * Expects registration of AnnotationInterface instances. Such instances
  21   * will be passed annotation content to their initialize() method, which
  22   * they are then responsible for parsing.
  23   */
  24  class GenericAnnotationParser implements ParserInterface
  25  {
  26      /**
  27       * @var array
  28       */
  29      protected $aliases = array();
  30  
  31      /**
  32       * @var array
  33       */
  34      protected $annotationNames = array();
  35  
  36      /**
  37       * @var AnnotationInterface[]
  38       */
  39      protected $annotations = array();
  40  
  41      /**
  42       * Listen to onCreateAnnotation, and attempt to return an annotation object
  43       * instance.
  44       *
  45       * If the annotation class or alias is not registered, immediately returns
  46       * false. Otherwise, resolves the class, clones it, and, if any content is
  47       * present, calls {@link AnnotationInterface::initialize()} with the
  48       * content.
  49       *
  50       * @param  EventInterface $e
  51       * @return false|AnnotationInterface
  52       */
  53      public function onCreateAnnotation(EventInterface $e)
  54      {
  55          $class = $e->getParam('class', false);
  56          if (!$class || !$this->hasAnnotation($class)) {
  57              return false;
  58          }
  59  
  60          $content = $e->getParam('content', '');
  61          $content = trim($content, '()');
  62  
  63          if ($this->hasAlias($class)) {
  64              $class = $this->resolveAlias($class);
  65          }
  66  
  67          $index      = array_search($class, $this->annotationNames);
  68          $annotation = $this->annotations[$index];
  69  
  70          $newAnnotation = clone $annotation;
  71          if ($content) {
  72              $newAnnotation->initialize($content);
  73          }
  74  
  75          return $newAnnotation;
  76      }
  77  
  78      /**
  79       * Register annotations
  80       *
  81       * @param  string|AnnotationInterface $annotation String class name of an
  82       *         AnnotationInterface implementation, or actual instance
  83       * @return GenericAnnotationParser
  84       * @throws Exception\InvalidArgumentException
  85       */
  86      public function registerAnnotation($annotation)
  87      {
  88          $class = false;
  89          if (is_string($annotation) && class_exists($annotation)) {
  90              $class      = $annotation;
  91              $annotation = new $annotation();
  92          }
  93  
  94          if (!$annotation instanceof AnnotationInterface) {
  95              throw new Exception\InvalidArgumentException(sprintf(
  96                  '%s: expects an instance of %s\AnnotationInterface; received "%s"',
  97                  __METHOD__,
  98                  __NAMESPACE__,
  99                  (is_object($annotation) ? get_class($annotation) : gettype($annotation))
 100              ));
 101          }
 102  
 103          $class = $class ?: get_class($annotation);
 104  
 105          if (in_array($class, $this->annotationNames)) {
 106              throw new Exception\InvalidArgumentException(sprintf(
 107                  'An annotation for this class %s already exists',
 108                  $class
 109              ));
 110          }
 111  
 112          $this->annotations[]     = $annotation;
 113          $this->annotationNames[] = $class;
 114      }
 115  
 116      /**
 117       * Register many annotations at once
 118       *
 119       * @param  array|Traversable $annotations
 120       * @throws Exception\InvalidArgumentException
 121       * @return GenericAnnotationParser
 122       */
 123      public function registerAnnotations($annotations)
 124      {
 125          if (!is_array($annotations) && !$annotations instanceof Traversable) {
 126              throw new Exception\InvalidArgumentException(sprintf(
 127                  '%s: expects an array or Traversable; received "%s"',
 128                  __METHOD__,
 129                  (is_object($annotations) ? get_class($annotations) : gettype($annotations))
 130              ));
 131          }
 132  
 133          foreach ($annotations as $annotation) {
 134              $this->registerAnnotation($annotation);
 135          }
 136  
 137          return $this;
 138      }
 139  
 140      /**
 141       * Checks if the manager has annotations for a class
 142       *
 143       * @param  string $class
 144       * @return bool
 145       */
 146      public function hasAnnotation($class)
 147      {
 148          if (in_array($class, $this->annotationNames)) {
 149              return true;
 150          }
 151  
 152          if ($this->hasAlias($class)) {
 153              return true;
 154          }
 155  
 156          return false;
 157      }
 158  
 159      /**
 160       * Alias an annotation name
 161       *
 162       * @param  string $alias
 163       * @param  string $class May be either a registered annotation name or another alias
 164       * @throws Exception\InvalidArgumentException
 165       * @return GenericAnnotationParser
 166       */
 167      public function setAlias($alias, $class)
 168      {
 169          if (!in_array($class, $this->annotationNames) && !$this->hasAlias($class)) {
 170              throw new Exception\InvalidArgumentException(sprintf(
 171                  '%s: Cannot alias "%s" to "%s", as class "%s" is not currently a registered annotation or alias',
 172                  __METHOD__,
 173                  $alias,
 174                  $class,
 175                  $class
 176              ));
 177          }
 178  
 179          $alias = $this->normalizeAlias($alias);
 180          $this->aliases[$alias] = $class;
 181  
 182          return $this;
 183      }
 184  
 185      /**
 186       * Normalize an alias name
 187       *
 188       * @param  string $alias
 189       * @return string
 190       */
 191      protected function normalizeAlias($alias)
 192      {
 193          return strtolower(str_replace(array('-', '_', ' ', '\\', '/'), '', $alias));
 194      }
 195  
 196      /**
 197       * Do we have an alias by the provided name?
 198       *
 199       * @param  string $alias
 200       * @return bool
 201       */
 202      protected function hasAlias($alias)
 203      {
 204          $alias = $this->normalizeAlias($alias);
 205  
 206          return (isset($this->aliases[$alias]));
 207      }
 208  
 209      /**
 210       * Resolve an alias to a class name
 211       *
 212       * @param  string $alias
 213       * @return string
 214       */
 215      protected function resolveAlias($alias)
 216      {
 217          do {
 218              $normalized = $this->normalizeAlias($alias);
 219              $class      = $this->aliases[$normalized];
 220          } while ($this->hasAlias($class));
 221  
 222          return $class;
 223      }
 224  }


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