Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
GenericAnnotationParser.php
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-2016 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 use function array_search;
018 use function class_exists;
019 use function get_class;
020 use function gettype;
021 use function in_array;
022 use function is_array;
023 use function is_object;
024 use function is_string;
025 use function sprintf;
026 use function str_replace;
027 use function strtolower;
028 use function trim;
029
030 /**
031 * Generic annotation parser
032 *
033 * Expects registration of AnnotationInterface instances. Such instances
034 * will be passed annotation content to their initialize() method, which
035 * they are then responsible for parsing.
036 */
037 class GenericAnnotationParser implements ParserInterface
038 {
039 /**
040 * @var array
041 */
042 protected $aliases = [];
043
044 /**
045 * @var array
046 */
047 protected $annotationNames = [];
048
049 /**
050 * @var AnnotationInterface[]
051 */
052 protected $annotations = [];
053
054 /**
055 * Listen to onCreateAnnotation, and attempt to return an annotation object
056 * instance.
057 *
058 * If the annotation class or alias is not registered, immediately returns
059 * false. Otherwise, resolves the class, clones it, and, if any content is
060 * present, calls {@link AnnotationInterface::initialize()} with the
061 * content.
062 *
063 * @param EventInterface $e
064 * @return false|AnnotationInterface
065 */
066 public function onCreateAnnotation(EventInterface $e)
067 {
068 $class = $e->getParam('class', false);
069 if (! $class || ! $this->hasAnnotation($class)) {
070 return false;
071 }
072
073 $content = $e->getParam('content', '');
074 $content = trim($content, '()');
075
076 if ($this->hasAlias($class)) {
077 $class = $this->resolveAlias($class);
078 }
079
080 $index = array_search($class, $this->annotationNames);
081 $annotation = $this->annotations[$index];
082
083 $newAnnotation = clone $annotation;
084 if ($content) {
085 $newAnnotation->initialize($content);
086 }
087
088 return $newAnnotation;
089 }
090
091 /**
092 * Register annotations
093 *
094 * @param string|AnnotationInterface $annotation String class name of an
095 * AnnotationInterface implementation, or actual instance
096 * @return void
097 * @throws Exception\InvalidArgumentException
098 */
099 public function registerAnnotation($annotation)
100 {
101 $class = false;
102 if (is_string($annotation) && class_exists($annotation)) {
103 $class = $annotation;
104 $annotation = new $annotation();
105 }
106
107 if (! $annotation instanceof AnnotationInterface) {
108 throw new Exception\InvalidArgumentException(sprintf(
109 '%s: expects an instance of %s\AnnotationInterface; received "%s"',
110 __METHOD__,
111 __NAMESPACE__,
112 is_object($annotation) ? get_class($annotation) : gettype($annotation)
113 ));
114 }
115
116 $class = $class ?: get_class($annotation);
117
118 if (in_array($class, $this->annotationNames)) {
119 throw new Exception\InvalidArgumentException(sprintf(
120 'An annotation for this class %s already exists',
121 $class
122 ));
123 }
124
125 $this->annotations[] = $annotation;
126 $this->annotationNames[] = $class;
127 }
128
129 /**
130 * Register many annotations at once
131 *
132 * @param array|Traversable $annotations
133 * @throws Exception\InvalidArgumentException
134 * @return GenericAnnotationParser
135 */
136 public function registerAnnotations($annotations)
137 {
138 if (! is_array($annotations) && ! $annotations instanceof Traversable) {
139 throw new Exception\InvalidArgumentException(sprintf(
140 '%s: expects an array or Traversable; received "%s"',
141 __METHOD__,
142 is_object($annotations) ? get_class($annotations) : gettype($annotations)
143 ));
144 }
145
146 foreach ($annotations as $annotation) {
147 $this->registerAnnotation($annotation);
148 }
149
150 return $this;
151 }
152
153 /**
154 * Checks if the manager has annotations for a class
155 *
156 * @param string $class
157 * @return bool
158 */
159 public function hasAnnotation($class)
160 {
161 if (in_array($class, $this->annotationNames)) {
162 return true;
163 }
164
165 if ($this->hasAlias($class)) {
166 return true;
167 }
168
169 return false;
170 }
171
172 /**
173 * Alias an annotation name
174 *
175 * @param string $alias
176 * @param string $class May be either a registered annotation name or another alias
177 * @throws Exception\InvalidArgumentException
178 * @return GenericAnnotationParser
179 */
180 public function setAlias($alias, $class)
181 {
182 if (! in_array($class, $this->annotationNames) && ! $this->hasAlias($class)) {
183 throw new Exception\InvalidArgumentException(sprintf(
184 '%s: Cannot alias "%s" to "%s", as class "%s" is not currently a registered annotation or alias',
185 __METHOD__,
186 $alias,
187 $class,
188 $class
189 ));
190 }
191
192 $alias = $this->normalizeAlias($alias);
193 $this->aliases[$alias] = $class;
194
195 return $this;
196 }
197
198 /**
199 * Normalize an alias name
200 *
201 * @param string $alias
202 * @return string
203 */
204 protected function normalizeAlias($alias)
205 {
206 return strtolower(str_replace(['-', '_', ' ', '\\', '/'], '', $alias));
207 }
208
209 /**
210 * Do we have an alias by the provided name?
211 *
212 * @param string $alias
213 * @return bool
214 */
215 protected function hasAlias($alias)
216 {
217 $alias = $this->normalizeAlias($alias);
218
219 return isset($this->aliases[$alias]);
220 }
221
222 /**
223 * Resolve an alias to a class name
224 *
225 * @param string $alias
226 * @return string
227 */
228 protected function resolveAlias($alias)
229 {
230 do {
231 $normalized = $this->normalizeAlias($alias);
232 $class = $this->aliases[$normalized];
233 } while ($this->hasAlias($class));
234
235 return $class;
236 }
237 }
238