[DependencyInjection] Introduce ClosureArgument
next to ServiceClosureArgument
#49489
Labels
ClosureArgument
next to ServiceClosureArgument
#49489
Description
The ServiceClosureArgument can be used to create closures that return a service.
The common use case is to pass a
Reference
like this:However, it also accepts a
Definition
:As a matter of fact, it does not care about the value at all, that means it accepts other scenario's like:
And even arrays:
I think this super useful. It allows developers to make lazy callable's for anything. This is especially handy when working with libraries that want closures. For example, the popular graphql-php library allows closures for almost everything.
But the fact that the above works doesn't mean it's the intention of this
ServiceClosureArgument
. This was pointed out by @stof here #49471 (comment).I would suggest to do one of the following:
A) Allow the above usages as valid for
ServiceClosureArgument
, and introduce tests to guard it's usage over time.B) Restrict what is possible in
ServiceClosureArgument
. Given the name, I think this should acceptReference
and maybeDefinition
. Introduce a newClosureArgument
that allows a wider (or unlimited) range of values with tests.Going further
With
ClosureArgument
support, we could even further improve this. For example, thegraphql-php
library wants callable's for resolvers. In a compiler pass I want to create closures with dynamic arguments. That are then dumped to the container like this:Now the closure can be passed to anything that is then able to call it. It doesn't have to bother about fetching the services, as that is now a compiler/container responsibility.
This could also be used for controller actions, becoming callable, with services injected / prepared. It would remove the need for ServiceValueResolver and a separate
ServiceLocator
that keeps track of those services.It could look like this:
The text was updated successfully, but these errors were encountered: