Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Skip object construction if no rebound callbacks are set #53502

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,13 @@ public function refresh($abstract, $target, $method)
*/
protected function rebound($abstract)
{
if (! $callbacks = $this->getReboundCallbacks($abstract)) {
return;
}

$instance = $this->make($abstract);

foreach ($this->getReboundCallbacks($abstract) as $callback) {
foreach ($callbacks as $callback) {
$callback($this, $instance);
}
}
Expand Down
51 changes: 43 additions & 8 deletions tests/Container/ResolvingCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,44 @@ public function testParametersPassedIntoResolvingCallbacks()
$container->make(ResolvingContractStub::class);
}

public function testResolvingCallbacksAreCallWhenRebindHappenForResolvedAbstract()
public function testResolvingCallbacksAreCallWhenRebindHappens()
{
$container = new Container;

$resolvingCallCounter = 0;
$container->resolving(ResolvingContractStub::class, function () use (&$resolvingCallCounter) {
$resolvingCallCounter++;
});

$rebindCallCounter = 0;
$container->rebinding(ResolvingContractStub::class, function () use (&$rebindCallCounter) {
$rebindCallCounter++;
});

$container->bind(ResolvingContractStub::class, ResolvingImplementationStub::class);

$container->make(ResolvingContractStub::class);
$this->assertEquals(1, $resolvingCallCounter);
$this->assertEquals(0, $rebindCallCounter);

$container->bind(ResolvingContractStub::class, ResolvingImplementationStubTwo::class);
$this->assertEquals(2, $resolvingCallCounter);
$this->assertEquals(1, $rebindCallCounter);

$container->make(ResolvingImplementationStubTwo::class);
$this->assertEquals(3, $resolvingCallCounter);
$this->assertEquals(1, $rebindCallCounter);

$container->bind(ResolvingContractStub::class, fn () => new ResolvingImplementationStubTwo);
$this->assertEquals(4, $resolvingCallCounter);
$this->assertEquals(2, $rebindCallCounter);

$container->make(ResolvingContractStub::class);
$this->assertEquals(5, $resolvingCallCounter);
$this->assertEquals(2, $rebindCallCounter);
}

public function testResolvingCallbacksArentCalledWhenNoRebindingsAreRegistered()
{
$container = new Container;

Expand All @@ -297,18 +334,16 @@ public function testResolvingCallbacksAreCallWhenRebindHappenForResolvedAbstract
$this->assertEquals(1, $callCounter);

$container->bind(ResolvingContractStub::class, ResolvingImplementationStubTwo::class);
$this->assertEquals(2, $callCounter);
$this->assertEquals(1, $callCounter);

$container->make(ResolvingImplementationStubTwo::class);
$this->assertEquals(3, $callCounter);
$this->assertEquals(2, $callCounter);

$container->bind(ResolvingContractStub::class, function () {
return new ResolvingImplementationStubTwo;
});
$this->assertEquals(4, $callCounter);
$container->bind(ResolvingContractStub::class, fn () => new ResolvingImplementationStubTwo);
$this->assertEquals(2, $callCounter);

$container->make(ResolvingContractStub::class);
$this->assertEquals(5, $callCounter);
$this->assertEquals(3, $callCounter);
}

public function testRebindingDoesNotAffectMultipleResolvingCallbacks()
Expand Down