Skip to content

Commit 03c05ac

Browse files
Merge pull request #254 from laravel/better-auth-support
Improved Gate/Policy support
2 parents dc13542 + 46e6ded commit 03c05ac

File tree

6 files changed

+272
-90
lines changed

6 files changed

+272
-90
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "laravel",
44
"displayName": "Laravel",
55
"description": "Official VS Code extension for Laravel",
6-
"version": "0.1.18",
6+
"version": "0.1.19",
77
"engines": {
88
"vscode": "^1.89.0"
99
},

php-templates/auth.php

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
<?php
22

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+
329
echo collect(\Illuminate\Support\Facades\Gate::abilities())
430
->map(function ($policy, $key) {
531
$reflection = new \ReflectionFunction($policy);
@@ -21,26 +47,17 @@
2147
return [
2248
'key' => $key,
2349
'uri' => $reflection->getFileName(),
24-
'policy_class' => $policyClass,
25-
'lineNumber' => $reflection->getStartLine(),
50+
'policy' => $policyClass,
51+
'line' => $reflection->getStartLine(),
2652
];
2753
})
2854
->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)),
4359
)
4460
->values()
4561
->groupBy('key')
62+
->map(fn($item) => $item->map(fn($i) => \Illuminate\Support\Arr::except($i, 'key')))
4663
->toJson();

0 commit comments

Comments
 (0)