diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 1fdaadee322f..8dd74a8f71a3 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -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 { diff --git a/tests/Container/ContextualBindingTest.php b/tests/Container/ContextualBindingTest.php index 4313b25900a1..1dae46248d7e 100644 --- a/tests/Container/ContextualBindingTest.php +++ b/tests/Container/ContextualBindingTest.php @@ -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()