Skip to content

Commit

Permalink
Fix for booted callbacks being called twice
Browse files Browse the repository at this point in the history
Booted callbacks which are added during the execution of other callbacks
are being called twice. This commit is an alternative inmplementation of
a fix for this issue. Originial PR #53683
  • Loading branch information
simonworkhouse committed Nov 28, 2024
1 parent e7b12ff commit b01320f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class Application extends Container implements ApplicationContract, CachesConfig
*/
protected $booted = false;

/**
* Indicates if the application is currently firing booted callbacks.
*
* @var bool
*/
protected $firingBootedCallbacks = false;

/**
* The array of booting callbacks.
*
Expand Down Expand Up @@ -1082,6 +1089,16 @@ public function isBooted()
return $this->booted;
}

/**
* Determine if the application is currently firing booted callbacks.
*
* @return bool
*/
public function isFiringBootedCallbacks()
{
return $this->firingBootedCallbacks;
}

/**
* Boot the application's service providers.
*
Expand All @@ -1103,8 +1120,13 @@ public function boot()
});

$this->booted = true;
$this->firingBootedCallbacks = true;

$this->fireAppCallbacks($this->bootedCallbacks);
try {
$this->fireAppCallbacks($this->bootedCallbacks);
} finally {
$this->firingBootedCallbacks = false;
}
}

/**
Expand Down Expand Up @@ -1145,7 +1167,7 @@ public function booted($callback)
{
$this->bootedCallbacks[] = $callback;

if ($this->isBooted()) {
if ($this->isBooted() && ! $this->isFiringBootedCallbacks()) {
$callback($this);
}
}
Expand Down
9 changes: 7 additions & 2 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ public function testBootedCallbacks()
$this->assertSame($application, $app);
};

$closure2Booted = function ($app) use ($closure2) {
$app->booted($closure2);
};

$closure3 = function ($app) use (&$counter, $application) {
$counter++;
$this->assertSame($application, $app);
Expand All @@ -373,13 +377,14 @@ public function testBootedCallbacks()
$application->booting($closure);
$application->booted($closure);
$application->booted($closure2);
$application->booted($closure2Booted);
$application->boot();

$this->assertEquals(3, $counter);
$this->assertEquals(4, $counter);

$application->booted($closure3);

$this->assertEquals(4, $counter);
$this->assertEquals(5, $counter);
}

public function testGetNamespace()
Expand Down

0 comments on commit b01320f

Please sign in to comment.