zendframework / zend-di

Di component from Zend Framework
BSD 3-Clause "New" or "Revised" License
46 stars 29 forks source link

Documentation AoT - factories + injector decorator #53

Open michalbundyra opened 5 years ago

michalbundyra commented 5 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.

tux-rampage commented 5 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

michalbundyra commented 5 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...

weierophinney commented 4 years ago

This repository has been closed and moved to laminas/laminas-di; a new issue has been opened at https://github.com/laminas/laminas-di/issues/2.