JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

Add support for Expression Language

Open slavafomin opened this issue 11 years ago • 14 comments

Consider adding support for Expression Language.

For example I'm using this configuration:

jms_di_extra:
    automatic_controller_injections:
        properties:
            environment: "@=service('kernel').getEnvironment()"

It tells me that it can not find service with name =service('kernel').getEnvironment().

Thanks )

slavafomin avatar Feb 22 '14 00:02 slavafomin

Seems that the bundle is just calling container->get. As container itself does not support this, the bundle fails.

nick4fake avatar Feb 24 '14 13:02 nick4fake

It's working with the direct service container configuration through yaml.

slavafomin avatar Feb 24 '14 17:02 slavafomin

@slavafomin your example is a bad use-case anyway, as the kernel environment is available as a parameter named kernel.environment

stof avatar Feb 24 '14 18:02 stof

I'd also like to see some use-cases for this. I cannot remember that I would have needed this feature in the past.

schmittjoh avatar Feb 24 '14 18:02 schmittjoh

@schmittjoh , @stof, Ok, better use-case: @DI\Inject("service('security.context').gettoken().getuser()")

I already have preauthorize annotation to check is user logined (thus token is already populated), but I need to inject whole security.context in method instead of just current user. Also if expressions are supported one might use User object in ParamConverters (for example, fetch something for current user).

etc.. Lots of possibilities

nick4fake avatar Feb 24 '14 18:02 nick4fake

@stof It was just a rough example to demonstrate the idea )

Another use case: I want to inject URI to some resource. Right now I have to inject Router itself and that makes my service tightly coupled with Symfony.

slavafomin avatar Feb 24 '14 19:02 slavafomin

Repositories are another use case, I'd love to be able to inject the repository directly without injecting the entity manager or using a factory (or registering them all as services).

LewisW avatar Apr 15 '14 17:04 LewisW

@LewisW What is the problem with repositories as services? I find such way very comfortable and clear.

nick4fake avatar Apr 15 '14 17:04 nick4fake

@nick4fake Nothing at all, it was just another use case. I haven't tried repositories as services yet to be honest - the thought of adding all of our repositories in the services.yml sounded like a bit of a nightmare so we avoided it (we've only just started using this bundle). Although my fear with using repositories as services is that Doctrine's getRepository wouldn't load it from the service container, and not load the dependencies properly.

Anyway, I'll tackle this problem tonight and send a PR.

LewisW avatar Apr 15 '14 18:04 LewisW

Did anything ever come of this? It'd be great if this bundle supported expressions.

jamesmoss avatar Dec 12 '14 11:12 jamesmoss

Any status on this?

QuingKhaos avatar Sep 09 '15 11:09 QuingKhaos

+1

chrisyue avatar May 08 '16 07:05 chrisyue

I thought I would need this in the past, but it was only, because we didn't properly created everything as clean services. IMHO if you begin to repeat a service expression two times and more (doesn't matter if in a annotation if possible or in the service description) it's time to refactor it to a new service.

QuingKhaos avatar May 08 '16 07:05 QuingKhaos

@patkar but it's always better to has another option right? I agree that it shoud be refactored in your situation, but before that, we (maybe you as well) still want this feature.

chrisyue avatar May 09 '16 07:05 chrisyue