1+ <?php
2+
3+ namespace Doctrine \Common \Annotations ;
4+
5+ use \ReflectionClass , \ReflectionMethod , \ReflectionProperty ;
6+ use Doctrine \Common \Cache \Cache ;
7+
8+ class AnnotationReader
9+ {
10+ private static $ CACHE_SALT = "@<Annot> " ;
11+ private $ _parser ;
12+ private $ _cache ;
13+ private $ _annotations = array ();
14+
15+ public function __construct (Cache $ cache )
16+ {
17+ $ this ->_parser = new Parser ;
18+ $ this ->_cache = $ cache ;
19+ }
20+
21+ public function setDefaultAnnotationNamespace ($ defaultNamespace )
22+ {
23+ $ this ->_parser ->setDefaultAnnotationNamespace ($ defaultNamespace );
24+ }
25+
26+ /**
27+ * Gets the annotations applied to a class.
28+ *
29+ * @param string|ReflectionClass $class The name or ReflectionClass of the class from which
30+ * the class annotations should be read.
31+ * @return array An array of Annotations.
32+ */
33+ public function getClassAnnotations ($ class )
34+ {
35+ if (is_string ($ class )) {
36+ $ className = $ class ;
37+ } else {
38+ $ className = $ class ->getName ();
39+ }
40+
41+ if (isset ($ this ->_annotations [$ className ])) {
42+ return $ this ->_annotations [$ className ];
43+ } else if ($ this ->_cache ->contains ($ className . self ::$ CACHE_SALT )) {
44+ $ this ->_annotations [$ className ] = $ this ->_cacheDriver ->get ($ className . self ::$ CACHE_SALT );
45+ return $ this ->_annotations [$ className ];
46+ }
47+
48+ if (is_string ($ class )) {
49+ $ class = new ReflectionClass ($ className );
50+ }
51+
52+ $ this ->_annotations [$ className ] = $ this ->_parser ->parse ($ class ->getDocComment ());
53+
54+ return $ this ->_annotations [$ className ];
55+ }
56+
57+ public function getClassAnnotation ($ class , $ annotation )
58+ {
59+ $ annotations = $ this ->getClassAnnotations ($ class );
60+ return isset ($ annotations [$ annotation ]) ? $ annotations [$ annotation ] : null ;
61+ }
62+
63+ /**
64+ * Gets the annotations applied to a property.
65+ *
66+ * @param string|ReflectionClass $class The name or ReflectionClass of the class that owns the property.
67+ * @param string|ReflectionProperty $property The name or ReflectionProperty of the property
68+ * from which the annotations should be read.
69+ * @return array An array of Annotations.
70+ */
71+ public function getPropertyAnnotations ($ class , $ property )
72+ {
73+ $ className = is_string ($ class ) ? $ class : $ class ->getName ();
74+ if (is_string ($ property )) {
75+ $ propertyName = $ className . '$ ' . $ property ;
76+ } else {
77+ $ propertyName = $ className . '$ ' . $ property ->getName ();
78+ }
79+
80+ if (isset ($ this ->_annotations [$ propertyName ])) {
81+ return $ this ->_annotations [$ propertyName ];
82+ } else if ($ this ->_cache ->contains ($ propertyName . self ::$ CACHE_SALT )) {
83+ $ this ->_annotations [$ propertyName ] = $ this ->_cacheDriver ->get ($ propertyName . self ::$ CACHE_SALT );
84+ return $ this ->_annotations [$ propertyName ];
85+ }
86+
87+ if (is_string ($ property )) {
88+ $ property = new ReflectionProperty ($ className , $ property );
89+ }
90+
91+ $ this ->_annotations [$ propertyName ] = $ this ->_parser ->parse ($ property ->getDocComment ());
92+
93+ return $ this ->_annotations [$ propertyName ];
94+ }
95+
96+ public function getPropertyAnnotation ($ class , $ property , $ annotation )
97+ {
98+ $ annotations = $ this ->getPropertyAnnotations ($ class , $ property );
99+ return isset ($ annotations [$ annotation ]) ? $ annotations [$ annotation ] : null ;
100+ }
101+
102+ /**
103+ * Gets the annotations applied to a method.
104+ *
105+ * @param string|ReflectionClass $class The name or ReflectionClass of the class that owns the method.
106+ * @param string|ReflectionMethod $property The name or ReflectionMethod of the method from which
107+ * the annotations should be read.
108+ * @return array An array of Annotations.
109+ */
110+ public function getMethodAnnotations ($ class , $ method )
111+ {
112+ $ className = is_string ($ class ) ? $ class : $ class ->getName ();
113+ if (is_string ($ method )) {
114+ $ methodName = $ className . '# ' . $ method ;
115+ } else {
116+ $ methodName = $ className . '# ' . $ method ->getName ();
117+ }
118+
119+ if (isset ($ this ->_annotations [$ methodName ])) {
120+ return $ this ->_annotations [$ methodName ];
121+ } else if ($ this ->_cache ->contains ($ methodName . self ::$ CACHE_SALT )) {
122+ $ this ->_annotations [$ methodName ] = $ this ->_cacheDriver ->get ($ methodName . self ::$ CACHE_SALT );
123+ return $ this ->_annotations [$ methodName ];
124+ }
125+
126+ if (is_string ($ method )) {
127+ $ method = new ReflectionMethod ($ className , $ method );
128+ }
129+
130+ $ this ->_annotations [$ methodName ] = $ this ->_parser ->parse ($ method ->getDocComment ());
131+
132+ return $ this ->_annotations [$ methodName ];
133+ }
134+
135+ public function getMethodAnnotation ($ class , $ method , $ annotation )
136+ {
137+ $ annotations = $ this ->getMethodAnnotations ($ class , $ method );
138+ return isset ($ annotations [$ annotation ]) ? $ annotations [$ annotation ] : null ;
139+ }
140+ }
0 commit comments