1717
1818namespace Biurad \Annotations ;
1919
20- use Doctrine \Common \Annotations \Reader as AnnotationReader ;
21- use Doctrine \Common \Annotations \SimpleAnnotationReader ;
2220use FilesystemIterator ;
2321use RecursiveDirectoryIterator ;
2422use RecursiveIteratorIterator ;
2523use ReflectionClass ;
24+ use ReflectionClassConstant ;
2625use ReflectionMethod ;
26+ use ReflectionParameter ;
2727use ReflectionProperty ;
2828use Reflector ;
2929use RegexIterator ;
30+ use Spiral \Attributes \ReaderInterface ;
3031
3132class AnnotationLoader implements LoaderInterface
3233{
33- /** @var null|AnnotationReader */
34+ /** @var ReaderInterface */
3435 private $ annotation ;
3536
3637 /** @var ListenerInterface[] */
@@ -40,15 +41,11 @@ class AnnotationLoader implements LoaderInterface
4041 private $ resources = [];
4142
4243 /**
43- * @param null|AnnotationReader $reader
44+ * @param ReaderInterface $reader
4445 */
45- public function __construct (? AnnotationReader $ reader = null )
46+ public function __construct (ReaderInterface $ reader )
4647 {
4748 $ this ->annotation = $ reader ;
48-
49- if (null === $ reader && \interface_exists (AnnotationReader::class)) {
50- $ this ->annotation = new SimpleAnnotationReader ();
51- }
5249 }
5350
5451 /**
@@ -57,10 +54,6 @@ public function __construct(?AnnotationReader $reader = null)
5754 public function attachListener (ListenerInterface ...$ listeners ): void
5855 {
5956 foreach ($ listeners as $ listener ) {
60- if ($ this ->annotation instanceof SimpleAnnotationReader) {
61- $ this ->annotation ->addNamespace ($ listener ->getAnnotation ());
62- }
63-
6457 $ this ->listeners [] = $ listener ;
6558 }
6659 }
@@ -89,15 +82,9 @@ public function load(): iterable
8982
9083 continue ;
9184 }
92-
93- if (!\file_exists ($ resource ) || \is_dir ($ resource )) {
94- continue ;
95- }
96-
97- (function () use ($ resource ): void {
98- require $ resource ;
99- })->call ($ listener ->getBinding ());
10085 }
86+
87+ //TODO: Read annotations from functions ...
10188 }
10289
10390 foreach ($ this ->listeners as $ listener ) {
@@ -110,7 +97,8 @@ public function load(): iterable
11097 /**
11198 * Finds annotations in the given resource
11299 *
113- * @param string $resource
100+ * @param string $resource
101+ * @param ListenerInterface $listener
114102 *
115103 * @return array<string,array<string,mixed>>
116104 */
@@ -139,7 +127,12 @@ private function findAnnotations(string $resource, ListenerInterface $listener):
139127 $ annotations [$ className ]['class ' ] = $ annotation ;
140128 }
141129
142- $ reflections = \array_merge ($ classReflection ->getMethods (), $ classReflection ->getProperties ());
130+ // Reflections belonging to class object.
131+ $ reflections = \array_merge (
132+ $ classReflection ->getMethods (),
133+ $ classReflection ->getProperties (),
134+ $ classReflection ->getConstants ()
135+ );
143136
144137 foreach ($ reflections as $ reflection ) {
145138 if ($ reflection instanceof ReflectionMethod && $ reflection ->isAbstract ()) {
@@ -150,6 +143,16 @@ private function findAnnotations(string $resource, ListenerInterface $listener):
150143 if ($ reflection instanceof ReflectionMethod) {
151144 $ annotations [$ className ]['method ' ][] = [$ reflection , $ annotation ];
152145
146+ foreach ($ this ->getMethodParameter ($ reflection ->getParameters (), $ listener ) as $ parameter ) {
147+ $ annotations [$ className ]['method_property ' ][] = $ parameter ;
148+ }
149+
150+ continue ;
151+ }
152+
153+ if ($ reflection instanceof ReflectionClassConstant) {
154+ $ annotations [$ className ]['constant ' ][] = [$ reflection , $ annotation ];
155+
153156 continue ;
154157 }
155158
@@ -164,7 +167,7 @@ private function findAnnotations(string $resource, ListenerInterface $listener):
164167 }
165168
166169 /**
167- * @param ReflectionClass|ReflectionMethod|ReflectionProperty $reflection
170+ * @param Reflector $reflection
168171 *
169172 * @return iterable<object>
170173 */
@@ -173,31 +176,29 @@ private function getAnnotations(Reflector $reflection, ListenerInterface $listen
173176 $ annotationClass = $ listener ->getAnnotation ();
174177 $ annotations = [];
175178
176- if (\PHP_VERSION_ID >= 80000 ) {
177- foreach ($ reflection ->getAttributes ($ annotationClass ) as $ attribute ) {
178- yield $ attribute ->newInstance ();
179- }
180- }
181-
182- if (null === $ this ->annotation ) {
183- return ;
184- }
185-
186179 switch (true ) {
187180 case $ reflection instanceof ReflectionClass:
188- $ annotations = $ this ->annotation ->getClassAnnotations ($ reflection );
181+ $ annotations = $ this ->annotation ->getClassMetadata ($ reflection );
189182
190183 break ;
191184
192185 case $ reflection instanceof ReflectionMethod:
193- $ annotations = $ this ->annotation ->getMethodAnnotations ($ reflection );
186+ $ annotations = $ this ->annotation ->getFunctionMetadata ($ reflection );
194187
195188 break ;
196189
197190 case $ reflection instanceof ReflectionProperty:
198- $ annotations = $ this ->annotation ->getPropertyAnnotations ($ reflection );
191+ $ annotations = $ this ->annotation ->getPropertyMetadata ($ reflection );
192+
193+ break ;
194+
195+ case $ reflection instanceof ReflectionClassConstant:
196+ $ annotations = $ this ->annotation ->getConstantMetadata ($ reflection );
199197
200198 break ;
199+
200+ case $ reflection instanceof ReflectionParameter:
201+ $ annotations = $ this ->annotation ->getParameterMetadata ($ reflection );
201202 }
202203
203204 foreach ($ annotations as $ annotation ) {
@@ -207,6 +208,25 @@ private function getAnnotations(Reflector $reflection, ListenerInterface $listen
207208 }
208209 }
209210
211+ /**
212+ * @param array $parameters
213+ * @param ListenerInterface $listener
214+ *
215+ * @return iterable<object>
216+ */
217+ private function getMethodParameter (array $ parameters , ListenerInterface $ listener ): iterable
218+ {
219+ foreach ($ listener ->getArguments () as $ name ) {
220+ foreach ($ parameters as $ parameter ) {
221+ if ($ parameter ->getName () === $ name ) {
222+ foreach ($ this ->getAnnotations ($ parameter , $ listener ) as $ annotation ) {
223+ yield [$ parameter , $ annotation ];
224+ }
225+ }
226+ }
227+ }
228+ }
229+
210230 /**
211231 * Finds classes in the given resource directory
212232 *
0 commit comments