diff --git a/src/Illuminate/Foundation/Console/OptimizeClearCommand.php b/src/Illuminate/Foundation/Console/OptimizeClearCommand.php index 02c9d5ab6cf5..bf4e666d71bc 100644 --- a/src/Illuminate/Foundation/Console/OptimizeClearCommand.php +++ b/src/Illuminate/Foundation/Console/OptimizeClearCommand.php @@ -3,6 +3,7 @@ namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; +use Illuminate\Support\ServiceProvider; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize:clear')] @@ -31,15 +32,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..14ec87c0fcc8 100644 --- a/src/Illuminate/Foundation/Console/OptimizeCommand.php +++ b/src/Illuminate/Foundation/Console/OptimizeCommand.php @@ -3,6 +3,7 @@ namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; +use Illuminate\Support\ServiceProvider; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize')] @@ -31,13 +32,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::$optimizingCommands, + ]; + } } diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index 607431363e2a..0516bd94f529 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 $optimizingCommands = []; + + /** + * 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::$optimizingCommands[$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..b2d585c4456a --- /dev/null +++ b/tests/Integration/Foundation/Console/OptimizeClearCommandTest.php @@ -0,0 +1,37 @@ +artisan('optimize:clear') + ->assertSuccessful() + ->expectsOutputToContain('my package'); + } +} + +class ServiceProviderWithOptimizeClear 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..30d7b26f4b65 --- /dev/null +++ b/tests/Integration/Foundation/Console/OptimizeCommandTest.php @@ -0,0 +1,37 @@ +artisan('optimize') + ->assertSuccessful() + ->expectsOutputToContain('my package'); + } +} + +class ServiceProviderWithOptimize extends ServiceProvider +{ + public function boot(): void + { + $this->commands([ + new ClosureCommand('my_package:cache', fn () => 0), + ]); + + $this->registerOptimizeCommands( + key: 'my package', + optimize: 'my_package:cache', + ); + } +}