From afc5300104e18af6800d2a789eb480c6e2761c5e Mon Sep 17 00:00:00 2001 From: Quynh Nguyen Date: Fri, 14 Jun 2024 09:10:18 +0700 Subject: [PATCH] Replace while loop with array_walk --- src/Illuminate/Foundation/Application.php | 20 ++----- src/Illuminate/Support/ServiceProvider.php | 20 ++----- tests/Support/ServiceProviderTest.php | 65 ++++++++++++++++++++++ 3 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 tests/Support/ServiceProviderTest.php diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 086a5339da26..11e2cfd497dd 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -1151,13 +1151,9 @@ public function booted($callback) */ protected function fireAppCallbacks(array &$callbacks) { - $index = 0; - - while ($index < count($callbacks)) { - $callbacks[$index]($this); - - $index++; - } + array_walk( + $callbacks, fn($callback) => $callback($this) + ); } /** @@ -1409,13 +1405,9 @@ public function terminating($callback) */ public function terminate() { - $index = 0; - - while ($index < count($this->terminatingCallbacks)) { - $this->call($this->terminatingCallbacks[$index]); - - $index++; - } + array_walk( + $this->terminatingCallbacks, fn ($callback) => $this->call($callback) + ); } /** diff --git a/src/Illuminate/Support/ServiceProvider.php b/src/Illuminate/Support/ServiceProvider.php index 593366765719..646718dbc405 100755 --- a/src/Illuminate/Support/ServiceProvider.php +++ b/src/Illuminate/Support/ServiceProvider.php @@ -104,13 +104,9 @@ public function booted(Closure $callback) */ public function callBootingCallbacks() { - $index = 0; - - while ($index < count($this->bootingCallbacks)) { - $this->app->call($this->bootingCallbacks[$index]); - - $index++; - } + array_walk( + $this->bootingCallbacks, fn ($callback) => $this->app->call($callback) + ); } /** @@ -120,13 +116,9 @@ public function callBootingCallbacks() */ public function callBootedCallbacks() { - $index = 0; - - while ($index < count($this->bootedCallbacks)) { - $this->app->call($this->bootedCallbacks[$index]); - - $index++; - } + array_walk( + $this->bootedCallbacks, fn($callback) => $this->app->call($callback) + ); } /** diff --git a/tests/Support/ServiceProviderTest.php b/tests/Support/ServiceProviderTest.php new file mode 100644 index 000000000000..f96760cb9fec --- /dev/null +++ b/tests/Support/ServiceProviderTest.php @@ -0,0 +1,65 @@ +booting(function () use (&$values) { + $values[] = 1; + }); + + $provider->booting(function () use (&$values) { + $values[] = 2; + }); + + $provider->booting(function () use (&$values) { + $values[] = 3; + }); + + $provider->callBootingCallbacks(); + + $this->assertSame([1, 2, 3], $values); + } + + public function testItFiresBootedCallbacksSequentially () + { + $provider = new StuffServiceProvider( + new Application() + ); + + $values = []; + + $provider->booted(function () use (&$values) { + $values[] = 1; + }); + + $provider->booted(function () use (&$values) { + $values[] = 2; + }); + + $provider->booted(function () use (&$values) { + $values[] = 3; + }); + + $provider->callBootedCallbacks(); + + $this->assertSame([1, 2, 3], $values); + } +} + +class StuffServiceProvider extends ServiceProvider +{ + // +}