Skip to content

Commit 48a33b4

Browse files
author
Given Ncube
committed
feat: added the ability to generate permissions
2 parents 765a20a + cd40796 commit 48a33b4

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/Commands/LaravelAuthorizerCommand.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class LaravelAuthorizerCommand extends Command
2929

3030
/**
3131
* Run the command.
32+
*
3233
* @return int
3334
*/
3435
public function handle(): int
@@ -84,9 +85,9 @@ public function generateAllPolicies(): void
8485
}
8586

8687
return $reflection->isSubclassOf(Model::class) &&
87-
!$reflection->isAbstract();
88+
! $reflection->isAbstract();
8889
})
89-
->map(fn($model) => Str::afterLast($model, '\\'));
90+
->map(fn ($model) => Str::afterLast($model, '\\'));
9091

9192
$models->each(function (string $model) {
9293
$this->generatePolicy($model, $model);
@@ -96,16 +97,17 @@ public function generateAllPolicies(): void
9697
/**
9798
* Generate a plain policy without a model.
9899
*
99-
* @param string $name
100+
* @param string $name
100101
* @return void
101102
*/
102103
public function generatePlainPolicy(string $name): void
103104
{
104105
if (
105106
file_exists($this->getPolicyPath($name)) &&
106-
!$this->option('force')
107+
! $this->option('force')
107108
) {
108109
$this->error(sprintf('Policy "%s" already exists!', $name));
110+
109111
return;
110112
}
111113

@@ -114,8 +116,8 @@ public function generatePlainPolicy(string $name): void
114116
'namespace' => $this->getNamespace(),
115117
'class' => $this->getClassName($name),
116118
])->reduce(
117-
fn($carry, $value, $key) => Str::replace(
118-
'{{ ' . $key . ' }}',
119+
fn ($carry, $value, $key) => Str::replace(
120+
'{{ '.$key.' }}',
119121
$value,
120122
$carry
121123
)
@@ -127,25 +129,25 @@ public function generatePlainPolicy(string $name): void
127129
/**
128130
* Generate a policy for a given model.
129131
*
130-
* @param string $name
131-
* @param string $model
132+
* @param string $name
133+
* @param string $model
132134
* @return void
133135
*/
134136
private function generatePolicy(string $name, string $model): void
135137
{
136138
if (
137139
file_exists($this->getPolicyPath($name)) &&
138-
!$this->option('force')
140+
! $this->option('force')
139141
) {
140142
$this->error(sprintf('Policy "%s" already exists!', $name));
143+
141144
return;
142145
}
143146

144147
$compiled = collect([
145148
'name' => $name,
146149
'model' => $model,
147-
'modelVariable' =>
148-
strtolower($model) ===
150+
'modelVariable' => strtolower($model) ===
149151
strtolower(
150152
Str::afterLast($this->getNamespacedUserModel(), '\\')
151153
)
@@ -159,8 +161,8 @@ private function generatePolicy(string $name, string $model): void
159161
'namespacedUserModel' => $this->getNamespacedUserModel(),
160162
'user' => Str::afterLast($this->getNamespacedUserModel(), '\\'),
161163
])->reduce(
162-
static fn($old, $value, $key) => Str::replace(
163-
'{{ ' . $key . ' }}',
164+
static fn ($old, $value, $key) => Str::replace(
165+
'{{ '.$key.' }}',
164166
$value,
165167
$old
166168
),
@@ -177,7 +179,7 @@ private function generatePolicy(string $name, string $model): void
177179
*/
178180
public function getPolicyPath(string $name): string
179181
{
180-
return app_path('Policies/' . $this->getClassName($name) . '.php');
182+
return app_path('Policies/'.$this->getClassName($name).'.php');
181183
}
182184

183185
/**
@@ -187,13 +189,13 @@ public function getPolicyPath(string $name): string
187189
*/
188190
public function getNamespace(): string
189191
{
190-
return app()->getNamespace() . 'Policies';
192+
return app()->getNamespace().'Policies';
191193
}
192194

193195
/**
194196
* Get the class name for the policy.
195197
*
196-
* @param string $name The name of the policy
198+
* @param string $name The name of the policy
197199
* @return string
198200
*/
199201
public function getClassName(string $name): string
@@ -202,18 +204,18 @@ public function getClassName(string $name): string
202204
return Str::studly($name);
203205
}
204206

205-
return Str::studly($name) . 'Policy';
207+
return Str::studly($name).'Policy';
206208
}
207209

208210
/**
209211
* Get the namespace for the model.
210212
*
211-
* @param string $model The name of the model
213+
* @param string $model The name of the model
212214
* @return string
213215
*/
214216
public function getNamespacedModel(string $model): string
215217
{
216-
return app()->getNamespace() . 'Models\\' . Str::studly($model);
218+
return app()->getNamespace().'Models\\'.Str::studly($model);
217219
}
218220

219221
/**
@@ -233,6 +235,6 @@ public function getNamespacedUserModel(): string
233235
*/
234236
public function getStub(): string
235237
{
236-
return __DIR__ . '/stubs/policy.stub';
238+
return __DIR__.'/stubs/policy.stub';
237239
}
238240
}

tests/LaravelAuthorizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function () {
4343
'Order',
4444
'OrderItem',
4545
])->each(
46-
fn($model) => $this->artisan('make:model', [
46+
fn ($model) => $this->artisan('make:model', [
4747
'name' => $model,
4848
])
4949
);

tests/TestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ protected function setUp(): void
2121
});
2222

2323
Factory::guessFactoryNamesUsing(
24-
fn(
24+
fn (
2525
string $modelName
26-
) => 'FlixtechsLabs\\LaravelAuthorizer\\Database\\Factories\\' .
27-
class_basename($modelName) .
26+
) => 'FlixtechsLabs\\LaravelAuthorizer\\Database\\Factories\\'.
27+
class_basename($modelName).
2828
'Factory'
2929
);
3030
}

0 commit comments

Comments
 (0)