-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotationReader.php
More file actions
175 lines (161 loc) · 4.8 KB
/
AnnotationReader.php
File metadata and controls
175 lines (161 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare( strict_types=1 );
namespace Axpecto\Annotation;
use Axpecto\Collection\Klist;
use Axpecto\Reflection\ReflectionUtils;
use ReflectionAttribute;
use ReflectionException;
use ReflectionMethod;
use ReflectionParameter;
/**
* AnnotationReader
*
* Reads PHP 8 attributes and turns them into Annotation instances,
* filtering by class, and annotating each instance
* with its declaring class and/or method name.
*
* @template A of Annotation
* @psalm-consistent-constructor
*/
class AnnotationReader {
/**
* @param ReflectionUtils $reflection
* Used to fetch native PHP ReflectionAttribute instances.
*/
public function __construct(
private readonly ReflectionUtils $reflection
) {
}
/**
* Fetch all annotations of the given type on a class.
*
* @template T of Annotation
* @param class-string<A> $class
* @param class-string<T> $annotationClass
*
* @return Klist<T> A list of instantiated annotations, each with its
* ->setAnnotatedClass($class) already applied.
*
* @throws ReflectionException
*/
public function getClassAnnotations(
string $class,
string $annotationClass
): Klist {
return $this->reflection
->getClassAttributes( $class )
->filter( fn( Annotation $ann ) => $ann instanceof $annotationClass )
->map( fn( Annotation $ann ) => $ann->setAnnotatedClass( $class ) );
}
/**
* Fetch all annotations of the given type on a specific method.
*
* @template T of Annotation
* @param class-string<A> $class
* @param string $method
* @param class-string<T> $annotationClass
*
* @return Klist<T> A list of instantiated annotations, each with
* ->setAnnotatedClass($class)
* and ->setAnnotatedMethod($method) applied.
*
* @throws ReflectionException
*/
public function getMethodAnnotations(
string $class,
string $method,
string $annotationClass
): Klist {
return $this->reflection
->getMethodAttributes( $class, $method )
->filter( fn( Annotation $ann ) => $ann instanceof $annotationClass )
->map( fn( Annotation $ann ) => $ann
->setAnnotatedClass( $class )
->setAnnotatedMethod( $method )
);
}
/**
* Fetch both class-level and method-level annotations of a given type.
*
* @template T of Annotation
* @param class-string<A> $class
* @param class-string<T> $annotationClass
*
* @return Klist<T> All matching annotations on the class itself
* and on any of its methods.
*
* @throws ReflectionException
*/
public function getAllAnnotations(
string $class,
string $annotationClass,
): Klist {
$classAnns = $this->getClassAnnotations( $class, $annotationClass );
$methodAnns = $this->reflection
->getAnnotatedMethods( $class, $annotationClass )
->map( fn( ReflectionMethod $m ) => $this->getMethodAnnotations( $class, $m->getName(), $annotationClass ) )
->flatten();
return $classAnns->merge( $methodAnns );
}
/**
* Fetch all annotations of the given type on a single method parameter.
*
* @template T of Annotation
* @param class-string<A> $class
* @param string $method
* @param string $parameterName
* @param class-string<T> $annotationClass
*
* @return Klist<T> A list (possibly empty) of annotations on that parameter,
* each with ->setAnnotatedClass() and ->setAnnotatedMethod().
*
* @throws ReflectionException
*/
public function getParameterAnnotations(
string $class,
string $method,
string $parameterName,
string $annotationClass
): Klist {
$param = listFrom( $this->reflection->getClassMethod( $class, $method )->getParameters() )
->filter( fn( ReflectionParameter $p ) => $p->getName() === $parameterName )
->firstOrNull();
if ( $param === null ) {
return emptyList();
}
return listFrom( $param->getAttributes() )
->map( fn( ReflectionAttribute $attr ) => $attr->newInstance() )
->filter( fn( $inst ) => $inst instanceof $annotationClass )
->map( fn( Annotation $ann ) => $ann
->setAnnotatedClass( $class )
->setAnnotatedMethod( $method )
);
}
/**
* Fetch exactly one annotation of the given type on a class property.
*
* @template T of Annotation
* @param class-string<object> $class
* @param string $property
* @param class-string<T> $annotationClass
*
* @return T The first matching annotation, or null if none.
*
* @throws ReflectionException
*/
public function getPropertyAnnotation(
string $class,
string $property,
string $annotationClass = Annotation::class
): ?Annotation {
$attrs = $this->reflection
->getReflectionClass( $class )
->getProperty( $property )
->getAttributes();
return listFrom( $attrs )
->map( fn( ReflectionAttribute $attr ) => $attr->newInstance() )
->filter( fn( $inst ) => $inst instanceof $annotationClass )
->firstOrNull()
?->setAnnotatedClass( $class );
}
}