Skip to content

Commit 4cee605

Browse files
committed
Add resolved state tracking to Loop
1 parent 1bc3b3d commit 4cee605

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Loop.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Loop
2020

2121
protected string $context = '';
2222

23+
protected bool $hasBeenResolved = false;
24+
2325
public function __construct()
2426
{
2527
$this->tools = new ToolCollection;
@@ -36,7 +38,9 @@ public function context(string $context): static
3638

3739
public function tool(Tool $tool): static
3840
{
39-
$this->tools->push($tool);
41+
if (! $this->hasBeenResolved) {
42+
$this->tools->push($tool);
43+
}
4044

4145
$this->storeRegistration(function (Loop $loop) use ($tool) {
4246
$loop->tools->push($tool);
@@ -47,8 +51,10 @@ public function tool(Tool $tool): static
4751

4852
public function toolkit(Toolkit $toolkit): static
4953
{
50-
foreach ($toolkit->getTools() as $tool) {
51-
$this->tools->push($tool);
54+
if (! $this->hasBeenResolved) {
55+
foreach ($toolkit->getTools() as $tool) {
56+
$this->tools->push($tool);
57+
}
5258
}
5359

5460
$this->storeRegistration(function (Loop $loop) use ($toolkit) {
@@ -132,4 +138,14 @@ protected function storeRegistration(callable $callback): void
132138
$registrations[] = $callback;
133139
app()->instance('loop.registrations', $registrations);
134140
}
141+
142+
public function markAsResolved(): void
143+
{
144+
$this->hasBeenResolved = true;
145+
}
146+
147+
public function hasBeenResolved(): bool
148+
{
149+
return $this->hasBeenResolved;
150+
}
135151
}

src/LoopServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ public function packageBooted(): void
4646
* Set up container resolving hook to replay tool registrations for Octane compatibility
4747
*/
4848
$this->app->resolving(Loop::class, function ($loop, $app) {
49+
$loop->markAsResolved();
50+
4951
if ($app->bound('loop.registrations')) {
5052
$registrations = $app->make('loop.registrations');
51-
5253
if (is_array($registrations)) {
5354
foreach ($registrations as $registration) {
5455
if (is_callable($registration)) {

0 commit comments

Comments
 (0)