Skip to content

Commit

Permalink
refactor: Change static properties to non-static
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver committed Feb 9, 2023
1 parent 991a000 commit 20cd940
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 70 deletions.
12 changes: 6 additions & 6 deletions database/migrations/add_teams_fields.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ class AddTeamsFields extends Migration
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');

if (DB::getDriverName() !== 'sqlite') {
$table->dropForeign([PermissionRegistrar::$pivotPermission]);
$table->dropForeign([app(PermissionRegistrar::class)->pivotPermission]);
}
$table->dropPrimary();

$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
$table->primary([$columnNames['team_foreign_key'], app(PermissionRegistrar::class)->pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
if (DB::getDriverName() !== 'sqlite') {
$table->foreign(PermissionRegistrar::$pivotPermission)
$table->foreign(app(PermissionRegistrar::class)->pivotPermission)
->references('id')->on($tableNames['permissions'])->onDelete('cascade');
}
});
Expand All @@ -64,14 +64,14 @@ class AddTeamsFields extends Migration
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');

if (DB::getDriverName() !== 'sqlite') {
$table->dropForeign([PermissionRegistrar::$pivotRole]);
$table->dropForeign([app(PermissionRegistrar::class)->pivotRole]);
}
$table->dropPrimary();

$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
$table->primary([$columnNames['team_foreign_key'], app(PermissionRegistrar::class)->pivotRole, $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
if (DB::getDriverName() !== 'sqlite') {
$table->foreign(PermissionRegistrar::$pivotRole)
$table->foreign(app(PermissionRegistrar::class)->pivotRole)
->references('id')->on($tableNames['roles'])->onDelete('cascade');
}
});
Expand Down
26 changes: 13 additions & 13 deletions database/migrations/create_permission_tables.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -51,67 +51,67 @@ class CreatePermissionTables extends Migration
});

Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
$table->unsignedBigInteger(app(PermissionRegistrar::class)->pivotPermission);

$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');

$table->foreign(PermissionRegistrar::$pivotPermission)
$table->foreign(app(PermissionRegistrar::class)->pivotPermission)
->references('id') // permission id
->on($tableNames['permissions'])
->onDelete('cascade');
if ($teams) {
$table->unsignedBigInteger($columnNames['team_foreign_key']);
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');

$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
$table->primary([$columnNames['team_foreign_key'], app(PermissionRegistrar::class)->pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
} else {
$table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
$table->primary([app(PermissionRegistrar::class)->pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
}

});

Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole);
$table->unsignedBigInteger(app(PermissionRegistrar::class)->pivotRole);

$table->string('model_type');
$table->unsignedBigInteger($columnNames['model_morph_key']);
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');

$table->foreign(PermissionRegistrar::$pivotRole)
$table->foreign(app(PermissionRegistrar::class)->pivotRole)
->references('id') // role id
->on($tableNames['roles'])
->onDelete('cascade');
if ($teams) {
$table->unsignedBigInteger($columnNames['team_foreign_key']);
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');

$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
$table->primary([$columnNames['team_foreign_key'], app(PermissionRegistrar::class)->pivotRole, $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
} else {
$table->primary([PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
$table->primary([app(PermissionRegistrar::class)->pivotRole, $columnNames['model_morph_key'], 'model_type'],
'model_has_roles_role_model_type_primary');
}
});

Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole);
$table->unsignedBigInteger(app(PermissionRegistrar::class)->pivotPermission);
$table->unsignedBigInteger(app(PermissionRegistrar::class)->pivotRole);

$table->foreign(PermissionRegistrar::$pivotPermission)
$table->foreign(app(PermissionRegistrar::class)->pivotPermission)
->references('id') // permission id
->on($tableNames['permissions'])
->onDelete('cascade');

$table->foreign(PermissionRegistrar::$pivotRole)
$table->foreign(app(PermissionRegistrar::class)->pivotRole)
->references('id') // role id
->on($tableNames['roles'])
->onDelete('cascade');

$table->primary([PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole], 'role_has_permissions_permission_id_role_id_primary');
$table->primary([app(PermissionRegistrar::class)->pivotPermission, app(PermissionRegistrar::class)->pivotRole], 'role_has_permissions_permission_id_role_id_primary');
});

app('cache')
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/CreateRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class CreateRole extends Command

protected $description = 'Create a role';

public function handle()
public function handle(PermissionRegistrar $permissionRegistrar)
{
$roleClass = app(RoleContract::class);

$teamIdAux = getPermissionsTeamId();
setPermissionsTeamId($this->option('team-id') ?: null);

if (! PermissionRegistrar::$teams && $this->option('team-id')) {
if (! $permissionRegistrar->teams && $this->option('team-id')) {
$this->warn('Teams feature disabled, argument --team-id has no effect. Either enable it in permissions config file or remove --team-id parameter');

return;
Expand All @@ -33,8 +33,8 @@ public function handle()
$role = $roleClass::findOrCreate($this->argument('name'), $this->argument('guard'));
setPermissionsTeamId($teamIdAux);

$teams_key = PermissionRegistrar::$teamsKey;
if (PermissionRegistrar::$teams && $this->option('team-id') && is_null($role->$teams_key)) {
$teams_key = $permissionRegistrar->teamsKey;
if ($permissionRegistrar->teams && $this->option('team-id') && is_null($role->$teams_key)) {
$this->warn("Role `{$role->name}` already exists on the global team; argument --team-id has no effect");
}

Expand Down
6 changes: 3 additions & 3 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function roles(): BelongsToMany
return $this->belongsToMany(
config('permission.models.role'),
config('permission.table_names.role_has_permissions'),
PermissionRegistrar::$pivotPermission,
PermissionRegistrar::$pivotRole
app(PermissionRegistrar::class)->pivotPermission,
app(PermissionRegistrar::class)->pivotRole
);
}

Expand All @@ -76,7 +76,7 @@ public function users(): BelongsToMany
getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
'model',
config('permission.table_names.model_has_permissions'),
PermissionRegistrar::$pivotPermission,
app(PermissionRegistrar::class)->pivotPermission,
config('permission.column_names.model_morph_key')
);
}
Expand Down
30 changes: 17 additions & 13 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public static function create(array $attributes = [])
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);

$params = ['name' => $attributes['name'], 'guard_name' => $attributes['guard_name']];
if (PermissionRegistrar::$teams) {
if (array_key_exists(PermissionRegistrar::$teamsKey, $attributes)) {
$params[PermissionRegistrar::$teamsKey] = $attributes[PermissionRegistrar::$teamsKey];
if (app(PermissionRegistrar::class)->teams) {
$teamsKey = app(PermissionRegistrar::class)->teamsKey;

if (array_key_exists($teamsKey, $attributes)) {
$params[$teamsKey] = $attributes[$teamsKey];
} else {
$attributes[PermissionRegistrar::$teamsKey] = getPermissionsTeamId();
$attributes[$teamsKey] = getPermissionsTeamId();
}
}
if (static::findByParam($params)) {
Expand All @@ -64,8 +66,8 @@ public function permissions(): BelongsToMany
return $this->belongsToMany(
config('permission.models.permission'),
config('permission.table_names.role_has_permissions'),
PermissionRegistrar::$pivotRole,
PermissionRegistrar::$pivotPermission
app(PermissionRegistrar::class)->pivotRole,
app(PermissionRegistrar::class)->pivotPermission
);
}

Expand All @@ -78,7 +80,7 @@ public function users(): BelongsToMany
getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
'model',
config('permission.table_names.model_has_roles'),
PermissionRegistrar::$pivotRole,
app(PermissionRegistrar::class)->pivotRole,
config('permission.column_names.model_morph_key')
);
}
Expand Down Expand Up @@ -139,7 +141,7 @@ public static function findOrCreate(string $name, $guardName = null): RoleContra
$role = static::findByParam(['name' => $name, 'guard_name' => $guardName]);

if (! $role) {
return static::query()->create(['name' => $name, 'guard_name' => $guardName] + (PermissionRegistrar::$teams ? [PermissionRegistrar::$teamsKey => getPermissionsTeamId()] : []));
return static::query()->create(['name' => $name, 'guard_name' => $guardName] + (app(PermissionRegistrar::class)->teams ? [app(PermissionRegistrar::class)->teamsKey => getPermissionsTeamId()] : []));
}

return $role;
Expand All @@ -149,12 +151,14 @@ protected static function findByParam(array $params = [])
{
$query = static::query();

if (PermissionRegistrar::$teams) {
$query->where(function ($q) use ($params) {
$q->whereNull(PermissionRegistrar::$teamsKey)
->orWhere(PermissionRegistrar::$teamsKey, $params[PermissionRegistrar::$teamsKey] ?? getPermissionsTeamId());
if (app(PermissionRegistrar::class)->teams) {
$teamsKey = app(PermissionRegistrar::class)->teamsKey;

$query->where(function ($q) use ($params, $teamsKey) {
$q->whereNull($teamsKey)
->orWhere($teamsKey, $params[$teamsKey] ?? getPermissionsTeamId());
});
unset($params[PermissionRegistrar::$teamsKey]);
unset($params[$teamsKey]);
}

foreach ($params as $key => $value) {
Expand Down
28 changes: 14 additions & 14 deletions src/PermissionRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ class PermissionRegistrar
protected $permissions;

/** @var string */
public static $pivotRole;
public $pivotRole;

/** @var string */
public static $pivotPermission;
public $pivotPermission;

/** @var \DateInterval|int */
public static $cacheExpirationTime;
public $cacheExpirationTime;

/** @var bool */
public static $teams;
public $teams;

/** @var string */
public static $teamsKey;
public $teamsKey;

/** @var int|string */
protected $teamId = null;

/** @var string */
public static $cacheKey;
public $cacheKey;

/** @var array */
private $cachedRoles = [];
Expand All @@ -74,15 +74,15 @@ public function __construct(CacheManager $cacheManager)

public function initializeCache()
{
self::$cacheExpirationTime = config('permission.cache.expiration_time') ?: \DateInterval::createFromDateString('24 hours');
$this->cacheExpirationTime = config('permission.cache.expiration_time') ?: \DateInterval::createFromDateString('24 hours');

self::$teams = config('permission.teams', false);
self::$teamsKey = config('permission.column_names.team_foreign_key');
$this->teams = config('permission.teams', false);
$this->teamsKey = config('permission.column_names.team_foreign_key');

self::$cacheKey = config('permission.cache.key');
$this->cacheKey = config('permission.cache.key');

self::$pivotRole = config('permission.column_names.role_pivot_key') ?: 'role_id';
self::$pivotPermission = config('permission.column_names.permission_pivot_key') ?: 'permission_id';
$this->pivotRole = config('permission.column_names.role_pivot_key') ?: 'role_id';
$this->pivotPermission = config('permission.column_names.permission_pivot_key') ?: 'permission_id';

$this->cache = $this->getCacheStoreFromConfig();
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public function forgetCachedPermissions()
{
$this->permissions = null;

return $this->cache->forget(self::$cacheKey);
return $this->cache->forget($this->cacheKey);
}

/**
Expand All @@ -174,7 +174,7 @@ private function loadPermissions()
return;
}

$this->permissions = $this->cache->remember(self::$cacheKey, self::$cacheExpirationTime, function () {
$this->permissions = $this->cache->remember($this->cacheKey, $this->cacheExpirationTime, function () {
return $this->getSerializedPermissionsForCache();
});

Expand Down
16 changes: 8 additions & 8 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public static function bootHasPermissions()
return;
}

$teams = PermissionRegistrar::$teams;
PermissionRegistrar::$teams = false;
$teams = app(PermissionRegistrar::class)->teams;
app(PermissionRegistrar::class)->teams = false;
$model->permissions()->detach();
PermissionRegistrar::$teams = $teams;
app(PermissionRegistrar::class)->teams = $teams;
});
}

Expand Down Expand Up @@ -77,14 +77,14 @@ public function permissions(): BelongsToMany
'model',
config('permission.table_names.model_has_permissions'),
config('permission.column_names.model_morph_key'),
PermissionRegistrar::$pivotPermission
app(PermissionRegistrar::class)->pivotPermission
);

if (! PermissionRegistrar::$teams) {
if (! app(PermissionRegistrar::class)->teams) {
return $relation;
}

return $relation->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId());
return $relation->wherePivot(app(PermissionRegistrar::class)->teamsKey, getPermissionsTeamId());
}

/**
Expand Down Expand Up @@ -361,8 +361,8 @@ public function collectPermissions(...$permissions)

$this->ensureModelSharesGuard($permission);

$array[$permission->getKey()] = PermissionRegistrar::$teams && ! is_a($this, Role::class) ?
[PermissionRegistrar::$teamsKey => getPermissionsTeamId()] : [];
$array[$permission->getKey()] = app(PermissionRegistrar::class)->teams && ! is_a($this, Role::class) ?
[app(PermissionRegistrar::class)->teamsKey => getPermissionsTeamId()] : [];

return $array;
}, []);
Expand Down
18 changes: 9 additions & 9 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function bootHasRoles()
return;
}

$teams = PermissionRegistrar::$teams;
PermissionRegistrar::$teams = false;
$teams = app(PermissionRegistrar::class)->teams;
app(PermissionRegistrar::class)->teams = false;
$model->roles()->detach();
PermissionRegistrar::$teams = $teams;
app(PermissionRegistrar::class)->teams = $teams;
});
}

Expand All @@ -50,16 +50,16 @@ public function roles(): BelongsToMany
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
PermissionRegistrar::$pivotRole
app(PermissionRegistrar::class)->pivotRole
);

if (! PermissionRegistrar::$teams) {
if (! app(PermissionRegistrar::class)->teams) {
return $relation;
}

return $relation->wherePivot(PermissionRegistrar::$teamsKey, getPermissionsTeamId())
return $relation->wherePivot(app(PermissionRegistrar::class)->teamsKey, getPermissionsTeamId())
->where(function ($q) {
$teamField = config('permission.table_names.roles').'.'.PermissionRegistrar::$teamsKey;
$teamField = config('permission.table_names.roles').'.'. app(PermissionRegistrar::class)->teamsKey;
$q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId());
});
}
Expand Down Expand Up @@ -117,8 +117,8 @@ public function assignRole(...$roles)

$this->ensureModelSharesGuard($role);

$array[$role->getKey()] = PermissionRegistrar::$teams && ! is_a($this, Permission::class) ?
[PermissionRegistrar::$teamsKey => getPermissionsTeamId()] : [];
$array[$role->getKey()] = app(PermissionRegistrar::class)->teams && ! is_a($this, Permission::class) ?
[app(PermissionRegistrar::class)->teamsKey => getPermissionsTeamId()] : [];

return $array;
}, []);
Expand Down

0 comments on commit 20cd940

Please sign in to comment.