Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix extensions of contextual bindings #53514

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
) {
}
}