Skip to content

Commit

Permalink
[11.x] Fix using container nesting to make the same 'abstract' in dif…
Browse files Browse the repository at this point in the history
…ferent context (#51989)

* Fix using container nesting to make the same 'abstract' in different context

* formatting'
:

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
c-v-c-v and taylorotwell authored Jul 2, 2024
1 parent b6a2abd commit 8d20a9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,13 @@ public function build($concrete)
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $this->getLastParameterOverride());
$this->buildStack[] = spl_object_hash($concrete);

try {
return $concrete($this, $this->getLastParameterOverride());
} finally {
array_pop($this->buildStack);
}
}

try {
Expand Down
15 changes: 15 additions & 0 deletions tests/Container/ContextualBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public function testContainerCanInjectDifferentImplementationsDependingOnContext

$this->assertInstanceOf(ContainerContextImplementationStub::class, $one->impl);
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $two->impl);

/*
* Test nesting to make the same 'abstract' in different context
*/
$container = new Container;

$container->bind(IContainerContextContractStub::class, ContainerContextImplementationStub::class);

$container->when(ContainerTestContextInjectOne::class)->needs(IContainerContextContractStub::class)->give(function ($container) {
return $container->make(IContainerContextContractStub::class);
});

$one = $container->make(ContainerTestContextInjectOne::class);

$this->assertInstanceOf(ContainerContextImplementationStub::class, $one->impl);
}

public function testContextualBindingWorksForExistingInstancedBindings()
Expand Down

0 comments on commit 8d20a9c

Please sign in to comment.