Skip to content

Commit 508e7d5

Browse files
author
Given Ncube
committed
feat: added some optimizations
1 parent 48a33b4 commit 508e7d5

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

src/Commands/GeneratePermissionsCommand.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Facades\File;
9-
use Illuminate\Support\Facades\Storage;
109
use Illuminate\Support\Str;
1110
use ReflectionClass;
1211
use ReflectionException;
1312
use Spatie\Permission\Models\Permission;
1413
use SplFileInfo;
15-
use Symfony\Component\Filesystem\Filesystem;
1614

1715
class GeneratePermissionsCommand extends Command
1816
{
@@ -53,12 +51,14 @@ public function handle(): int
5351

5452
/**
5553
* Generate all permissions.
54+
*
55+
* @return void
5656
*/
5757
protected function generatePermissionsForAllModels(): void
5858
{
59-
$models = $this->getModels();
60-
61-
$models->each(fn(string $model) => $this->generatePermissions($model));
59+
$this->getModels()->each(
60+
fn(string $model) => $this->generatePermissions($model)
61+
);
6262
}
6363

6464
/**
@@ -79,26 +79,36 @@ public function generatePermissions(string $model): void
7979
);
8080
}
8181

82-
public function generatePermission(string $model, string $permission)
82+
/**
83+
* Generate a permission for a given model.
84+
*
85+
* @param string $model
86+
* @param string $permission
87+
* @return mixed
88+
*/
89+
public function generatePermission(string $model, string $permission): mixed
8390
{
8491
if (
8592
Str::contains($permission, 'any') ||
8693
Str::contains($permission, 'all')
8794
) {
8895
return Permission::updateOrCreate([
89-
'name' => $permission . ' ' . Str::plural(Str::lower($model)),
96+
'name' =>
97+
$permission .
98+
' ' .
99+
Str::snake(Str::plural(Str::lower($model))),
90100
]);
91101
}
92102

93103
return Permission::updateOrCreate([
94-
'name' => $permission . ' ' . Str::lower($model),
104+
'name' => $permission . ' ' . Str::snake(Str::lower($model)),
95105
]);
96106
}
97107

98108
/**
99109
* Get all models.
100110
*
101-
* @return array
111+
* @return array|Collection
102112
*/
103113
public function getModels(): array|Collection
104114
{

src/Commands/LaravelAuthorizerCommand.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public function generateAllPolicies(): void
8585
}
8686

8787
return $reflection->isSubclassOf(Model::class) &&
88-
! $reflection->isAbstract();
88+
!$reflection->isAbstract();
8989
})
90-
->map(fn ($model) => Str::afterLast($model, '\\'));
90+
->map(fn($model) => Str::afterLast($model, '\\'));
9191

9292
$models->each(function (string $model) {
9393
$this->generatePolicy($model, $model);
@@ -104,7 +104,7 @@ public function generatePlainPolicy(string $name): void
104104
{
105105
if (
106106
file_exists($this->getPolicyPath($name)) &&
107-
! $this->option('force')
107+
!$this->option('force')
108108
) {
109109
$this->error(sprintf('Policy "%s" already exists!', $name));
110110

@@ -116,8 +116,8 @@ public function generatePlainPolicy(string $name): void
116116
'namespace' => $this->getNamespace(),
117117
'class' => $this->getClassName($name),
118118
])->reduce(
119-
fn ($carry, $value, $key) => Str::replace(
120-
'{{ '.$key.' }}',
119+
fn($carry, $value, $key) => Str::replace(
120+
'{{ ' . $key . ' }}',
121121
$value,
122122
$carry
123123
)
@@ -137,7 +137,7 @@ private function generatePolicy(string $name, string $model): void
137137
{
138138
if (
139139
file_exists($this->getPolicyPath($name)) &&
140-
! $this->option('force')
140+
!$this->option('force')
141141
) {
142142
$this->error(sprintf('Policy "%s" already exists!', $name));
143143

@@ -147,7 +147,8 @@ private function generatePolicy(string $name, string $model): void
147147
$compiled = collect([
148148
'name' => $name,
149149
'model' => $model,
150-
'modelVariable' => strtolower($model) ===
150+
'modelVariable' =>
151+
strtolower($model) ===
151152
strtolower(
152153
Str::afterLast($this->getNamespacedUserModel(), '\\')
153154
)
@@ -161,8 +162,8 @@ private function generatePolicy(string $name, string $model): void
161162
'namespacedUserModel' => $this->getNamespacedUserModel(),
162163
'user' => Str::afterLast($this->getNamespacedUserModel(), '\\'),
163164
])->reduce(
164-
static fn ($old, $value, $key) => Str::replace(
165-
'{{ '.$key.' }}',
165+
static fn($old, $value, $key) => Str::replace(
166+
'{{ ' . $key . ' }}',
166167
$value,
167168
$old
168169
),
@@ -175,11 +176,12 @@ private function generatePolicy(string $name, string $model): void
175176
/**
176177
* Get the path to the policy.
177178
*
179+
* @param string $name
178180
* @return string
179181
*/
180182
public function getPolicyPath(string $name): string
181183
{
182-
return app_path('Policies/'.$this->getClassName($name).'.php');
184+
return app_path('Policies/' . $this->getClassName($name) . '.php');
183185
}
184186

185187
/**
@@ -189,7 +191,7 @@ public function getPolicyPath(string $name): string
189191
*/
190192
public function getNamespace(): string
191193
{
192-
return app()->getNamespace().'Policies';
194+
return app()->getNamespace() . 'Policies';
193195
}
194196

195197
/**
@@ -204,7 +206,7 @@ public function getClassName(string $name): string
204206
return Str::studly($name);
205207
}
206208

207-
return Str::studly($name).'Policy';
209+
return Str::studly($name) . 'Policy';
208210
}
209211

210212
/**
@@ -215,7 +217,7 @@ public function getClassName(string $name): string
215217
*/
216218
public function getNamespacedModel(string $model): string
217219
{
218-
return app()->getNamespace().'Models\\'.Str::studly($model);
220+
return app()->getNamespace() . 'Models\\' . Str::studly($model);
219221
}
220222

221223
/**
@@ -235,6 +237,6 @@ public function getNamespacedUserModel(): string
235237
*/
236238
public function getStub(): string
237239
{
238-
return __DIR__.'/stubs/policy.stub';
240+
return __DIR__ . '/stubs/policy.stub';
239241
}
240242
}

src/LaravelAuthorizerServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public function configurePackage(Package $package): void
2020
$package
2121
->name('laravel-authorizer')
2222
->hasConfigFile()
23-
->hasViews()
24-
->hasMigration('create_laravel-authorizer_table')
2523
->hasCommands([
2624
LaravelAuthorizerCommand::class,
2725
GeneratePermissionsCommand::class,

0 commit comments

Comments
 (0)