Skip to content

Commit

Permalink
Fix extensions of contextual bindings (#53514)
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon authored Nov 14, 2024
1 parent a296e81 commit 377ba1d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,9 @@ protected function resolve($abstract, $parameters = [], $raiseEvents = true)
// Before returning, we will also set the resolved flag to "true" and pop off
// the parameter overrides for this build. After those two things are done
// we will be ready to return back the fully constructed class instance.
$this->resolved[$abstract] = true;
if (! $needsContextualBuild) {
$this->resolved[$abstract] = true;
}

array_pop($this->with);

Expand Down
57 changes: 57 additions & 0 deletions tests/Container/ContainerExtendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,43 @@ public function testUnsetExtend()

$this->assertSame('foo', $container->make('foo'));
}

public function testExtendContextualBinding()
{
$container = new Container();
$container->when(ContainerExtendConsumesInterfaceStub::class)
->needs(ContainerExtendInterfaceStub::class)
->give(fn () => new ContainerExtendInterfaceImplementationStub('foo'));

$container->extend(ContainerExtendInterfaceStub::class, function ($instance) {
self::assertInstanceOf(ContainerExtendInterfaceImplementationStub::class, $instance);
self::assertSame('foo', $instance->value);

return new ContainerExtendInterfaceImplementationStub('bar');
});

self::assertSame('bar', $container->make(ContainerExtendConsumesInterfaceStub::class)->stub->value);
}

// https://github.com/laravel/framework/issues/53501
public function testExtendContextualBindingAfterResolution()
{
$container = new Container();
$container->when(ContainerExtendConsumesInterfaceStub::class)
->needs(ContainerExtendInterfaceStub::class)
->give(fn () => new ContainerExtendInterfaceImplementationStub('foo'));

$container->make(ContainerExtendConsumesInterfaceStub::class);

$container->extend(ContainerExtendInterfaceStub::class, function ($instance) {
self::assertInstanceOf(ContainerExtendInterfaceImplementationStub::class, $instance);
self::assertSame('foo', $instance->value);

return new ContainerExtendInterfaceImplementationStub('bar');
});

self::assertSame('bar', $container->make(ContainerExtendConsumesInterfaceStub::class)->stub->value);
}
}

class ContainerLazyExtendStub
Expand All @@ -198,3 +235,23 @@ public function init()
static::$initialized = true;
}
}

interface ContainerExtendInterfaceStub
{
}

class ContainerExtendInterfaceImplementationStub implements ContainerExtendInterfaceStub
{
public function __construct(
public string $value,
) {
}
}

class ContainerExtendConsumesInterfaceStub
{
public function __construct(
public ContainerExtendInterfaceStub $stub,
) {
}
}

0 comments on commit 377ba1d

Please sign in to comment.