22
33namespace Spatie \ResponseCache \Support ;
44
5- use ReflectionClass ;
6- use ReflectionMethod ;
5+ use Spatie \Attributes \Attributes ;
76
87class AttributeReader
98{
@@ -26,21 +25,25 @@ public static function getFirstAttribute(string $action, array $attributeClasses
2625 return null ;
2726 }
2827
29- try {
30- $ reflectionClass = new ReflectionClass ( $ controller );
31- $ reflectionMethod = $ reflectionClass -> getMethod ( $ method );
28+ // Check method-level attributes first (they take precedence)
29+ foreach ( $ attributeClasses as $ attributeClass ) {
30+ $ attribute = Attributes:: onMethod ( $ controller , $ method, $ attributeClass );
3231
33- // Check method-level attributes first (they take precedence)
34- $ attribute = static ::findAttributeInReflection ($ reflectionMethod , $ attributeClasses );
3532 if ($ attribute ) {
3633 return $ attribute ;
3734 }
35+ }
3836
39- // Then check class-level attributes
40- return static ::findAttributeInReflection ($ reflectionClass , $ attributeClasses );
41- } catch (\ReflectionException ) {
42- return null ;
37+ // Then check class-level attributes
38+ foreach ($ attributeClasses as $ attributeClass ) {
39+ $ attribute = Attributes::get ($ controller , $ attributeClass );
40+
41+ if ($ attribute ) {
42+ return $ attribute ;
43+ }
4344 }
45+
46+ return null ;
4447 }
4548
4649 protected static function parseAction (string $ action ): array
@@ -51,19 +54,4 @@ protected static function parseAction(string $action): array
5154
5255 return explode ('@ ' , $ action );
5356 }
54-
55- protected static function findAttributeInReflection (
56- ReflectionClass |ReflectionMethod $ reflection ,
57- array $ attributeClasses
58- ): ?object {
59- foreach ($ attributeClasses as $ attributeClass ) {
60- $ attributes = $ reflection ->getAttributes ($ attributeClass );
61-
62- if (! empty ($ attributes )) {
63- return $ attributes [0 ]->newInstance ();
64- }
65- }
66-
67- return null ;
68- }
6957}
0 commit comments