Skip to content

Commit 0303fd5

Browse files
authored
Merge pull request spatie#512 from spatie/feature/use-php-attribute-reader
Use spatie/php-attribute-reader
2 parents 79c4cb9 + 63e92af commit 0303fd5

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
"require": {
2323
"php": "^8.4",
2424
"illuminate/cache": "^12.0|^13.0",
25-
"illuminate/container": "^12.0|^13.0",
2625
"illuminate/console": "^12.0|^13.0",
26+
"illuminate/container": "^12.0|^13.0",
2727
"illuminate/http": "^12.0|^13.0",
2828
"illuminate/support": "^12.0|^13.0",
2929
"nesbot/carbon": "^3.0",
30-
"spatie/laravel-package-tools": "^1.9"
30+
"spatie/laravel-package-tools": "^1.9",
31+
"spatie/php-attribute-reader": "^1.0"
3132
},
3233
"require-dev": {
3334
"larastan/larastan": "^3.9",

src/Support/AttributeReader.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Spatie\ResponseCache\Support;
44

5-
use ReflectionClass;
6-
use ReflectionMethod;
5+
use Spatie\Attributes\Attributes;
76

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

Comments
 (0)