From 5d35dae9588624ad1bda45c21dfc09ef6aad3fe9 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Wed, 25 Sep 2024 15:55:07 +0200 Subject: [PATCH] [11.x] Optimize events --- .../Console/OptimizeClearCommand.php | 10 +++++ .../Foundation/Console/OptimizeCommand.php | 10 +++++ .../Foundation/Events/OptimizeClearing.php | 10 +++++ .../Foundation/Events/Optimizing.php | 10 +++++ .../Console/OptimizeClearCommandTest.php | 45 +++++++++++++++++++ .../Console/OptimizeCommandTest.php | 43 ++++++++++++++++++ 6 files changed, 128 insertions(+) create mode 100644 src/Illuminate/Foundation/Events/OptimizeClearing.php create mode 100644 src/Illuminate/Foundation/Events/Optimizing.php create mode 100644 tests/Foundation/Console/OptimizeClearCommandTest.php create mode 100644 tests/Foundation/Console/OptimizeCommandTest.php diff --git a/src/Illuminate/Foundation/Console/OptimizeClearCommand.php b/src/Illuminate/Foundation/Console/OptimizeClearCommand.php index 02c9d5ab6cf..fa0d8fceff8 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\Foundation\Events\OptimizeClearing; 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. * @@ -40,6 +48,8 @@ public function handle() 'views' => fn () => $this->callSilent('view:clear') == 0, ])->each(fn ($task, $description) => $this->components->task($description, $task)); + $this->events->dispatch(new OptimizeClearing()); + $this->newLine(); } } diff --git a/src/Illuminate/Foundation/Console/OptimizeCommand.php b/src/Illuminate/Foundation/Console/OptimizeCommand.php index 0bcf3e97a3a..e2e25c77ef7 100644 --- a/src/Illuminate/Foundation/Console/OptimizeCommand.php +++ b/src/Illuminate/Foundation/Console/OptimizeCommand.php @@ -3,6 +3,8 @@ namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; +use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Foundation\Events\Optimizing; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'optimize')] @@ -22,6 +24,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. * @@ -38,6 +46,8 @@ public function handle() 'views' => fn () => $this->callSilent('view:cache') == 0, ])->each(fn ($task, $description) => $this->components->task($description, $task)); + $this->events->dispatch(new Optimizing()); + $this->newLine(); } } diff --git a/src/Illuminate/Foundation/Events/OptimizeClearing.php b/src/Illuminate/Foundation/Events/OptimizeClearing.php new file mode 100644 index 00000000000..cd845a9ab48 --- /dev/null +++ b/src/Illuminate/Foundation/Events/OptimizeClearing.php @@ -0,0 +1,10 @@ +app = new Application( + new \Illuminate\Foundation\Application(__DIR__), + $this->events = new Dispatcher(), + 'testing', + ); + + $this->app->addCommands([ + new OptimizeClearCommand($this->events), + new ClosureCommand('cache:clear', fn () => 0), + new ClosureCommand('clear-compiled', fn () => 0), + new ClosureCommand('config:clear', fn () => 0), + new ClosureCommand('event:clear', fn () => 0), + new ClosureCommand('route:clear', fn () => 0), + new ClosureCommand('view:clear', fn () => 0), + ]); + } + + public function testCanListenToOptimizingEvent(): void + { + $this->events->listen(function (OptimizeClearing $event) use (&$called) { + $called = true; + }); + + $this->app->call('optimize:clear'); + + $this->assertTrue($called); + } +} diff --git a/tests/Foundation/Console/OptimizeCommandTest.php b/tests/Foundation/Console/OptimizeCommandTest.php new file mode 100644 index 00000000000..0450627807b --- /dev/null +++ b/tests/Foundation/Console/OptimizeCommandTest.php @@ -0,0 +1,43 @@ +app = new Application( + new \Illuminate\Foundation\Application(__DIR__), + $this->events = new Dispatcher(), + 'testing', + ); + + $this->app->addCommands([ + new OptimizeCommand($this->events), + new ClosureCommand('config:cache', fn () => 0), + new ClosureCommand('event:cache', fn () => 0), + new ClosureCommand('route:cache', fn () => 0), + new ClosureCommand('view:cache', fn () => 0), + ]); + } + + public function testCanListenToOptimizingEvent(): void + { + $this->events->listen(function (Optimizing $event) use (&$called) { + $called = true; + }); + + $this->app->call('optimize'); + + $this->assertTrue($called); + } +}