|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Go! AOP framework |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2011, Lisachenko Alexander <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Go\Instrument; |
|
12
|
|
|
|
|
13
|
|
|
use TokenReflection\ReflectionClass as ParsedReflectionClass; |
|
14
|
|
|
use TokenReflection\ReflectionMethod as ParsedReflectionMethod; |
|
15
|
|
|
use TokenReflection\ReflectionProperty as ParsedReflectionProperty; |
|
16
|
|
|
use Doctrine\Common\Annotations\DocParser; |
|
17
|
|
|
use Doctrine\Common\Annotations\Annotation\Target; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Simple Raw Annotation Reader. |
|
21
|
|
|
* |
|
22
|
|
|
* This annotation reader is intended to be used in projects where you have |
|
23
|
|
|
* full-control over all annotations that are available. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 2.2 |
|
26
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
|
27
|
|
|
* @author Fabio B. Silva <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class RawAnnotationReader |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var DocParser |
|
33
|
|
|
*/ |
|
34
|
|
|
private $parser; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* Initializes a new SimpleAnnotationReader. |
|
40
|
|
|
*/ |
|
41
|
30 |
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
30 |
|
$this->parser = new DocParser(); |
|
44
|
30 |
|
$this->parser->setIgnoreNotImportedAnnotations(true); |
|
45
|
30 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set imports for annotations |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $imports |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setImports(array $imports) |
|
53
|
|
|
{ |
|
54
|
|
|
$convertedImports = []; |
|
55
|
|
|
foreach ($imports as $aliasName=>$fullName) { |
|
|
|
|
|
|
56
|
|
|
$convertedImports[strtolower($aliasName)] = $fullName; |
|
57
|
|
|
} |
|
58
|
|
|
$this->parser->setImports($convertedImports); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Adds a namespace in which we will look for annotations. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $namespace |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addNamespace($namespace) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->parser->addNamespace($namespace); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Gets the annotations applied to a class. |
|
73
|
|
|
* |
|
74
|
|
|
* @param ParsedReflectionClass $class The ReflectionClass of the class from which |
|
75
|
|
|
* the class annotations should be read. |
|
76
|
|
|
* @return array An array of Annotations. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getClassAnnotations(ParsedReflectionClass $class) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->parser->setTarget(Target::TARGET_CLASS); |
|
81
|
|
|
|
|
82
|
|
|
return $this->parser->parse($class->getDocComment(), 'class '.$class->getName()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Gets the annotations applied to a method. |
|
87
|
|
|
* |
|
88
|
|
|
* @param ParsedReflectionMethod $method The ReflectionMethod of the method from which |
|
89
|
|
|
* the annotations should be read. |
|
90
|
|
|
* @return array An array of Annotations. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getMethodAnnotations(ParsedReflectionMethod $method) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->parser->setTarget(Target::TARGET_METHOD); |
|
95
|
|
|
|
|
96
|
|
|
return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Gets the annotations applied to a property. |
|
101
|
|
|
* |
|
102
|
|
|
* @param ParsedReflectionProperty $property The ReflectionProperty of the property |
|
103
|
|
|
* from which the annotations should be read. |
|
104
|
|
|
* @return array An array of Annotations. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getPropertyAnnotations(ParsedReflectionProperty $property) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->parser->setTarget(Target::TARGET_PROPERTY); |
|
109
|
|
|
|
|
110
|
|
|
return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets a class annotation. |
|
115
|
|
|
* |
|
116
|
|
|
* @param ParsedReflectionClass $class The ReflectionClass of the class from which |
|
117
|
|
|
* the class annotations should be read. |
|
118
|
|
|
* @param string $annotationName The name of the annotation. |
|
119
|
|
|
* @return The Annotation or NULL, if the requested annotation does not exist. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getClassAnnotation(ParsedReflectionClass $class, $annotationName) |
|
122
|
|
|
{ |
|
123
|
|
|
foreach ($this->getClassAnnotations($class) as $annot) { |
|
124
|
|
|
if ($annot instanceof $annotationName) { |
|
125
|
|
|
return $annot; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Gets a method annotation. |
|
134
|
|
|
* |
|
135
|
|
|
* @param ParsedReflectionMethod $method |
|
136
|
|
|
* @param string $annotationName The name of the annotation. |
|
137
|
|
|
* @return The Annotation or NULL, if the requested annotation does not exist. |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getMethodAnnotation(ParsedReflectionMethod $method, $annotationName) |
|
140
|
|
|
{ |
|
141
|
|
|
foreach ($this->getMethodAnnotations($method) as $annot) { |
|
142
|
|
|
if ($annot instanceof $annotationName) { |
|
143
|
|
|
return $annot; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Gets a property annotation. |
|
152
|
|
|
* |
|
153
|
|
|
* @param ParsedReflectionProperty $property |
|
154
|
|
|
* @param string $annotationName The name of the annotation. |
|
155
|
|
|
* @return The Annotation or NULL, if the requested annotation does not exist. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getPropertyAnnotation(ParsedReflectionProperty $property, $annotationName) |
|
158
|
|
|
{ |
|
159
|
|
|
foreach ($this->getPropertyAnnotations($property) as $annot) { |
|
160
|
|
|
if ($annot instanceof $annotationName) { |
|
161
|
|
|
return $annot; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return null; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|