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
{{ message }}
Repository navigation
Autowiring and Annotations do not work for DI\object() inside arrays #343
<?phprequire'vendor/autoload.php';
class A {
publicfunction__construct(B$b) {}
}
class B {
/** * @Inject */publicfunctiontest() {
echo"Autowiring and Annotations work.";
}
}
$builder=newDI\ContainerBuilder();
$builder->useAnnotations(true);
$builder->addDefinitions([
'test'=>[DI\object(A::class)]
]);
$container=$builder->build();
try {
$container->get('test');
}
catch (Exception$e)
{
echo$e->getMessage();
}
Executing the code above gives the following output:
Error while resolving test[0]. Entry cannot be resolved: The parameter 'b' of A::__construct has no value defined or guessable
Full definition:
Object (
class = A
scope = singleton
lazy = false
)
changing the definitions to $builder->addDefinitions(['test'=>DI\object(A::class)]); works as expected.
That's weird indeed, thanks for the great bug report. The usage seems valid to me and I think I can see why it's not working… After a very quick look I'm not sure what the best fix would be, I'll try to have a look at it soon.
I know what causes the bug (ArrayDefinition doesn't trigger autowiring on sub-definitions) but from what I remember it's not an easy one to fix and I don't have a lot of time available to work on it at the moment (publishing the last Slim bridge should have been 2 weeks ago for example).
For anyone wanting to have a look at it: ArrayDefinition should me made to implement HasSubDefinition based on its items. If an item implements HasSubDefinition then it should trigger a sub-definition resolution (i.e. the autowiring). Problem: an array definition could have 0, 1 or many sub-definitions because it can have many items (and the interface doesn't allow "many sub-definitions" for now). Also it needs to be done with as little performance impact as possible (though the cache should minimize that).
Hi everyone, this issue has been indirectly fixed by the replacement of DI\object() with DI\create() and DI\autowire(). The good news too is that since the container can now be compiled (#494) performances will not be affected.
Activity
juliangut commentedon Nov 5, 2015
Is DI\object inside an array a correct usage? I mean you're already using DI\object to create the resolvable
mopahle commentedon Nov 5, 2015
Having only one item in an array may be not really a usecase. But how would you solve something like this without an array:
and maybe with a second defintion:
mnapoli commentedon Nov 5, 2015
That's weird indeed, thanks for the great bug report. The usage seems valid to me and I think I can see why it's not working… After a very quick look I'm not sure what the best fix would be, I'll try to have a look at it soon.
As a workaround (in the meantime):
tuanmh commentedon Mar 11, 2016
I got the same error here. Any plan to fix it?
mnapoli commentedon Mar 11, 2016
I know what causes the bug (ArrayDefinition doesn't trigger autowiring on sub-definitions) but from what I remember it's not an easy one to fix and I don't have a lot of time available to work on it at the moment (publishing the last Slim bridge should have been 2 weeks ago for example).
For anyone wanting to have a look at it: ArrayDefinition should me made to implement
HasSubDefinitionbased on its items. If an item implementsHasSubDefinitionthen it should trigger a sub-definition resolution (i.e. the autowiring). Problem: an array definition could have 0, 1 or many sub-definitions because it can have many items (and the interface doesn't allow "many sub-definitions" for now). Also it needs to be done with as little performance impact as possible (though the cache should minimize that).tuanmh commentedon Mar 13, 2016
Thanks for your quick response & detail guides on this 👍. I'll see if I can help :)
noebogtrbrslseo commentedon Jul 7, 2016
Just a hack, sorry if it completely against your vision: https://github.com/PHP-DI/PHP-DI/pull/426/files
Any feedback is welcome ;-)
Cover #343 with tests now that it's fixed
Cover #343 with tests now that it's fixed
mnapoli commentedon Jun 4, 2017
Hi everyone, this issue has been indirectly fixed by the replacement of
DI\object()withDI\create()andDI\autowire(). The good news too is that since the container can now be compiled (#494) performances will not be affected.