Pass the container to factory closures (callables) by default
odan opened this issue · comments
For performance reason I use a lot of factory closures/anonymous functions to build objects.
Very often the container object is needed. Here is a generic example:
$container = new League\Container\Container;
$container = new Container();
$container->add(Foo:class, static function(ContainerInterface $container) {
$bar = $container->get(Bar::class)
return new Foo($bar);
})->addArgument($container);
You see, I always have to call addArgument
to pass the container instance. My question / feature request is: Would it be possible to pass the container instance by default if no extra argument is defined via addArgument
?
This is also the default parameter-list for PHP-DI factories and it would be great to have this as default too.
Ideally a minimal closures/anonymous function could be defined in this way:
use Psr\Container\ContainerInterface;
$container->share(ContainerInterface::class, static function (ContainerInterface $container) {
return $container;
})->addArgument($container);
// All other callables can be added without calling addArgument
$container->add(Foo:class, static function(ContainerInterface $container) {
$bar = $container->get(Bar::class)
return new Foo($bar);
});
Is there no interest? If not, I will close this PR.
Hi @philipobenito Where can I find more information about this topic? Do you have a small example?