Skip to content

Commit

Permalink
Register about details (#2584)
Browse files Browse the repository at this point in the history
* Register package information in About command output

* Fix styling
  • Loading branch information
drbyte authored Apr 18, 2024
1 parent d191ada commit 13ff85b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Spatie\Permission;

use Composer\InstalledVersions;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -38,6 +40,8 @@ public function boot()
});

$this->app->singleton(PermissionRegistrar::class);

$this->registerAbout();
}

public function register()
Expand Down Expand Up @@ -169,4 +173,27 @@ protected function getMigrationFileName(string $migrationFileName): string
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
->first();
}

protected function registerAbout(): void
{
if (! class_exists(InstalledVersions::class) || ! class_exists(AboutCommand::class)) {
return;
}

$features = [
'Teams' => 'teams',
'Wildcard-Permissions' => 'enable_wildcard_permission',
'Octane-Listener' => 'register_octane_reset_listener',
'Passport' => 'use_passport_client_credentials',
];

AboutCommand::add('Spatie Permissions', fn () => [
'Features Enabled' => collect($features)
->filter(fn (string $feature, string $name): bool => $this->app['config']->get("permission.{$feature}"))
->keys()
->whenEmpty(fn (Collection $collection) => $collection->push('Default'))
->join(', '),
'Version' => InstalledVersions::getPrettyVersion('spatie/laravel-permission'),
]);
}
}
18 changes: 18 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,22 @@ public function it_can_show_roles_by_teams()
$this->assertRegExp('/\|\s+\|\s+testRole\s+\|\s+testRole_2\s+\|\s+testRole_Team\s+\|\s+testRole_Team\s+\|/', $output);
}
}

/** @test */
public function it_can_respond_to_about_command()
{
config()->set('permission.teams', true);
app(\Spatie\Permission\PermissionRegistrar::class)->initializeCache();

Artisan::call('about');

$output = Artisan::output();

$pattern = '/Spatie Permissions[ .\n]*Features Enabled[ .]*Teams[ .\n]*Version/';
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $output);
} else { // phpUnit 9/8
$this->assertRegExp($pattern, $output);
}
}
}

0 comments on commit 13ff85b

Please sign in to comment.