laminas / laminas-di

Automated dependency injection for PSR-11 containers
https://docs.laminas.dev/laminas-di/
BSD 3-Clause "New" or "Revised" License
36 stars 20 forks source link

Documentation AoT - factories + injector decorator #2

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

I think the documentation is wrong: https://docs.zendframework.com/zend-di/cookbook/aot-guide/#5-add-aot-to-the-service-manager

In getDependencies method of ConfigProvider we have:

            'factories' => $this->getGeneratedFactories(),
            'delegators' => [
                InjectorInterface::class => [
                    InjectorDecoratorFactory::class,
                ],
            ],

but I think one of this configuration is useless. If we inject generated factories in factories key then we don't need to use decorator (which uses GeneratedInjector and in fact is an abstract factory - but uses only generated factories list).

But in case we have factories injected already into service manager the abstract factory for the service is not going to be called as the factory is defined. If the factory is not defined (so for example service was added later and factories were not regenerated) then normal injector is going to be used anyway (generated injector is just the proxy to origin injector in case factory is not defined).

Of course it doesn't change too much, but in case we need use injector (abstract factory) we don't need to check again on generated list, as this is not there.


Originally posted by @michalbundyra at https://github.com/zendframework/zend-di/issues/53

weierophinney commented 4 years ago

This part of the documentation will cover use cases where the Injector or AutowireFactory is used in a custom service factory. When this is done the Injector will perform a runtime injection instead of using an AoT factory.

Example:


class MyServiceFactory
{
    public function __invoke(ContainerInterface $container)
    {
        return (new AutowireFactory())->create($container, MyService::class, [
            'customParam' => 'SomeService',
        ]);
    }
}

I don't like the idea to sacrifice this feature or leave it undocumented. Maybe we should rephrase the docs here


Originally posted by @tux-rampage at https://github.com/zendframework/zend-di/issues/53#issuecomment-452854721

weierophinney commented 4 years ago

Ok, now I see - after checking the source of AutowireFactory. Yeah, I think it could be rephrased to make it clearer. In general I think it is quite an edge case...


Originally posted by @michalbundyra at https://github.com/zendframework/zend-di/issues/53#issuecomment-453018683