1818namespace Biurad \Annotations \Tests \Fixtures ;
1919
2020use Biurad \Annotations \ListenerInterface ;
21+ use Biurad \Annotations \Locate \Annotation ;
22+ use Biurad \Annotations \Locate \Class_ ;
23+ use Biurad \Annotations \Locate \Method ;
2124
2225class SampleListener implements ListenerInterface
2326{
@@ -29,14 +32,6 @@ public function __construct(?SampleCollector $collector = null)
2932 $ this ->collector = $ collector ?? new SampleCollector ();
3033 }
3134
32- /**
33- * {@inheritdoc}
34- */
35- public function getArguments (): array
36- {
37- return ['parameter ' ];
38- }
39-
4035 /**
4136 * {@inheritdoc}
4237 */
@@ -48,72 +43,65 @@ public function getAnnotation(): string
4843 /**
4944 * {@inheritdoc}
5045 */
51- public function onAnnotation (array $ annotations ): SampleCollector
46+ public function load (array $ annotations ): SampleCollector
5247 {
53- foreach ($ annotations as $ class => $ collection ) {
54- $ reflections = \array_merge (
55- $ collection ['method ' ] ?? [],
56- $ collection ['property ' ] ?? [],
57- $ collection ['constant ' ] ?? [],
58- $ collection ['method_property ' ] ?? []
59- );
48+ foreach ($ annotations as $ annotation ) {
49+ if ($ annotation instanceof Class_) {
50+ $ attributes = \array_merge ($ annotation ->methods , $ annotation ->properties , $ annotation ->constants );
51+
52+ if ([] === $ attributes ) {
53+ $ this ->addSample (null , $ annotation );
54+
55+ continue ;
56+ }
6057
61- if (!empty ($ reflections )) {
62- $ this ->addSampleGroup ($ collection ['class ' ] ?? [], $ reflections );
58+ foreach ($ attributes as $ attribute ) {
59+ $ this ->addSample ($ annotation ->getAnnotation (), $ attribute );
60+
61+ if ($ attribute instanceof Method) {
62+ foreach ($ attribute ->parameters as $ parameter ) {
63+ $ this ->addSample ($ annotation ->getAnnotation (), $ parameter );
64+ }
65+ }
66+ }
6367
6468 continue ;
6569 }
6670
67- foreach ($ collection ['class ' ] ?? [] as $ annotation ) {
68- $ this ->addSample ($ annotation , $ class );
71+ $ this ->addSample (null , $ annotation );
72+
73+ foreach ($ annotation ->parameters as $ parameter ) {
74+ $ this ->addSample (null , $ parameter );
6975 }
7076 }
7177
7278 return $ this ->collector ;
7379 }
7480
7581 /**
76- * @param Sample $annotation
77- * @param \Reflector|string $handler
78- * @param null|Sample $group
82+ * @param Sample[]|Sample|null $attribute
7983 */
80- private function addSample (Sample $ annotation , $ handler , ? Sample $ group = null ): void
84+ private function addSample ($ attribute , Annotation $ listener ): void
8185 {
82- $ name = $ annotation ->getName ();
83- $ priority = $ annotation ->getPriority ();
86+ if (is_array ($ attribute ) && [] !== $ attribute ) {
87+ foreach ($ attribute as $ annotation ) {
88+ $ this ->addSample ($ annotation , $ listener );
89+ }
8490
85- if (null !== $ group ) {
86- $ name = $ group ->getName () . '_ ' . $ name ;
87- $ priority += $ group ->getPriority ();
91+ return ;
8892 }
8993
90- $ this ->collector ->add ($ name , $ priority , $ handler );
91- }
94+ /** @var Sample $annotated */
95+ foreach ($ listener ->getAnnotation () as $ annotated ) {
96+ $ name = $ annotated ->getName ();
97+ $ priority = $ annotated ->getPriority ();
9298
93- /**
94- * @param Sample[] $groups
95- * @param mixed[] $collection
96- */
97- private function addSampleGroup (array $ groups , array $ collection ): void
98- {
99- $ handleCollection = function (array $ collection , ?Sample $ group = null ): void {
100- /**
101- * @var \ReflectionMethod|\ReflectionProperty $reflector
102- * @var Sample $annotation
103- */
104- foreach ($ collection as [$ reflector , $ annotation ]) {
105- $ this ->addSample ($ annotation , $ reflector , $ group );
99+ if ($ attribute instanceof Sample) {
100+ $ name = $ attribute ->getName () . '_ ' . $ name ;
101+ $ priority += $ attribute ->getPriority ();
106102 }
107- };
108-
109- if (empty ($ groups )) {
110- $ handleCollection ($ collection );
111-
112- return ;
113- }
114103
115- foreach ($ groups as $ group ) {
116- $ handleCollection ($ collection , $ group );
104+ $ this ->collector ->add ($ name , $ priority , $ listener ->getReflection ());
117105 }
118106 }
119107}
0 commit comments