@@ -22,12 +22,6 @@ public static function flushCache(): void
22
22
app ()->make (static ::class)->cache = [];
23
23
}
24
24
25
- /**
26
- * @param mixed $user
27
- * @param string $ability
28
- * @param mixed $args
29
- * @return mixed
30
- */
31
25
public function handleGateCall (mixed $ user , string $ ability , mixed $ args ): mixed
32
26
{
33
27
if (! is_array ($ args )) {
@@ -42,28 +36,41 @@ public function handleGateCall(mixed $user, string $ability, mixed $args): mixed
42
36
43
37
$ policy = Gate::getPolicyFor ($ model );
44
38
45
- if ($ model && $ this ->shouldCache ($ policy )) {
39
+ if ($ model && $ this ->shouldCache ($ policy, $ ability )) {
46
40
return $ this ->callPolicyMethod ($ user , $ policy , $ ability , $ args );
47
41
}
48
42
49
43
return null ;
50
44
}
51
45
52
- /**
53
- * @param object|null $policy
54
- * @return bool
55
- */
56
- protected function shouldCache (?object $ policy ): bool
46
+ protected function shouldCache (?object $ policy , string $ ability ): bool
57
47
{
58
- return $ policy && ($ policy instanceof SoftCacheable || config ('policy-soft-cache.cache_all_policies ' , false ) === true );
48
+ // when policy is not filled don't cache it
49
+ if (blank ($ policy )) {
50
+ return false ;
51
+ }
52
+
53
+ // when policy is not an object don't cache it
54
+ if (! is_object ($ policy )) {
55
+ return false ;
56
+ }
57
+
58
+ // when policy doesn't have the ability don't cache it
59
+ if (! method_exists ($ policy , $ ability )) {
60
+ return false ;
61
+ }
62
+
63
+ // when policy is soft cacheable cache it
64
+ if ($ policy instanceof SoftCacheable) {
65
+ return true ;
66
+ }
67
+
68
+ // when config is set to cache all policies cache it
69
+ return config ('policy-soft-cache.cache_all_policies ' , false ) === true ;
59
70
}
60
71
61
72
/**
62
- * @param Model $user
63
- * @param object $policy
64
- * @param string $ability
65
73
* @param array<int,mixed> $args
66
- * @return mixed
67
74
*/
68
75
protected function callPolicyMethod (Model $ user , object $ policy , string $ ability , array $ args ): mixed
69
76
{
@@ -80,11 +87,7 @@ protected function callPolicyMethod(Model $user, object $policy, string $ability
80
87
}
81
88
82
89
/**
83
- * @param Model $user
84
- * @param object $policy
85
90
* @param array<int,mixed> $args
86
- * @param string $ability
87
- * @return string
88
91
*/
89
92
protected function getCacheKey (Model $ user , object $ policy , array $ args , string $ ability ): string
90
93
{
0 commit comments