From ec5b9a73d1c567baa8ec554cc664e4c821f6099f Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Wed, 25 Sep 2024 15:55:07 +0200 Subject: [PATCH] [11.x] Optimize commands registration --- .../Console/OptimizeClearCommand.php | 32 ++++++++++++---- .../Foundation/Console/OptimizeCommand.php | 29 ++++++++++++--- src/Illuminate/Support/ServiceProvider.php | 25 +++++++++++++ .../Console/OptimizeClearCommandTest.php | 37 +++++++++++++++++++ .../Console/OptimizeCommandTest.php | 37 +++++++++++++++++++ 5 files changed, 146 insertions(+), 14 deletions(-) create mode 100644 tests/Integration/Foundation/Console/OptimizeClearCommandTest.php create mode 100644 tests/Integration/Foundation/Console/OptimizeCommandTest.php diff --git a/src/Illuminate/Foundation/Console/OptimizeClearCommand.php b/src/Illuminate/Foundation/Console/OptimizeClearCommand.php index 02c9d5ab6cf5..0d7ba2dc8722 100644 --- a/src/Illuminate/Foundation/Console/OptimizeClearCommand.php +++ b/src/Illuminate/Foundation/Console/OptimizeClearCommand.php @@ -3,6 +3,8 @@ namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; +use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Support\ServiceProvider; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize:clear')] @@ -22,6 +24,12 @@ class OptimizeClearCommand extends Command */ protected $description = 'Remove the cached bootstrap files'; + public function __construct( + protected Dispatcher $events, + ) { + parent::__construct(); + } + /** * Execute the console command. * @@ -31,15 +39,23 @@ public function handle() { $this->components->info('Clearing cached bootstrap files.'); - collect([ - 'cache' => fn () => $this->callSilent('cache:clear') == 0, - 'compiled' => fn () => $this->callSilent('clear-compiled') == 0, - 'config' => fn () => $this->callSilent('config:clear') == 0, - 'events' => fn () => $this->callSilent('event:clear') == 0, - 'routes' => fn () => $this->callSilent('route:clear') == 0, - 'views' => fn () => $this->callSilent('view:clear') == 0, - ])->each(fn ($task, $description) => $this->components->task($description, $task)); + foreach ($this->getOptimizeClearTasks() as $description => $command) { + $this->components->task($description, fn () => $this->callSilently($command) == 0); + } $this->newLine(); } + + public function getOptimizeClearTasks(): array + { + return [ + 'cache' => 'cache:clear', + 'compiled' => 'clear-compiled', + 'config' => 'config:clear', + 'events' => 'event:clear', + 'routes' => 'route:clear', + 'views' => 'view:clear', + ...ServiceProvider::$optimizeClearingCommands + ]; + } } diff --git a/src/Illuminate/Foundation/Console/OptimizeCommand.php b/src/Illuminate/Foundation/Console/OptimizeCommand.php index 0bcf3e97a3a6..b7cdf3d1915a 100644 --- a/src/Illuminate/Foundation/Console/OptimizeCommand.php +++ b/src/Illuminate/Foundation/Console/OptimizeCommand.php @@ -3,6 +3,9 @@ namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; +use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Foundation\Events\Optimizing; +use Illuminate\Support\ServiceProvider; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize')] @@ -22,6 +25,12 @@ class OptimizeCommand extends Command */ protected $description = 'Cache framework bootstrap, configuration, and metadata to increase performance'; + public function __construct( + protected Dispatcher $events, + ) { + parent::__construct(); + } + /** * Execute the console command. * @@ -31,13 +40,21 @@ public function handle() { $this->components->info('Caching framework bootstrap, configuration, and metadata.'); - collect([ - 'config' => fn () => $this->callSilent('config:cache') == 0, - 'events' => fn () => $this->callSilent('event:cache') == 0, - 'routes' => fn () => $this->callSilent('route:cache') == 0, - 'views' => fn () => $this->callSilent('view:cache') == 0, - ])->each(fn ($task, $description) => $this->components->task($description, $task)); + foreach ($this->getOptimizeTasks() as $description => $command) { + $this->components->task($description, fn () => $this->callSilently($command) == 0); + } $this->newLine(); } + + protected function getOptimizeTasks(): array + { + return [ + 'config' => 'config:cache', + 'events' => 'event:cache', + 'routes' => 'route:cache', + 'views' => 'view:cache', + ...ServiceProvider::$optimizationCommands + ]; + } } diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index 607431363e2a..08692961dd3d 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -51,6 +51,20 @@ abstract class ServiceProvider */ public static $publishGroups = []; + /** + * Commands that should be run during the optimize command. + * + * @var array + */ + public static array $optimizationCommands = []; + + /** + * Commands that should be run during the optimize:clear command. + * + * @var array + */ + public static array $optimizeClearingCommands = []; + /** * The migration paths available for publishing. * @@ -537,4 +551,15 @@ public static function addProviderToBootstrapFile(string $provider, ?string $pat return true; } + + protected function registerOptimizeCommands(string $key, string $optimize = null, string $optimizeClear = null): void + { + if ($optimize) { + static::$optimizationCommands[$key] = $optimize; + } + + if ($optimizeClear) { + static::$optimizeClearingCommands[$key] = $optimizeClear; + } + } } diff --git a/tests/Integration/Foundation/Console/OptimizeClearCommandTest.php b/tests/Integration/Foundation/Console/OptimizeClearCommandTest.php new file mode 100644 index 000000000000..a18bd9155ca7 --- /dev/null +++ b/tests/Integration/Foundation/Console/OptimizeClearCommandTest.php @@ -0,0 +1,37 @@ +artisan('optimize:clear') + ->assertSuccessful() + ->expectsOutputToContain('my package'); + } +} + +class TestServiceProvider extends ServiceProvider +{ + public function boot(): void + { + $this->commands([ + new ClosureCommand('my_package:clear', fn () => 0), + ]); + + $this->registerOptimizeCommands( + key: 'my package', + optimizeClear: 'my_package:clear', + ); + } +} diff --git a/tests/Integration/Foundation/Console/OptimizeCommandTest.php b/tests/Integration/Foundation/Console/OptimizeCommandTest.php new file mode 100644 index 000000000000..cf1b0f20236f --- /dev/null +++ b/tests/Integration/Foundation/Console/OptimizeCommandTest.php @@ -0,0 +1,37 @@ +artisan('optimize') + ->assertSuccessful() + ->expectsOutputToContain('my package'); + } +} + +class TestServiceProvider extends ServiceProvider +{ + public function boot(): void + { + $this->commands([ + new ClosureCommand('my_package:cache', fn () => 0), + ]); + + $this->registerOptimizeCommands( + key: 'my package', + optimize: 'my_package:cache', + ); + } +}