-
Notifications
You must be signed in to change notification settings - Fork 28
PSR-11 implementation and auto-wiring #803
Comments
Thanks for your in-depth explanation. I ran into this issue when I wanted to abstract if($container instanceof \Illuminate\Container\Container) {
return $container->make($abstract);
}
return $container->get($abstract); Which kind of defeat the purpose... I think it would be awesome if the |
I understand that there can be people who will complain that the service is not registered in the container. And it should be delegated to another container. So here is what I propose. The container can turn on the feature for autowiring.
Now on the container side public function autoResolvePsr11()
{
$this->autoResolvePsr11 = true;
}
public function get($id)
{
if ($this->has($id)) {
return $this->resolve($id);
}
throw new EntryNotFoundException();
}
public function has($id)
{
if ($this->bound($id)) {
return true;
}
if ($this->autoResolvePsr11) {
if (in_array($id, $this->cachedKeys)) {
return true;
}
if ($this->isInstantiable($id)) {
$this->cachedKeys[] = $id;
return true;
}
}
return false;
}
private function isInstantiable($id)
{
if (class_exists($id)) {
return true;
}
try {
$reflectionClass = new \ReflectionClass($id);
return $reflectionClass->isInstantiable();
} catch (\ReflectionException $e) {
return false;
}
} Some parts of the code is taken from an implementation over : https://github.com/dms-org/web.laravel/blob/582d970393656a011ffcee4f6716ae79c5e57813/src/Ioc/LaravelIocContainer.php#L157-L199 Thank you. UPDATE : Code is updated a bit removing certain functions. |
@moufmouf : what other autowiring capable PSR-11 containers do in that case ? |
Good question, I'm not a big autowiring fan so I haven't tracked that closely. Let's ask @mnapoli. His PHP-DI package is PSR-11 compliant and does auto-wiring (+ he is PSR-11 co-editor so he knows what he does :) ) |
Hi, in PHP-DI
See the code here: https://github.com/PHP-DI/PHP-DI/blob/master/src/Definition/ObjectDefinition.php#L222 That means that |
Sorry for my interpretation of the first implementation. I didn't like it either, but I thought it was what the PSR-11 implied. |
Well, I guess we gonna have to live with it as PR was rejected.. :( |
Not necessarily. I think it's worth persisting with this and hopefully we can all agree on an implementation. |
I just proposed a different path for auto-wire to try and make amends for my first interpretation. laravel/framework#21335 |
I proposed another solution on laravel/framework#25870 if anybody want to chime in. |
Hey guys!
PSR-11 editor here :)
It was just brought to my attention by @harikt here (laravel/framework#21220) that the PSR-11 implementation of Laravel container in 5.5 requires that the services are explicitly bound in the container.
It is clearly documented here: https://laravel.com/docs/5.5/container#psr-11
It's a bit of a shame because it means that we cannot use the PSR-11 side of Laravel container for auto-wiring. And Laravel container is sooo good at auto-wiring.... :)
Looking back at why this decision was taken, I ran into this PR: laravel/framework#19822
@thecrypticace says:
So to be clear: PSR-11 gives the container a great deal of freedom regarding what you consider to be part of the container or not. The current implementation is totally legit, but
has
returningtrue
for autowired object is also legit if you decide that any instantiable class is part of the container.We have had a lengthy discussion on this topic here: container-interop/container-interop#37 (comment)
(note: container-interop/container-interop was the draft of PSR-11)
IMHO, it would make a great deal of sense to change the behaviour of the Laravel container so that the
get
method can also return auto-wired objects. The current implementation is not wrong, but it is definitely limiting the power of what can be done with the container and there is no good reason to do that.The text was updated successfully, but these errors were encountered: