JMSDiExtraBundle
JMSDiExtraBundle copied to clipboard
Add support for Expression Language
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 )
Seems that the bundle is just calling container->get. As container itself does not support this, the bundle fails.
It's working with the direct service container configuration through yaml.
@slavafomin your example is a bad use-case anyway, as the kernel environment is available as a parameter named kernel.environment
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 , @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
@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.
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 What is the problem with repositories as services? I find such way very comfortable and clear.
@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.
Did anything ever come of this? It'd be great if this bundle supported expressions.
Any status on this?
+1
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.
@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.