55use Illuminate \Contracts \Auth \Access \Gate as GateContract ;
66use Illuminate \Contracts \Cache \Repository ;
77use Illuminate \Foundation \Auth \User ;
8+ use Illuminate \Support \Collection ;
89use Illuminate \Support \Str ;
910use Yajra \Acl \Models \Permission ;
1011
1112class GateRegistrar
1213{
13- public function __construct (public GateContract $ gate , public Repository $ cache )
14- {
15- }
14+ public function __construct (public GateContract $ gate , public Repository $ cache ) {}
1615
1716 public function register (): void
1817 {
19- collect ($ this ->getPermissions ())->each (function ($ data ) {
20- $ permission = new Permission ($ data );
21-
18+ $ this ->getPermissions ()->each (function (Permission $ permission ) {
2219 $ ability = $ permission ->slug ;
2320 $ policy = function (User $ user ) use ($ permission ) {
2421 if (method_exists ($ user , 'getPermissions ' )) {
2522 // @phpstan-ignore-next-line
26- $ permissions = collect ($ user ->getPermissions ());
27-
28- return $ permissions ->contains ($ permission ->slug );
23+ return collect ($ user ->getPermissions ())->contains ($ permission ->slug );
2924 }
3025
3126 return false ;
@@ -43,36 +38,32 @@ public function register(): void
4338 /**
4439 * Get all permissions.
4540 *
46- * @return array <array<string, mixed> >
41+ * @return \Illuminate\Support\Collection <array-key, Permission >
4742 */
48- protected function getPermissions (): array
43+ protected function getPermissions (): Collection
4944 {
5045 /** @var string $key */
5146 $ key = config ('acl.cache.key ' , 'permissions.policies ' );
5247
5348 try {
54- if (config ('acl.cache.enabled ' , true )) {
55- return $ this ->cache ->rememberForever ($ key , fn () => $ this ->getPermissionsFromQuery ());
56- } else {
57- return $ this ->getPermissionsFromQuery ();
58- }
49+ return config ('acl.cache.enabled ' , true )
50+ ? $ this ->cache ->rememberForever ($ key , fn () => $ this ->getPermissionsFromQuery ())
51+ : $ this ->getPermissionsFromQuery ();
5952 } catch (\Throwable ) {
6053 $ this ->cache ->forget ($ key );
6154
62- return [] ;
55+ return collect () ;
6356 }
6457 }
6558
6659 /**
67- * @return array <array<string, mixed> >
60+ * @return \Illuminate\Support\Collection <array-key, Permission >
6861 */
69- public function getPermissionsFromQuery (): array
62+ public function getPermissionsFromQuery (): Collection
7063 {
71- // @phpstan-ignore-next-line
7264 return $ this ->getPermissionClass ()
7365 ->with ('roles ' )
74- ->get ()
75- ->toArray ();
66+ ->get ();
7667 }
7768
7869 protected function getPermissionClass (): Permission
0 commit comments