From 609d3e10180920cb5ae2aab971e297115258554a Mon Sep 17 00:00:00 2001 From: C&V Date: Tue, 2 Jul 2024 16:16:42 +0800 Subject: [PATCH 1/2] Fix using container nesting to make the same 'abstract' in different context --- src/Illuminate/Container/Container.php | 7 ++++++- tests/Container/ContextualBindingTest.php | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 1fdaadee322..44322197992 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -925,7 +925,12 @@ 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 4313b25900a..1dae46248d7 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() From 19e2c6ef1531e93627846976df71a291ec532155 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 2 Jul 2024 10:41:02 -0500 Subject: [PATCH 2/2] formatting' : --- src/Illuminate/Container/Container.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Container/Container.php b/src/Illuminate/Container/Container.php index 44322197992..8dd74a8f71a 100755 --- a/src/Illuminate/Container/Container.php +++ b/src/Illuminate/Container/Container.php @@ -926,6 +926,7 @@ public function build($concrete) // used as resolvers for more fine-tuned resolution of these objects. if ($concrete instanceof Closure) { $this->buildStack[] = spl_object_hash($concrete); + try { return $concrete($this, $this->getLastParameterOverride()); } finally {