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

GenericAnnotationParser.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 6.13 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\Annotation\Parser;
 011   
 012  use Traversable;
 013  use Zend\Code\Annotation\AnnotationInterface;
 014  use Zend\Code\Exception;
 015  use Zend\EventManager\EventInterface;
 016   
 017  /**
 018   * Generic annotation parser
 019   *
 020   * Expects registration of AnnotationInterface instances. Such instances
 021   * will be passed annotation content to their initialize() method, which
 022   * they are then responsible for parsing.
 023   */
 024  class GenericAnnotationParser implements ParserInterface
 025  {
 026      /**
 027       * @var array
 028       */
 029      protected $aliases = array();
 030   
 031      /**
 032       * @var array
 033       */
 034      protected $annotationNames = array();
 035   
 036      /**
 037       * @var AnnotationInterface[]
 038       */
 039      protected $annotations = array();
 040   
 041      /**
 042       * Listen to onCreateAnnotation, and attempt to return an annotation object
 043       * instance.
 044       *
 045       * If the annotation class or alias is not registered, immediately returns
 046       * false. Otherwise, resolves the class, clones it, and, if any content is
 047       * present, calls {@link AnnotationInterface::initialize()} with the
 048       * content.
 049       *
 050       * @param  EventInterface $e
 051       * @return false|AnnotationInterface
 052       */
 053      public function onCreateAnnotation(EventInterface $e)
 054      {
 055          $class = $e->getParam('class', false);
 056          if (!$class || !$this->hasAnnotation($class)) {
 057              return false;
 058          }
 059   
 060          $content = $e->getParam('content', '');
 061          $content = trim($content, '()');
 062   
 063          if ($this->hasAlias($class)) {
 064              $class = $this->resolveAlias($class);
 065          }
 066   
 067          $index      = array_search($class, $this->annotationNames);
 068          $annotation = $this->annotations[$index];
 069   
 070          $newAnnotation = clone $annotation;
 071          if ($content) {
 072              $newAnnotation->initialize($content);
 073          }
 074   
 075          return $newAnnotation;
 076      }
 077   
 078      /**
 079       * Register annotations
 080       *
 081       * @param  string|AnnotationInterface $annotation String class name of an
 082       *         AnnotationInterface implementation, or actual instance
 083       * @return GenericAnnotationParser
 084       * @throws Exception\InvalidArgumentException
 085       */
 086      public function registerAnnotation($annotation)
 087      {
 088          $class = false;
 089          if (is_string($annotation) && class_exists($annotation)) {
 090              $class      = $annotation;
 091              $annotation = new $annotation();
 092          }
 093   
 094          if (!$annotation instanceof AnnotationInterface) {
 095              throw new Exception\InvalidArgumentException(sprintf(
 096                  '%s: expects an instance of %s\AnnotationInterface; received "%s"',
 097                  __METHOD__,
 098                  __NAMESPACE__,
 099                  (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  }
 225