Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] Use variadics for autowiring tagged services #23608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zmitic opened this issue Jul 21, 2017 · 6 comments
Closed

[DI] Use variadics for autowiring tagged services #23608

zmitic opened this issue Jul 21, 2017 · 6 comments
Labels
DependencyInjection Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed)

Comments

@zmitic
Copy link

zmitic commented Jul 21, 2017

Q A
Bug report? no
Feature request? yes
BC Break report? not sure
RFC? yes
Symfony version 3.3.5

Although a documentation doesn't describe it, tagged services can be injected via

$definition->replaceArgument(1, $arrayOfTransportDefinitions);

In all of cases I worked, a service would ever accept only one type of tagged services. Can this be used to have services autowired like this:

class TransportChain
{
    public function __construct(LoggerInterface $logger, TransportInterface ...$transports)
    {
        // ....
    }

This way user would not have to write compiler pass, giving better experience. The con is lack of injecting multiple tagged services but I don't think users do it anyway.

@chalasr chalasr added DependencyInjection Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Jul 21, 2017
@ro0NL
Copy link
Contributor

ro0NL commented Jul 21, 2017

See #22200

What about an additional !variadic / VariadicArgument. In the same spirit as !iterator / IteratorArgument.

With tagged services it would look something like;

'$arg': !variadic !tagged-services tag

@zmitic
Copy link
Author

zmitic commented Jul 21, 2017

I was thinking about having injected tagged services without any extra code in services.yml (as long as autowire=true) or extra compiler pass.

@nicolas-grekas
Copy link
Member

That implies that we're able to compute a collection of TransportInterface.
But that's impossible, we don't do that anytime (and if autowiring does some sort of reflection, that's a deprecated feature, because it's a too fragile behavior).
Let's say we are able to create a list of such services, how would you compute it? The only algo I can think of is: do reflection on all defined services and create the list of all services that implement that interface.
That is extremely fragile also, and forbids any fine tuned behavior. There are many cases where that criteria (the interface some class implement) is not enough to be turned into a collection.
#22200 is a much more tailored solution, one that requires an explicit opt-in into such a behavior. A requirement to me.

@ro0NL
Copy link
Contributor

ro0NL commented Jul 21, 2017

Agree; to me the bare minimum is having extra config in services.yml, i dont see how it relates to autowiring at first.

The only "auto" thing i can think of is some typehint sniffing, ie technically we could this;

public function __construct(array $a, iterable $b, ...$c) { }
'$a': ['@service', '@other_service']
'$b': ['@service', '@other_service']
'$c': ['@service', '@other_service']

I think that's intuitive, though magic-ish. I believe this conflicts with @nicolas-grekas concern;

one that requires an explicit opt-in into such a behavior. A requirement to me.

But how i see it is; you configure a sequence; you ask for a sequence. Enough for DI to process :)

@GuilhemN
Copy link
Contributor

GuilhemN commented Jul 22, 2017

Just as a side note, if I remember well, chaining tags (in '$arg': !variadic !tagged-services tag) is not allowed.

Edit: it's indeed forbidden. See http://www.yaml.org/spec/1.2/spec.html#id2783797 for the reference.

@nicolas-grekas
Copy link
Member

as explained, I'm 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

No branches or pull requests

5 participants