forked from jsuchal/addendum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.php
406 lines (339 loc) · 12.4 KB
/
annotations.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/**
* Addendum PHP Reflection Annotations
* http://code.google.com/p/addendum/
*
* Copyright (C) 2006-2009 Jan "johno Suchal <[email protected]>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
require_once(dirname(__FILE__).'/annotations/annotation_parser.php');
class Annotation {
public $value;
private static $creationStack = array();
public final function __construct($data = array(), $target = false) {
$reflection = new ReflectionClass($this);
$class = $reflection->getName();
if(isset(self::$creationStack[$class])) {
trigger_error("Circular annotation reference on '$class'", E_USER_ERROR);
return;
}
self::$creationStack[$class] = true;
foreach($data as $key => $value) {
if($reflection->hasProperty($key)) {
$this->$key = $value;
} else {
trigger_error("Property '$key' not defined for annotation '$class'");
}
}
$this->checkTargetConstraints($target);
$this->checkConstraints($target);
unset(self::$creationStack[$class]);
}
private function checkTargetConstraints($target) {
$reflection = new ReflectionAnnotatedClass($this);
if($reflection->hasAnnotation('Target')) {
$value = $reflection->getAnnotation('Target')->value;
$values = is_array($value) ? $value : array($value);
foreach($values as $value) {
if($value == 'class' && $target instanceof ReflectionClass) return;
if($value == 'method' && $target instanceof ReflectionMethod) return;
if($value == 'property' && $target instanceof ReflectionProperty) return;
if($value == 'nested' && $target === false) return;
}
if($target === false) {
trigger_error("Annotation '".get_class($this)."' nesting not allowed", E_USER_ERROR);
} else {
trigger_error("Annotation '".get_class($this)."' not allowed on ".$this->createName($target), E_USER_ERROR);
}
}
}
private function createName($target) {
if($target instanceof ReflectionMethod) {
return $target->getDeclaringClass()->getName().'::'.$target->getName();
} elseif($target instanceof ReflectionProperty) {
return $target->getDeclaringClass()->getName().'::$'.$target->getName();
} else {
return $target->getName();
}
}
protected function checkConstraints($target) {}
}
class AnnotationsCollection {
private $annotations;
public function __construct($annotations) {
$this->annotations = $annotations;
}
public function hasAnnotation($class) {
$class = Addendum::resolveClassName($class);
return isset($this->annotations[$class]);
}
public function getAnnotation($class) {
$class = Addendum::resolveClassName($class);
return isset($this->annotations[$class]) ? end($this->annotations[$class]) : false;
}
public function getAnnotations() {
$result = array();
foreach($this->annotations as $instances) {
$result[] = end($instances);
}
return $result;
}
public function getAllAnnotations($restriction = false) {
$restriction = Addendum::resolveClassName($restriction);
$result = array();
foreach($this->annotations as $class => $instances) {
if(!$restriction || $restriction == $class) {
$result = array_merge($result, $instances);
}
}
return $result;
}
}
class Annotation_Target extends Annotation {}
class AnnotationsBuilder {
private static $cache = array();
public function build($targetReflection) {
$data = $this->parse($targetReflection);
$annotations = array();
foreach($data as $class => $parameters) {
foreach($parameters as $params) {
$annotation = $this->instantiateAnnotation($class, $params, $targetReflection);
if($annotation !== false) {
$annotations[get_class($annotation)][] = $annotation;
}
}
}
return new AnnotationsCollection($annotations);
}
public function instantiateAnnotation($class, $parameters, $targetReflection = false) {
$class = Addendum::resolveClassName($class);
if(is_subclass_of($class, 'Annotation') && !Addendum::ignores($class) || $class == 'Annotation') {
$annotationReflection = new ReflectionClass($class);
return $annotationReflection->newInstance($parameters, $targetReflection);
}
return false;
}
private function parse($reflection) {
$key = $this->createName($reflection);
if(!isset(self::$cache[$key])) {
$parser = new AnnotationsMatcher;
$parser->matches($this->getDocComment($reflection), $data);
self::$cache[$key] = $data;
}
return self::$cache[$key];
}
private function createName($target) {
if($target instanceof ReflectionMethod) {
return $target->getDeclaringClass()->getName().'::'.$target->getName();
} elseif($target instanceof ReflectionProperty) {
return $target->getDeclaringClass()->getName().'::$'.$target->getName();
} else {
return $target->getName();
}
}
protected function getDocComment($reflection) {
return Addendum::getDocComment($reflection);
}
public static function clearCache() {
self::$cache = array();
}
}
class ReflectionAnnotatedClass extends ReflectionClass {
private $annotations;
public function __construct($class) {
parent::__construct($class);
$this->annotations = $this->createAnnotationBuilder()->build($this);
}
public function hasAnnotation($class) {
return $this->annotations->hasAnnotation($class);
}
public function getAnnotation($annotation) {
return $this->annotations->getAnnotation($annotation);
}
public function getAnnotations() {
return $this->annotations->getAnnotations();
}
public function getAllAnnotations($restriction = false) {
return $this->annotations->getAllAnnotations($restriction);
}
public function getConstructor() {
return $this->createReflectionAnnotatedMethod(parent::getConstructor());
}
public function getMethod($name) {
return $this->createReflectionAnnotatedMethod(parent::getMethod($name));
}
public function getMethods($filter = -1) {
$result = array();
foreach(parent::getMethods($filter) as $method) {
$result[] = $this->createReflectionAnnotatedMethod($method);
}
return $result;
}
public function getProperty($name) {
return $this->createReflectionAnnotatedProperty(parent::getProperty($name));
}
public function getProperties($filter = -1) {
$result = array();
foreach(parent::getProperties($filter) as $property) {
$result[] = $this->createReflectionAnnotatedProperty($property);
}
return $result;
}
public function getInterfaces() {
$result = array();
foreach(parent::getInterfaces() as $interface) {
$result[] = $this->createReflectionAnnotatedClass($interface);
}
return $result;
}
public function getParentClass() {
$class = parent::getParentClass();
return $this->createReflectionAnnotatedClass($class);
}
protected function createAnnotationBuilder() {
return new AnnotationsBuilder();
}
private function createReflectionAnnotatedClass($class) {
return ($class !== false) ? new ReflectionAnnotatedClass($class->getName()) : false;
}
private function createReflectionAnnotatedMethod($method) {
return ($method !== null) ? new ReflectionAnnotatedMethod($this->getName(), $method->getName()) : null;
}
private function createReflectionAnnotatedProperty($property) {
return ($property !== null) ? new ReflectionAnnotatedProperty($this->getName(), $property->getName()) : null;
}
}
class ReflectionAnnotatedMethod extends ReflectionMethod {
private $annotations;
public function __construct($class, $name) {
parent::__construct($class, $name);
$this->annotations = $this->createAnnotationBuilder()->build($this);
}
public function hasAnnotation($class) {
return $this->annotations->hasAnnotation($class);
}
public function getAnnotation($annotation) {
return $this->annotations->getAnnotation($annotation);
}
public function getAnnotations() {
return $this->annotations->getAnnotations();
}
public function getAllAnnotations($restriction = false) {
return $this->annotations->getAllAnnotations($restriction);
}
public function getDeclaringClass() {
$class = parent::getDeclaringClass();
return new ReflectionAnnotatedClass($class->getName());
}
protected function createAnnotationBuilder() {
return new AnnotationsBuilder();
}
}
class ReflectionAnnotatedProperty extends ReflectionProperty {
private $annotations;
public function __construct($class, $name) {
parent::__construct($class, $name);
$this->annotations = $this->createAnnotationBuilder()->build($this);
}
public function hasAnnotation($class) {
return $this->annotations->hasAnnotation($class);
}
public function getAnnotation($annotation) {
return $this->annotations->getAnnotation($annotation);
}
public function getAnnotations() {
return $this->annotations->getAnnotations();
}
public function getAllAnnotations($restriction = false) {
return $this->annotations->getAllAnnotations($restriction);
}
public function getDeclaringClass() {
$class = parent::getDeclaringClass();
return new ReflectionAnnotatedClass($class->getName());
}
protected function createAnnotationBuilder() {
return new AnnotationsBuilder();
}
}
class Addendum {
private static $rawMode;
private static $ignore;
private static $classnames = array();
private static $annotations = false;
public static function getDocComment($reflection) {
if(self::checkRawDocCommentParsingNeeded()) {
$docComment = new DocComment();
return $docComment->get($reflection);
} else {
return $reflection->getDocComment();
}
}
/** Raw mode test */
private static function checkRawDocCommentParsingNeeded() {
if(self::$rawMode === null) {
$reflection = new ReflectionClass('Addendum');
$method = $reflection->getMethod('checkRawDocCommentParsingNeeded');
self::setRawMode($method->getDocComment() === false);
}
return self::$rawMode;
}
public static function setRawMode($enabled = true) {
if($enabled) {
require_once(dirname(__FILE__).'/annotations/doc_comment.php');
}
self::$rawMode = $enabled;
}
public static function resetIgnoredAnnotations() {
self::$ignore = array();
}
public static function ignores($class) {
return isset(self::$ignore[$class]);
}
public static function ignore() {
foreach(func_get_args() as $class) {
self::$ignore[$class] = true;
}
}
public static function resolveClassName($class) {
if(isset(self::$classnames[$class])) return self::$classnames[$class];
$matching = array();
foreach(self::getDeclaredAnnotations() as $declared) {
if($declared == $class) {
$matching[] = $declared;
} else {
$pos = strrpos($declared, "_$class");
if($pos !== false && ($pos + strlen($class) == strlen($declared) - 1)) {
$matching[] = $declared;
}
}
}
$result = null;
switch(count($matching)) {
case 0: $result = $class; break;
case 1: $result = $matching[0]; break;
default: trigger_error("Cannot resolve class name for '$class'. Possible matches: " . join(', ', $matching), E_USER_ERROR);
}
self::$classnames[$class] = $result;
return $result;
}
private static function getDeclaredAnnotations() {
if(!self::$annotations) {
self::$annotations = array();
foreach(get_declared_classes() as $class) {
if(is_subclass_of($class, 'Annotation') || $class == 'Annotation') self::$annotations[] = $class;
}
}
return self::$annotations;
}
}
?>