23
23
*/
24
24
final class ParamReader implements ParamReaderInterface
25
25
{
26
+ /**
27
+ * @var Reader|null
28
+ */
26
29
private $ annotationReader ;
27
30
28
- public function __construct (Reader $ annotationReader )
31
+ public function __construct (? Reader $ annotationReader = null )
29
32
{
30
33
$ this ->annotationReader = $ annotationReader ;
31
34
}
@@ -50,7 +53,17 @@ public function read(\ReflectionClass $reflection, string $method): array
50
53
*/
51
54
public function getParamsFromMethod (\ReflectionMethod $ method ): array
52
55
{
53
- $ annotations = $ this ->annotationReader ->getMethodAnnotations ($ method );
56
+ $ annotations = [];
57
+ if (\PHP_VERSION_ID >= 80000 ) {
58
+ $ annotations = $ this ->getParamsFromAttributes ($ method );
59
+ }
60
+
61
+ if (null !== $ this ->annotationReader ) {
62
+ $ annotations = array_merge (
63
+ $ annotations ,
64
+ $ this ->annotationReader ->getMethodAnnotations ($ method ) ?? []
65
+ );
66
+ }
54
67
55
68
return $ this ->getParamsFromAnnotationArray ($ annotations );
56
69
}
@@ -60,7 +73,17 @@ public function getParamsFromMethod(\ReflectionMethod $method): array
60
73
*/
61
74
public function getParamsFromClass (\ReflectionClass $ class ): array
62
75
{
63
- $ annotations = $ this ->annotationReader ->getClassAnnotations ($ class );
76
+ $ annotations = [];
77
+ if (\PHP_VERSION_ID >= 80000 ) {
78
+ $ annotations = $ this ->getParamsFromAttributes ($ class );
79
+ }
80
+
81
+ if (null !== $ this ->annotationReader ) {
82
+ $ annotations = array_merge (
83
+ $ annotations ,
84
+ $ this ->annotationReader ->getClassAnnotations ($ class ) ?? []
85
+ );
86
+ }
64
87
65
88
return $ this ->getParamsFromAnnotationArray ($ annotations );
66
89
}
@@ -79,4 +102,20 @@ private function getParamsFromAnnotationArray(array $annotations): array
79
102
80
103
return $ params ;
81
104
}
105
+
106
+ /**
107
+ * @param \ReflectionClass|\ReflectionMethod $reflection
108
+ *
109
+ * @return ParamInterface[]
110
+ */
111
+ private function getParamsFromAttributes ($ reflection ): array
112
+ {
113
+ $ params = [];
114
+ foreach ($ reflection ->getAttributes (ParamInterface::class, \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
115
+ $ param = $ attribute ->newInstance ();
116
+ $ params [$ param ->getName ()] = $ param ;
117
+ }
118
+
119
+ return $ params ;
120
+ }
82
121
}
0 commit comments