@@ -28,7 +28,7 @@ class AnnotationLoader implements LoaderInterface
2828{
2929 protected $ reader ;
3030
31- public function __construct (Reader $ reader )
31+ public function __construct (Reader $ reader = null )
3232 {
3333 $ this ->reader = $ reader ;
3434 }
@@ -42,7 +42,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
4242 $ className = $ reflClass ->name ;
4343 $ success = false ;
4444
45- foreach ($ this ->reader -> getClassAnnotations ($ reflClass ) as $ constraint ) {
45+ foreach ($ this ->getAnnotations ($ reflClass ) as $ constraint ) {
4646 if ($ constraint instanceof GroupSequence) {
4747 $ metadata ->setGroupSequence ($ constraint ->groups );
4848 } elseif ($ constraint instanceof GroupSequenceProvider) {
@@ -56,7 +56,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
5656
5757 foreach ($ reflClass ->getProperties () as $ property ) {
5858 if ($ property ->getDeclaringClass ()->name === $ className ) {
59- foreach ($ this ->reader -> getPropertyAnnotations ($ property ) as $ constraint ) {
59+ foreach ($ this ->getAnnotations ($ property ) as $ constraint ) {
6060 if ($ constraint instanceof Constraint) {
6161 $ metadata ->addPropertyConstraint ($ property ->name , $ constraint );
6262 }
@@ -68,7 +68,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
6868
6969 foreach ($ reflClass ->getMethods () as $ method ) {
7070 if ($ method ->getDeclaringClass ()->name === $ className ) {
71- foreach ($ this ->reader -> getMethodAnnotations ($ method ) as $ constraint ) {
71+ foreach ($ this ->getAnnotations ($ method ) as $ constraint ) {
7272 if ($ constraint instanceof Callback) {
7373 $ constraint ->callback = $ method ->getName ();
7474
@@ -88,4 +88,35 @@ public function loadClassMetadata(ClassMetadata $metadata)
8888
8989 return $ success ;
9090 }
91+
92+ /**
93+ * @param \ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection
94+ */
95+ private function getAnnotations (object $ reflection ): iterable
96+ {
97+ if (\PHP_VERSION_ID >= 80000 ) {
98+ foreach ($ reflection ->getAttributes (GroupSequence::class) as $ attribute ) {
99+ yield $ attribute ->newInstance ();
100+ }
101+ foreach ($ reflection ->getAttributes (GroupSequenceProvider::class) as $ attribute ) {
102+ yield $ attribute ->newInstance ();
103+ }
104+ foreach ($ reflection ->getAttributes (Constraint::class, \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
105+ yield $ attribute ->newInstance ();
106+ }
107+ }
108+ if (!$ this ->reader ) {
109+ return ;
110+ }
111+
112+ if ($ reflection instanceof \ReflectionClass) {
113+ yield from $ this ->reader ->getClassAnnotations ($ reflection );
114+ }
115+ if ($ reflection instanceof \ReflectionMethod) {
116+ yield from $ this ->reader ->getMethodAnnotations ($ reflection );
117+ }
118+ if ($ reflection instanceof \ReflectionProperty) {
119+ yield from $ this ->reader ->getPropertyAnnotations ($ reflection );
120+ }
121+ }
91122}
0 commit comments