Closed yethee closed 5 years ago
@yethee thx for the report. I will check it tomorrow and give you my feedback.
@yethee well you are right. The problem is related to the dependencies ArrayDefinition and factory callable function:
Foo::class => function (ContainerInterface $c) {
return new Foo($c->get('config')['key']);
}
When we have a callable through an ArrayDefintion, the callable is resolved automatically which in this case, leads to a circular dependency because the callable requires the config key from the container.
This issue could be resolved by using a factory class as follow:
class FooFactory
{
public function __invoke(ContainerInterface $c)
{
return new Foo($c->get('config')['key']);
}
}
$config = new Config([
'key' => 'null',
'dependencies' => [
'factories' => [
Foo::class => function (ContainerInterface $c) {
return new Foo($c->get('config')['key']);
},
],
],
]);
However, I prefer also removes the dependencies key from the definitions as they are already resolved and not needed any more:
private function addDefinitions(ContainerBuilder $builder): void
{
$this->addServices();
$this->addFactories();
$this->addInvokables();
$this->addAutowires();
$this->addAliases();
$this->addDelegators();
/**
* PHPDI ArrayDefinition would resolve all keys
* or dependencies are already resolved
*/
unset($this->definitions[$this::CONFIG]['dependencies']);
$builder->addDefinitions($this->definitions);
}
@elie29 thanks for fix and a new release!
Occurs error while resolving lazy injection:
Seems, in this case PHP-DI tries resolve each entry of config, because it is
ArrayDefinition
.Simple code to reproduce:
If delete
config.dependencies
from the definitions this solves the issue. But, I do not know whether this can lead to issues in other cases?