You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Its possible to use Container::extend() to extend the behavior of a container service, this makes it easy to compose behavior based on specific needs. However when using this feature together with contextual bindings it may fail depending on a few factors (detailed below).
Steps To Reproduce
Register a contextual binding in the container, this binding must resolve something that the container can't build on its own (e.g. an interface)
Trigger the contextual binding
Register an extension for the $abstract (e.g. the interface) that we bound contextually in the prior step
If you followed steps 1, 2, and 3 an exception will be thrown, as whatever interface you picked cannot be resolved. If you skip step 2, everything works as expected.
Below is some example code that contains both scenarios:
<?phpnamespaceLaravelContainerBugExample;
useIlluminate\Container\Container;
require_once__DIR__ . '/vendor/autoload.php';
interface Foo
{
}
class Bar implements Foo
{
}
class Baz
{
publicfunction__construct(
publicFoo$foo,
) {
}
}
class Qux implements Foo
{
publicfunction__construct(
privateFoo$foo,
) {
}
}
$container = newContainer();
$container->when(Baz::class)->needs(Foo::class)->give(fn () => newBar());
// This works...$app = clone$container;
$app->extend(Foo::class, fn (Foo$foo) => newQux($foo));
$baz = $app->make(Baz::class);
echo$baz->fooinstanceof Qux ? 'Foo implementation was extended' : 'Foo implementation was not extended';
echoPHP_EOL;
// This breaks...$app = clone$container;
$app->make(Baz::class);
$app->extend(Foo::class, fn (Foo$foo) => newQux($foo));
The text was updated successfully, but these errors were encountered:
axlon
changed the title
[11.x] Container::extend() breaks when $abstract was previously contextually boundContainer::extend() breaks when $abstract was previously contextually bound
Nov 14, 2024
Laravel Version
11.31.0
PHP Version
8.3.10
Database Driver & Version
n/a
Description
Its possible to use
Container::extend()
to extend the behavior of a container service, this makes it easy to compose behavior based on specific needs. However when using this feature together with contextual bindings it may fail depending on a few factors (detailed below).Steps To Reproduce
$abstract
(e.g. the interface) that we bound contextually in the prior stepIf you followed steps 1, 2, and 3 an exception will be thrown, as whatever interface you picked cannot be resolved. If you skip step 2, everything works as expected.
Below is some example code that contains both scenarios:
The text was updated successfully, but these errors were encountered: