Skip to content

Commit

Permalink
Replace while loop with array_walk
Browse files Browse the repository at this point in the history
  • Loading branch information
seriquynh committed Jun 14, 2024
1 parent 741560f commit afc5300
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
20 changes: 6 additions & 14 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down Expand Up @@ -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)
);
}

/**
Expand Down
20 changes: 6 additions & 14 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand All @@ -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)
);
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/Support/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Illuminate\Tests\Support;

use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use PHPUnit\Framework\TestCase;

class ServiceProviderTest extends TestCase
{
public function testItFiresBootingCallbacksSequentially ()
{
$provider = new StuffServiceProvider(
new Application()
);

$values = [];

$provider->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
{
//
}

0 comments on commit afc5300

Please sign in to comment.