Skip to content

[DependencyInjection] Mandatory "class" option for factory services returning something else than an object #7352

Closed
@gnutix

Description

@gnutix

Hello everyone,

I'm using service factories for some services in my applications. Here's two examples of them :

services:
    acme.repo.country:
        class: Acme\AcmeBundle\Entity\CountryRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments: ['Acme\AcmeBundle\Entity\Country']

    acme.locale:
        class: Symfony\Component\HttpFoundation\Request
        factory_service: request
        factory_method: getLocale

The first one, acme.repo.country have a class option that describes the type of object returned by the service, which is the Doctrine Repository for my Country Entity. Make sense.

But the second one, acme.locale, that will return a string**, disturbs me a lot, because it must have the class option in order to work, and there's no way to set a "good" value to it. So I generally chose the type of the "factory_service" class, even though it should be the type returned by the "factory_method". If I don't put a class option, I'm getting the following error :

Please add the class to service "acme.locale" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.

I've talked with @lsmith77 about it and we weren't able to figure out in what case the DIC may have to add method calls when dealing with factory services, and anyway we both agreed it wouldn't work well on a string. ;)

The question is then : what is the reason of this mandatory class parameter, and is there a way to do it otherwise so that services are more accurately described when returning something else than an object, and the usage of it doesn't feel like a dirty hack (because of the error message above) ?

Thanks for your inputs.

** PS: don't suggest me to inject the request instead of using this factory, it's not the point and it's just an example among a lot of others. Thanks !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions