apisearch-io / apisearch-bundle

:boat: Apisearch Symfony-Drift Bundle
http://apisearch.io
MIT License
23 stars 4 forks source link

Make the bundle autowiring friendly #51

Closed mmoreram closed 4 years ago

mmoreram commented 4 years ago

Apisearch bundle creates a set of repositories given a configuration. All repositories have implemented the same methods (a single interface), what makes impossible to make differences between them.

Given that the implementation layer should never depend on the configuration layer, what means that would be a VERY bad idea to start adding interfaces because of configuration needs, how can we address this?

nicolas-grekas commented 4 years ago

The solution is to use named autowiring aliases, see https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type

See usage e.g. in https://github.com/thephpleague/flysystem-bundle/blob/master/src/DependencyInjection/FlysystemExtension.php#L56

mmoreram commented 4 years ago

@nicolas-grekas what happens if we have multiple instances of the same interface? Some places will need one of them, some places the other. How auto-wiring can really know which one should inject?

apisearch:
    repositories:
        repo1: xxx => apisearch.repo1
        repo2: yyy => apisearch.repo2
nicolas-grekas commented 4 years ago

I think the linked doc answers this very question. There is a best practice here, which is to name the service repository using the exact name given in the configuration - ie no extra prefix/suffix convention:

apisearch:
    repositories:
        repo1.apisearch: xxx => repo1.apisearch
        foo: xxx => foo

Then, named autowiring aliases will allow one to express which repo they need by naming the argument accordingly (using camel case).

You can define a default autowiring alias (regular ones), in which case this will be the one when the name of a type-hinted argument doesn't match. Or you can skip defining a default autowiring alias, in which case ppl will either have to use explicit wiring - or name their arguments according to one of the named aliases. This helps with typos.

mmoreram commented 4 years ago

So, there must be a correlation between the name of the service and the name of the method parameter?

LOL.

Okay. Thanks.

nicolas-grekas commented 4 years ago

I know why contributing to OSS is rude. Where should I send the invoice to, for consulting job here?

mmoreram commented 4 years ago

Rudeness? Where?