Skip to content

Commit

Permalink
fix call static class
Browse files Browse the repository at this point in the history
  • Loading branch information
makladuxbert committed Sep 7, 2017
1 parent 3c75e54 commit dbe1877
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 114 deletions.
22 changes: 13 additions & 9 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@

use Illuminate\Support\Collection;

/**
* Class Helpers
* @package Maklad\Permission
*/
class Helpers
{
/**
* @param string $guard
*
* @return string|null
*/
public static function getModelForGuard(string $guard)
public function getModelForGuard(string $guard)
{
return collect(config('auth.guards'))
return \collect(\config('auth.guards'))
->map(function ($guard) {
return config("auth.providers.{$guard['provider']}.model");
return \config("auth.providers.{$guard['provider']}.model");
})->get($guard);
}

/**
* @param string $expected
* @param Collection $expected
* @param string $given
*
* @return string
*/
public static function getGuardDoesNotMatchMessage(Collection $expected, string $given): string
public function getGuardDoesNotMatchMessage(Collection $expected, string $given): string
{
return "The given role or permission should use guard `{$expected->implode(', ')}` instead of `{$given}`.";
}
Expand All @@ -36,7 +40,7 @@ public static function getGuardDoesNotMatchMessage(Collection $expected, string
*
* @return string
*/
public static function getPermissionAlreadyExistsMessage(string $name, string $guardName): string
public function getPermissionAlreadyExistsMessage(string $name, string $guardName): string
{
return "A permission `{$name}` already exists for guard `{$guardName}`.";
}
Expand All @@ -47,7 +51,7 @@ public static function getPermissionAlreadyExistsMessage(string $name, string $g
*
* @return string
*/
public static function getPermissionDoesNotExistMessage(string $name, string $guardName): string
public function getPermissionDoesNotExistMessage(string $name, string $guardName): string
{
return "There is no permission named `{$name}` for guard `{$guardName}`.";
}
Expand All @@ -58,7 +62,7 @@ public static function getPermissionDoesNotExistMessage(string $name, string $gu
*
* @return string
*/
public static function getRoleAlreadyExistsMessage(string $name, string $guardName): string
public function getRoleAlreadyExistsMessage(string $name, string $guardName): string
{
return "A role `{$name}` already exists for guard `{$guardName}`.";
}
Expand All @@ -70,7 +74,7 @@ public static function getRoleAlreadyExistsMessage(string $name, string $guardNa
*
* @return string
*/
public static function getRoleDoesNotExistMessage(string $name, string $guardName): string
public function getRoleDoesNotExistMessage(string $name, string $guardName): string
{
return "There is no role named `{$name}` for guard `{$guardName}`.";
}
Expand Down
21 changes: 13 additions & 8 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Permission extends Model implements PermissionInterface
use RefreshesPermissionCache;

public $guarded = ['id'];
protected $helpers;

/**
* Permission constructor.
Expand All @@ -25,11 +26,13 @@ class Permission extends Model implements PermissionInterface
*/
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

parent::__construct($attributes);

$this->setTable(config('permission.table_names.permissions'));
$this->helpers = new Helpers();

$this->setTable(\config('permission.table_names.permissions'));
}

/**
Expand All @@ -41,15 +44,16 @@ public function __construct(array $attributes = [])
*/
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

if (static::getPermissions()->where('name', $attributes['name'])->where(
'guard_name',
$attributes['guard_name']
)->first()) {
$name = $attributes['name'];
$guard_name = $attributes['guard_name'];
throw new PermissionAlreadyExists(Helpers::getPermissionAlreadyExistsMessage($name, $guard_name));
$helpers = new Helpers();
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guard_name));
}

return static::query()->create($attributes);
Expand All @@ -62,8 +66,8 @@ public static function create(array $attributes = [])
public function roles(): BelongsToMany
{
return $this->belongsToMany(
config('permission.models.role'),
config('permission.table_names.role_has_permissions')
\config('permission.models.role'),
\config('permission.table_names.role_has_permissions')
);
}

Expand All @@ -73,7 +77,7 @@ public function roles(): BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(Helpers::getModelForGuard($this->attributes['guard_name']));
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
}

/**
Expand All @@ -92,7 +96,8 @@ public static function findByName(string $name, $guardName = null): PermissionIn
$permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first();

if (! $permission) {
throw new PermissionDoesNotExist(Helpers::getPermissionDoesNotExistMessage($name, $guardName));
$helpers = new Helpers();
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName));
}

return $permission;
Expand Down
29 changes: 17 additions & 12 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ class Role extends Model implements RoleInterface
use RefreshesPermissionCache;

public $guarded = ['id'];
protected $helpers;

public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

parent::__construct($attributes);

$this->setTable(config('permission.table_names.roles'));
$this->helpers = new Helpers();

$this->setTable(\config('permission.table_names.roles'));
}

/**
Expand All @@ -38,12 +41,13 @@ public function __construct(array $attributes = [])
*/
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard');

if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
$name = $attributes['name'];
$guard_name = $attributes['guard_name'];
throw new RoleAlreadyExists(Helpers::getRoleAlreadyExistsMessage($name, $guard_name));
$helpers = new Helpers();
throw new RoleAlreadyExists($helpers->getRoleAlreadyExistsMessage($name, $guard_name));
}

return static::query()->create($attributes);
Expand All @@ -56,8 +60,8 @@ public static function create(array $attributes = [])
public function permissions(): BelongsToMany
{
return $this->belongsToMany(
config('permission.models.permission'),
config('permission.table_names.role_has_permissions')
\config('permission.models.permission'),
\config('permission.table_names.role_has_permissions')
);
}

Expand All @@ -66,7 +70,7 @@ public function permissions(): BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(Helpers::getModelForGuard($this->attributes['guard_name']));
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name']));
}

/**
Expand All @@ -80,12 +84,13 @@ public function users(): BelongsToMany
*/
public static function findByName(string $name, $guardName = null): RoleInterface
{
$guardName = $guardName ?? config('auth.defaults.guard');
$guardName = $guardName ?? \config('auth.defaults.guard');

$role = static::where('name', $name)->where('guard_name', $guardName)->first();

if (! $role) {
throw new RoleDoesNotExist(Helpers::getRoleDoesNotExistMessage($name, $guardName));
$helpers = new Helpers();
throw new RoleDoesNotExist($helpers->getRoleDoesNotExistMessage($name, $guardName));
}

return $role;
Expand All @@ -102,15 +107,15 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
*/
public function hasPermissionTo($permission): bool
{
if (is_string($permission)) {
$permission = app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
if (\is_string($permission)) {
$permission = \app(Permission::class)->findByName($permission, $this->getDefaultGuardName());
}

if (! $this->getGuardNames()->contains($permission->guard_name)) {
$expected = $this->getGuardNames();
$given = $permission->guard_name;

throw new GuardDoesNotMatch(Helpers::getGuardDoesNotMatchMessage($expected, $given));
throw new GuardDoesNotMatch($this->helpers->getGuardDoesNotMatchMessage($expected, $given));
}

return $this->permissions->contains('id', $permission->id);
Expand Down
4 changes: 2 additions & 2 deletions src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function forgetCachedPermissions()

public function getPermissions(): Collection
{
return $this->cache->remember($this->cacheKey, config('permission.cache_expiration_time'), function () {
return app(Permission::class)->with('roles')->get();
return $this->cache->remember($this->cacheKey, \config('permission.cache_expiration_time'), function () {
return \app(Permission::class)->with('roles')->get();
});
}
}
12 changes: 8 additions & 4 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Maklad\Permission\Contracts\PermissionInterface as Permission;
use Maklad\Permission\Contracts\RoleInterface as Role;

/**
* Class PermissionServiceProvider
* @package Maklad\Permission
*/
class PermissionServiceProvider extends ServiceProvider
{
public function boot(PermissionRegistrar $permissionLoader)
Expand Down Expand Up @@ -49,7 +53,7 @@ protected function registerBladeExtensions()
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$bladeCompiler->directive('role', function ($arguments) {
list($role, $guard) = explode(',', $arguments . ',');
list($role, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
});
Expand All @@ -58,7 +62,7 @@ protected function registerBladeExtensions()
});

$bladeCompiler->directive('hasrole', function ($arguments) {
list($role, $guard) = explode(',', $arguments . ',');
list($role, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
});
Expand All @@ -67,7 +71,7 @@ protected function registerBladeExtensions()
});

$bladeCompiler->directive('hasanyrole', function ($arguments) {
list($roles, $guard) = explode(',', $arguments . ',');
list($roles, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
});
Expand All @@ -76,7 +80,7 @@ protected function registerBladeExtensions()
});

$bladeCompiler->directive('hasallroles', function ($arguments) {
list($roles, $guard) = explode(',', $arguments . ',');
list($roles, $guard) = \explode(',', $arguments . ',');

return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
});
Expand Down
27 changes: 17 additions & 10 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Maklad\Permission\Helpers;
use Maklad\Permission\PermissionRegistrar;

/**
* Trait HasPermissions
* @package Maklad\Permission\Traits
*/
trait HasPermissions
{
/**
Expand All @@ -17,10 +21,11 @@ trait HasPermissions
* @param string|array|Permission|\Illuminate\Support\Collection $permissions
*
* @return $this
* @throws GuardDoesNotMatch
*/
public function givePermissionTo(...$permissions)
{
$permissions = collect($permissions)
$permissions = \collect($permissions)
->flatten()
->map(function ($permission) {
return $this->getStoredPermission($permission);
Expand All @@ -43,6 +48,7 @@ public function givePermissionTo(...$permissions)
* @param string|array|Permission|\Illuminate\Support\Collection $permissions
*
* @return $this
* @throws GuardDoesNotMatch
*/
public function syncPermissions(...$permissions)
{
Expand Down Expand Up @@ -74,8 +80,8 @@ public function revokePermissionTo($permission)
*/
protected function getStoredPermission($permissions): Permission
{
if (is_string($permissions)) {
return app(Permission::class)->findByName($permissions, $this->getDefaultGuardName());
if (\is_string($permissions)) {
return \app(Permission::class)->findByName($permissions, $this->getDefaultGuardName());
}

return $permissions;
Expand All @@ -91,30 +97,31 @@ protected function ensureModelSharesGuard(Model $roleOrPermission)
if (! $this->getGuardNames()->contains($roleOrPermission->guard_name)) {
$expected = $this->getGuardNames();
$given = $roleOrPermission->guard_name;
$helpers = new Helpers();

throw new GuardDoesNotMatch(Helpers::getGuardDoesNotMatchMessage($expected, $given));
throw new GuardDoesNotMatch($helpers->getGuardDoesNotMatchMessage($expected, $given));
}
}

protected function getGuardNames(): Collection
{
if ($this->guard_name) {
return collect($this->guard_name);
return \collect($this->guard_name);
}

return collect(config('auth.guards'))
return \collect(\config('auth.guards'))
->map(function ($guard) {
return config("auth.providers.{$guard['provider']}.model");
return \config("auth.providers.{$guard['provider']}.model");
})
->filter(function ($model) {
return get_class($this) === $model;
return \get_class($this) === $model;
})
->keys();
}

protected function getDefaultGuardName(): string
{
$default = config('auth.defaults.guard');
$default = \config('auth.defaults.guard');

return $this->getGuardNames()->first() ?: $default;
}
Expand All @@ -124,6 +131,6 @@ protected function getDefaultGuardName(): string
*/
public function forgetCachedPermissions()
{
app(PermissionRegistrar::class)->forgetCachedPermissions();
\app(PermissionRegistrar::class)->forgetCachedPermissions();
}
}
Loading

0 comments on commit dbe1877

Please sign in to comment.