diff --git a/src/GateRegistrar.php b/src/GateRegistrar.php index 394b92b..32703b5 100644 --- a/src/GateRegistrar.php +++ b/src/GateRegistrar.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Cache\Repository; use Illuminate\Foundation\Auth\User; use Illuminate\Support\Str; +use Illuminate\Support\Collection; use Yajra\Acl\Models\Permission; class GateRegistrar @@ -16,16 +17,12 @@ public function __construct(public GateContract $gate, public Repository $cache) public function register(): void { - collect($this->getPermissions())->each(function ($data) { - $permission = new Permission($data); - + $this->getPermissions()->each(function (Permission $permission) { $ability = $permission->slug; $policy = function (User $user) use ($permission) { if (method_exists($user, 'getPermissions')) { // @phpstan-ignore-next-line - $permissions = collect($user->getPermissions()); - - return $permissions->contains($permission->slug); + return collect($user->getPermissions())->contains($permission->slug); } return false; @@ -43,28 +40,28 @@ public function register(): void /** * Get all permissions. * - * @return array> + * @return \Illuminate\Support\Collection */ - protected function getPermissions(): array + protected function getPermissions(): Collection { /** @var string $key */ $key = config('acl.cache.key', 'permissions.policies'); try { - if (config('acl.cache.enabled', true)) { - return $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery()); - } else { - return $this->getPermissionsFromQuery(); - } + $permissions = config('acl.cache.enabled', true) + ? $this->cache->rememberForever($key, fn () => $this->getPermissionsFromQuery()) + : $this->getPermissionsFromQuery(); + + return collect($permissions)->map(fn ($data) => new Permission($data)); } catch (\Throwable) { $this->cache->forget($key); - return []; + return collect([]); } } /** - * @return array> + * @return array */ public function getPermissionsFromQuery(): array {