Skip to content

Commit 377ba1d

Browse files
authored
Fix extensions of contextual bindings (#53514)
1 parent a296e81 commit 377ba1d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Illuminate/Container/Container.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,9 @@ protected function resolve($abstract, $parameters = [], $raiseEvents = true)
836836
// Before returning, we will also set the resolved flag to "true" and pop off
837837
// the parameter overrides for this build. After those two things are done
838838
// we will be ready to return back the fully constructed class instance.
839-
$this->resolved[$abstract] = true;
839+
if (! $needsContextualBuild) {
840+
$this->resolved[$abstract] = true;
841+
}
840842

841843
array_pop($this->with);
842844

tests/Container/ContainerExtendTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,43 @@ public function testUnsetExtend()
187187

188188
$this->assertSame('foo', $container->make('foo'));
189189
}
190+
191+
public function testExtendContextualBinding()
192+
{
193+
$container = new Container();
194+
$container->when(ContainerExtendConsumesInterfaceStub::class)
195+
->needs(ContainerExtendInterfaceStub::class)
196+
->give(fn () => new ContainerExtendInterfaceImplementationStub('foo'));
197+
198+
$container->extend(ContainerExtendInterfaceStub::class, function ($instance) {
199+
self::assertInstanceOf(ContainerExtendInterfaceImplementationStub::class, $instance);
200+
self::assertSame('foo', $instance->value);
201+
202+
return new ContainerExtendInterfaceImplementationStub('bar');
203+
});
204+
205+
self::assertSame('bar', $container->make(ContainerExtendConsumesInterfaceStub::class)->stub->value);
206+
}
207+
208+
// https://github.com/laravel/framework/issues/53501
209+
public function testExtendContextualBindingAfterResolution()
210+
{
211+
$container = new Container();
212+
$container->when(ContainerExtendConsumesInterfaceStub::class)
213+
->needs(ContainerExtendInterfaceStub::class)
214+
->give(fn () => new ContainerExtendInterfaceImplementationStub('foo'));
215+
216+
$container->make(ContainerExtendConsumesInterfaceStub::class);
217+
218+
$container->extend(ContainerExtendInterfaceStub::class, function ($instance) {
219+
self::assertInstanceOf(ContainerExtendInterfaceImplementationStub::class, $instance);
220+
self::assertSame('foo', $instance->value);
221+
222+
return new ContainerExtendInterfaceImplementationStub('bar');
223+
});
224+
225+
self::assertSame('bar', $container->make(ContainerExtendConsumesInterfaceStub::class)->stub->value);
226+
}
190227
}
191228

192229
class ContainerLazyExtendStub
@@ -198,3 +235,23 @@ public function init()
198235
static::$initialized = true;
199236
}
200237
}
238+
239+
interface ContainerExtendInterfaceStub
240+
{
241+
}
242+
243+
class ContainerExtendInterfaceImplementationStub implements ContainerExtendInterfaceStub
244+
{
245+
public function __construct(
246+
public string $value,
247+
) {
248+
}
249+
}
250+
251+
class ContainerExtendConsumesInterfaceStub
252+
{
253+
public function __construct(
254+
public ContainerExtendInterfaceStub $stub,
255+
) {
256+
}
257+
}

0 commit comments

Comments
 (0)