Skip to content

Commit e35e9bd

Browse files
Updated implementation for annotations/attributes listener
1 parent f35d1b1 commit e35e9bd

File tree

3 files changed

+45
-81
lines changed

3 files changed

+45
-81
lines changed

src/AnnotationLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function build(): void
102102
}
103103
}
104104

105-
$found = $listener->onAnnotation($listenerAnnotations);
105+
$found = $listener->load($listenerAnnotations);
106106

107107
if (null !== $found) {
108108
$this->annotations[] = $found;

src/ListenerInterface.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,14 @@ interface ListenerInterface
2222
/**
2323
* This method utilises found annotations and return collector.
2424
*
25-
* The array that is received contains eg:
26-
* ```php
27-
* $annotations = [
28-
* ClassName::class => [
29-
* 'class' => [$annotation, ...],
30-
* 'methods' => [[$reflection, $annotation], ..],
31-
* 'property' => [[$reflection, $annotation], ..],
32-
* 'constant' => [[$reflection, $annotation], ..],
33-
* 'method_propert' => [[$reflection, $annotation], ..],
34-
* ],
35-
* ...
36-
* ];
37-
* ```
38-
*
39-
* @param array<string,array<string,mixed>> $annotations
25+
* @param Locate\Class_[]|Locate\Function_[] $annotations
4026
*
4127
* @return mixed
4228
*/
43-
public function onAnnotation(array $annotations);
29+
public function load(array $annotations);
4430

4531
/**
46-
* The annotation class to find
47-
*
48-
* @return string
32+
* The annotation class to find.
4933
*/
5034
public function getAnnotation(): string;
51-
52-
/**
53-
* This methods gets the annotation from a function or method's
54-
* paramater.
55-
*
56-
* @return string[]
57-
*/
58-
public function getArguments(): array;
5935
}

tests/Fixtures/SampleListener.php

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
namespace Biurad\Annotations\Tests\Fixtures;
1919

2020
use Biurad\Annotations\ListenerInterface;
21+
use Biurad\Annotations\Locate\Annotation;
22+
use Biurad\Annotations\Locate\Class_;
23+
use Biurad\Annotations\Locate\Method;
2124

2225
class 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

Comments
 (0)