Skip to content

Commit 8d20a9c

Browse files
[11.x] Fix using container nesting to make the same 'abstract' in different context (#51989)
* Fix using container nesting to make the same 'abstract' in different context * formatting' : --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b6a2abd commit 8d20a9c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Illuminate/Container/Container.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,13 @@ public function build($concrete)
925925
// hand back the results of the functions, which allows functions to be
926926
// used as resolvers for more fine-tuned resolution of these objects.
927927
if ($concrete instanceof Closure) {
928-
return $concrete($this, $this->getLastParameterOverride());
928+
$this->buildStack[] = spl_object_hash($concrete);
929+
930+
try {
931+
return $concrete($this, $this->getLastParameterOverride());
932+
} finally {
933+
array_pop($this->buildStack);
934+
}
929935
}
930936

931937
try {

tests/Container/ContextualBindingTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ public function testContainerCanInjectDifferentImplementationsDependingOnContext
4040

4141
$this->assertInstanceOf(ContainerContextImplementationStub::class, $one->impl);
4242
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $two->impl);
43+
44+
/*
45+
* Test nesting to make the same 'abstract' in different context
46+
*/
47+
$container = new Container;
48+
49+
$container->bind(IContainerContextContractStub::class, ContainerContextImplementationStub::class);
50+
51+
$container->when(ContainerTestContextInjectOne::class)->needs(IContainerContextContractStub::class)->give(function ($container) {
52+
return $container->make(IContainerContextContractStub::class);
53+
});
54+
55+
$one = $container->make(ContainerTestContextInjectOne::class);
56+
57+
$this->assertInstanceOf(ContainerContextImplementationStub::class, $one->impl);
4358
}
4459

4560
public function testContextualBindingWorksForExistingInstancedBindings()

0 commit comments

Comments
 (0)