Skip to content

Commit 964501a

Browse files
author
Given Ncube
committed
feat: added the ability to setup and scaffold the package in one command
1 parent 78f7b17 commit 964501a

File tree

6 files changed

+111
-16
lines changed

6 files changed

+111
-16
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
4141
- name: Install dependencies
4242
run: |
43-
composer install --no-interaction --prefer-dist
43+
composer install --no-interaction --prefer-dist --quiet
4444
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
4545
4646
- name: Execute tests

phpstan.neon.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ parameters:
1111
checkOctaneCompatibility: true
1212
checkModelProperties: true
1313
checkMissingIterableValueType: false
14+
reportUnmatchedIgnoredErrors: true
1415

16+
ignoreErrors:
17+
- "#with a nullable type declaration#"
18+
- "#type mixed is not subtype of native#"
19+
- "#is not allowed to extend#"
20+
- "#Language construct eval#"
21+
- "# with null as default value#"
22+
- "#has parameter \\$closure with default value.#"
23+
- "#has parameter \\$description with default value.#"
24+
- "#Method Pest\\\\Support\\\\Reflection::getParameterClassName\\(\\) has a nullable return type declaration.#"

src/Commands/LaravelAuthorizerCommand.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LaravelAuthorizerCommand extends Command
1616
*
1717
* @var string
1818
*/
19-
public $signature = 'authorizer:generate {name? : The name of the policy to generate} {--m|model= : The model to use for the policy} {--f|force : Overwrite existing files}';
19+
public $signature = 'authorizer:policies:generate {name? : The name of the policy to generate} {--m|model= : The model to use for the policy} {--f|force : Overwrite existing files}';
2020

2121
/**
2222
* The console command description.
@@ -66,7 +66,7 @@ public function handle(): int
6666
public function generateAllPolicies(): void
6767
{
6868
$this->getModels()->each(
69-
fn (string $model) => $this->generatePolicy($model, $model)
69+
fn(string $model) => $this->generatePolicy($model, $model)
7070
);
7171
}
7272

@@ -80,7 +80,7 @@ public function generatePlainPolicy(string $name): void
8080
{
8181
if (
8282
file_exists($this->getPolicyPath($name)) &&
83-
! $this->option('force')
83+
!$this->option('force')
8484
) {
8585
$this->error(sprintf('Policy "%s" already exists!', $name));
8686

@@ -92,8 +92,8 @@ public function generatePlainPolicy(string $name): void
9292
'namespace' => $this->getNamespace(),
9393
'class' => $this->getClassName($name),
9494
])->reduce(
95-
fn ($carry, $value, $key) => Str::replace(
96-
'{{ '.$key.' }}',
95+
fn($carry, $value, $key) => Str::replace(
96+
'{{ ' . $key . ' }}',
9797
$value,
9898
$carry
9999
)
@@ -113,7 +113,7 @@ private function generatePolicy(string $name, string $model): void
113113
{
114114
if (
115115
file_exists($this->getPolicyPath($name)) &&
116-
! $this->option('force')
116+
!$this->option('force')
117117
) {
118118
$this->error(sprintf('Policy "%s" already exists!', $name));
119119

@@ -123,7 +123,8 @@ private function generatePolicy(string $name, string $model): void
123123
$compiled = collect([
124124
'name' => $name,
125125
'model' => $model,
126-
'modelVariable' => strtolower($model) ===
126+
'modelVariable' =>
127+
strtolower($model) ===
127128
strtolower(
128129
Str::afterLast($this->getNamespacedUserModel(), '\\')
129130
)
@@ -137,8 +138,8 @@ private function generatePolicy(string $name, string $model): void
137138
'namespacedUserModel' => $this->getNamespacedUserModel(),
138139
'user' => Str::afterLast($this->getNamespacedUserModel(), '\\'),
139140
])->reduce(
140-
static fn ($old, $value, $key) => Str::replace(
141-
'{{ '.$key.' }}',
141+
static fn($old, $value, $key) => Str::replace(
142+
'{{ ' . $key . ' }}',
142143
$value,
143144
$old
144145
),
@@ -156,7 +157,7 @@ private function generatePolicy(string $name, string $model): void
156157
*/
157158
public function getPolicyPath(string $name): string
158159
{
159-
return app_path('Policies/'.$this->getClassName($name).'.php');
160+
return app_path('Policies/' . $this->getClassName($name) . '.php');
160161
}
161162

162163
/**
@@ -166,7 +167,7 @@ public function getPolicyPath(string $name): string
166167
*/
167168
public function getNamespace(): string
168169
{
169-
return app()->getNamespace().'Policies';
170+
return app()->getNamespace() . 'Policies';
170171
}
171172

172173
/**
@@ -181,7 +182,7 @@ public function getClassName(string $name): string
181182
return Str::studly($name);
182183
}
183184

184-
return Str::studly($name).'Policy';
185+
return Str::studly($name) . 'Policy';
185186
}
186187

187188
/**
@@ -192,7 +193,7 @@ public function getClassName(string $name): string
192193
*/
193194
public function getNamespacedModel(string $model): string
194195
{
195-
return app()->getNamespace().'Models\\'.Str::studly($model);
196+
return app()->getNamespace() . 'Models\\' . Str::studly($model);
196197
}
197198

198199
/**
@@ -212,6 +213,6 @@ public function getNamespacedUserModel(): string
212213
*/
213214
public function getStub(): string
214215
{
215-
return __DIR__.'/stubs/policy.stub';
216+
return __DIR__ . '/stubs/policy.stub';
216217
}
217218
}

src/Commands/SetupCommand.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace FlixtechsLabs\LaravelAuthorizer\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Spatie\Permission\PermissionServiceProvider;
7+
8+
class SetupCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'authorizer:setup {--p|permissions : Generate all permissions} {--P|policies : Generate all policies}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Setup the authorizer package';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return int
28+
*/
29+
public function handle(): int
30+
{
31+
$this->call('vendor:publish', [
32+
'--provider' => PermissionServiceProvider::class,
33+
]);
34+
35+
$this->call('migrate');
36+
37+
if ($this->option('permissions')) {
38+
$this->info('Generating permissions...');
39+
$this->call('authorizer:permissions:generate');
40+
}
41+
42+
if ($this->option('policies')) {
43+
$this->info('Generating policies...');
44+
$this->call('authorizer:policies:generate');
45+
}
46+
47+
$this->info('Setup complete!');
48+
49+
return self::SUCCESS;
50+
}
51+
}

src/LaravelAuthorizerServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use FlixtechsLabs\LaravelAuthorizer\Commands\GeneratePermissionsCommand;
66
use FlixtechsLabs\LaravelAuthorizer\Commands\LaravelAuthorizerCommand;
7+
use FlixtechsLabs\LaravelAuthorizer\Commands\SetupCommand;
78
use Illuminate\Foundation\Console\AboutCommand;
89
use Spatie\LaravelPackageTools\Package;
910
use Spatie\LaravelPackageTools\PackageServiceProvider;
@@ -23,6 +24,7 @@ public function configurePackage(Package $package): void
2324
->hasCommands([
2425
LaravelAuthorizerCommand::class,
2526
GeneratePermissionsCommand::class,
27+
SetupCommand::class,
2628
]);
2729
}
2830

@@ -32,7 +34,7 @@ public function boot()
3234

3335
AboutCommand::add(
3436
'Laravel Authorizer',
35-
fn () => [
37+
fn() => [
3638
'version' => '0.0.1',
3739
'author' => 'Flixtechs Labs',
3840
'license' => 'MIT',

tests/SetupTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use FlixtechsLabs\LaravelAuthorizer\Commands\SetupCommand;
4+
5+
it('can setup the authorizer package', function () {
6+
$this->artisan(SetupCommand::class)
7+
->expectsOutput('Setup complete!')
8+
->assertSuccessful();
9+
});
10+
11+
it('can generate permissions on setup', function () {
12+
$this->artisan('make:model', ['name' => 'User'])->assertSuccessful();
13+
14+
$this->artisan(SetupCommand::class, [
15+
'--permissions' => true,
16+
])->assertSuccessful();
17+
18+
$this->assertDatabaseHas('permissions', [
19+
'name' => 'create user',
20+
]);
21+
});
22+
23+
it('can generate policies on setup', function () {
24+
$this->artisan('make:model', ['name' => 'User'])->assertSuccessful();
25+
26+
$this->artisan(SetupCommand::class, [
27+
'--policies' => true,
28+
])->assertSuccessful();
29+
30+
$this->assertFileExists(app_path('Policies/UserPolicy.php'));
31+
});

0 commit comments

Comments
 (0)