From 13ff85b1fe61e8dcf7acff839a8cc7cc79685933 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Thu, 18 Apr 2024 18:33:13 -0400 Subject: [PATCH] Register about details (#2584) * Register package information in About command output * Fix styling --- src/PermissionServiceProvider.php | 27 +++++++++++++++++++++++++++ tests/CommandTest.php | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/PermissionServiceProvider.php b/src/PermissionServiceProvider.php index b3a77308c..1273b8c1c 100644 --- a/src/PermissionServiceProvider.php +++ b/src/PermissionServiceProvider.php @@ -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; @@ -38,6 +40,8 @@ public function boot() }); $this->app->singleton(PermissionRegistrar::class); + + $this->registerAbout(); } public function register() @@ -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'), + ]); + } } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 923cad4e0..d4ee4bd18 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -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); + } + } }