|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +collect(glob(base_path('**/Models/*.php')))->each(fn($file) => include_once($file)); |
| 4 | + |
| 5 | +$modelPolicies = collect(get_declared_classes()) |
| 6 | + ->filter(fn($class) => is_subclass_of($class, \Illuminate\Database\Eloquent\Model::class)) |
| 7 | + ->filter(fn($class) => !in_array($class, [ |
| 8 | + \Illuminate\Database\Eloquent\Relations\Pivot::class, |
| 9 | + \Illuminate\Foundation\Auth\User::class, |
| 10 | + ])) |
| 11 | + ->flatMap(fn($class) => [ |
| 12 | + $class => Gate::getPolicyFor($class), |
| 13 | + ]) |
| 14 | + ->filter(fn($policy) => $policy !== null); |
| 15 | + |
| 16 | +function vsCodeGetPolicyInfo($policy, $model) |
| 17 | +{ |
| 18 | + $methods = (new ReflectionClass($policy))->getMethods(); |
| 19 | + |
| 20 | + return collect($methods)->map(fn(ReflectionMethod $method) => [ |
| 21 | + 'key' => $method->getName(), |
| 22 | + 'uri' => $method->getFileName(), |
| 23 | + 'policy' => is_string($policy) ? $policy : get_class($policy), |
| 24 | + 'model' => $model, |
| 25 | + 'line' => $method->getStartLine(), |
| 26 | + ])->filter(fn($ability) => !in_array($ability['key'], ['allow', 'deny'])); |
| 27 | +} |
| 28 | + |
3 | 29 | echo collect(\Illuminate\Support\Facades\Gate::abilities())
|
4 | 30 | ->map(function ($policy, $key) {
|
5 | 31 | $reflection = new \ReflectionFunction($policy);
|
|
21 | 47 | return [
|
22 | 48 | 'key' => $key,
|
23 | 49 | 'uri' => $reflection->getFileName(),
|
24 |
| - 'policy_class' => $policyClass, |
25 |
| - 'lineNumber' => $reflection->getStartLine(), |
| 50 | + 'policy' => $policyClass, |
| 51 | + 'line' => $reflection->getStartLine(), |
26 | 52 | ];
|
27 | 53 | })
|
28 | 54 | ->merge(
|
29 |
| - collect(\Illuminate\Support\Facades\Gate::policies())->flatMap(function ($policy, $model) { |
30 |
| - $methods = (new ReflectionClass($policy))->getMethods(); |
31 |
| - |
32 |
| - return collect($methods)->map(function (ReflectionMethod $method) use ($policy) { |
33 |
| - return [ |
34 |
| - 'key' => $method->getName(), |
35 |
| - 'uri' => $method->getFileName(), |
36 |
| - 'policy_class' => $policy, |
37 |
| - 'lineNumber' => $method->getStartLine(), |
38 |
| - ]; |
39 |
| - })->filter(function ($ability) { |
40 |
| - return !in_array($ability['key'], ['allow', 'deny']); |
41 |
| - }); |
42 |
| - }), |
| 55 | + collect(\Illuminate\Support\Facades\Gate::policies())->flatMap(fn($policy, $model) => vsCodeGetPolicyInfo($policy, $model)), |
| 56 | + ) |
| 57 | + ->merge( |
| 58 | + $modelPolicies->flatMap(fn($policy, $model) => vsCodeGetPolicyInfo($policy, $model)), |
43 | 59 | )
|
44 | 60 | ->values()
|
45 | 61 | ->groupBy('key')
|
| 62 | + ->map(fn($item) => $item->map(fn($i) => \Illuminate\Support\Arr::except($i, 'key'))) |
46 | 63 | ->toJson();
|
0 commit comments