@@ -38,7 +38,7 @@ class AnnotationLoader
3838 /** @var string[] */
3939 private $ resources = [];
4040
41- /** @var callable(string[])|null */
41+ /** @var callable(string[]) */
4242 private $ classLoader ;
4343
4444 /**
@@ -51,7 +51,7 @@ public function __construct(ReaderInterface $reader = null, callable $classLoade
5151 }
5252
5353 $ this ->reader = $ reader ;
54- $ this ->classLoader = $ classLoader ;
54+ $ this ->classLoader = $ classLoader ?? [ self ::class, ' findClasses ' ] ;
5555 }
5656
5757 /**
@@ -84,39 +84,30 @@ public function resource(string ...$resources): void
8484 *
8585 * @param string ...$listener the name of class name for registered lister
8686 * annotation/attribute class name or listener's aliased name
87+ *
88+ * @return mixed
8789 */
8890 public function load (string ...$ listener )
8991 {
90- $ loadedAnnotation = [];
91-
92- if (empty ($ listener )) {
93- if (empty ($ this ->loadedListeners )) {
94- foreach ($ this ->listeners as $ name => $ value ) {
95- $ this ->loadedListeners [$ name ] = $ value ->load ($ this ->build (...$ value ->getAnnotations ()));
96- }
97- }
98-
99- $ loadedAnnotation = $ this ->loadedListeners ;
100- } else {
101- foreach ($ listener as $ name ) {
102- $ name = $ this ->aliases [$ name ] ?? $ name ;
103- $ loaded = ($ this ->loadedListeners [$ name ] ?? $ this ->loadedAttributes [$ name ] ?? null );
92+ $ loaded = [];
10493
105- if (null === $ loaded ) {
106- $ l = $ this ->listeners [$ name ] ?? null ;
94+ foreach (($ listener ?: $ this ->listeners ) as $ name => $ value ) {
95+ if (\is_int ($ name )) {
96+ $ name = $ this ->aliases [$ value ] ?? $ value ;
10797
108- if ($ l instanceof ListenerInterface) {
109- $ this ->loadedListeners [$ name ] = $ l ->load ($ this ->build (...$ l ->getAnnotations ()));
110- }
111-
112- $ loaded = $ this ->loadedListeners [$ name ] ?? ($ this ->loadedAttributes [$ name ] = $ this ->build ($ name ));
98+ if (!isset ($ this ->listeners [$ name ])) {
99+ $ loaded [$ name ] = $ this ->loadedAttributes [$ name ] ?? ($ this ->loadedAttributes [$ name ] = $ this ->build ($ name ));
100+ continue ;
113101 }
114102
115- $ loadedAnnotation [$ name ] = $ loaded ;
103+ if (!isset ($ this ->loadedListeners [$ name ])) {
104+ $ value = $ this ->listeners [$ name ];
105+ }
116106 }
107+ $ loaded [$ name ] = $ this ->loadedListeners [$ name ] ?? $ this ->loadedListeners [$ name ] = $ value ->load ($ this ->build (...$ value ->getAnnotations ()));
117108 }
118109
119- return 1 === \count ($ loadedAnnotation ) ? \current ($ loadedAnnotation ) : $ loadedAnnotation ;
110+ return 1 === \count ($ loaded ) ? \current ($ loaded ) : $ loaded ;
120111 }
121112
122113 /**
@@ -135,21 +126,14 @@ protected function build(string ...$annotationClass): array
135126 $ iterator = new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator ($ resource , \FilesystemIterator::CURRENT_AS_PATHNAME ));
136127 $ files = new \RegexIterator ($ iterator , '/\.php$/ ' );
137128
138- if ( \iterator_count ( $ files ) > 0 ) {
139- $ values = ( $ this ->classLoader ?? [ $ this , ' findClasses ' ])( $ files );
129+ foreach (( $ this -> classLoader )( $ files ) as $ class ) {
130+ $ classes = $ this ->fetchClassAnnotation ( $ class , $ annotationClass );
140131
141- foreach ($ values as $ class ) {
142- $ classes = $ this ->fetchClassAnnotation ($ class , $ annotationClass );
143-
144- if (!empty ($ classes )) {
145- $ annotations [] = $ classes ;
146- }
132+ if (!empty ($ classes )) {
133+ $ annotations [] = $ classes ;
147134 }
148135 }
149- continue ;
150- }
151-
152- if (\function_exists ($ resource )) {
136+ } elseif (\function_exists ($ resource )) {
153137 $ values = $ this ->fetchFunctionAnnotation (new \ReflectionFunction ($ resource ), $ annotationClass );
154138 } elseif (\class_exists ($ resource )) {
155139 $ values = $ this ->fetchClassAnnotation ($ resource , $ annotationClass );
@@ -233,20 +217,14 @@ private function fetchClassAnnotation(string $resource, array $annotationClass):
233217 $ methods [] = $ method ;
234218 ++$ classRefCount ;
235219 }
236-
237- continue ;
238- }
239-
240- if (empty ($ annotations = $ this ->getAnnotations ($ reflection , $ annotationClass ))) {
241- continue ;
242- }
243-
244- if ($ reflection instanceof \ReflectionProperty) {
245- $ properties [] = ['attributes ' => $ annotations , 'type ' => $ reflection ];
246- ++$ classRefCount ;
247- } elseif ($ reflection instanceof \ReflectionClassConstant) {
248- $ constants [] = ['attributes ' => $ annotations , 'type ' => $ reflection ];
249- ++$ classRefCount ;
220+ } elseif (!empty ($ annotations = $ this ->getAnnotations ($ reflection , $ annotationClass ))) {
221+ if ($ reflection instanceof \ReflectionProperty) {
222+ $ properties [] = ['attributes ' => $ annotations , 'type ' => $ reflection ];
223+ ++$ classRefCount ;
224+ } elseif ($ reflection instanceof \ReflectionClassConstant) {
225+ $ constants [] = ['attributes ' => $ annotations , 'type ' => $ reflection ];
226+ ++$ classRefCount ;
227+ }
250228 }
251229 }
252230
0 commit comments