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

Skip to content

How to configure named argument with variadics and autowire in YAML? #24935

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
PabloKowalczyk opened this issue Nov 12, 2017 · 5 comments
Closed

Comments

@PabloKowalczyk
Copy link
Contributor

Q A
Bug report? yes/no
Feature request? yes/no
BC Break report? no
RFC? no
Symfony version 3.3

Hello,

I'm wondering how to configure named argument with variadics parameter and autowire.

Please look at these example classes:

class Bar
{
}
class Foo
{
    public function __construct(PropertyAccessorInterface $propertyAccessor, Bar ...$bars)
    {
        // ...
    }
}

As we can see, class Foo has $bars constructor param as a variadic.
Without autowire i can configure it as presented below:

bar1:
  class: Bar

bar2:
  class: Bar

Foo:
  arguments:
    - '@property_accessor'
    - '@bar1'
    - '@bar2'

Now, i would like to use autowire feature, but i don't know how to configure it.

Below config doesn't work - exception Type error: Argument 2 passed to Foo::__construct() must be an instance of Bar, array given:

Foo:
  autowire: true
  arguments:
    $bars:
      - '@bar1'
      - '@bar2'

Below config works but only last service is injected:

Foo:
  autowire: true
  arguments:
    $bars: '@bar1'
    $bars: '@bar2'

Is there any way to configure it properly?

Thanks.

@nicolas-grekas
Copy link
Member

I don't think that's supported, just because nobody made it to work. You could be the one :)

@PabloKowalczyk
Copy link
Contributor Author

Ok, i can try to do it. Shall i introduce new tag !variadic or rely on Reflection?

@nicolas-grekas
Copy link
Member

Reflection, as it's already used, so should be just a new case to handle in ResolveNamedArgumentsPass I guess?

@PabloKowalczyk
Copy link
Contributor Author

I think that make sense.

@fabpot fabpot closed this as completed Dec 1, 2017
fabpot added a commit that referenced this issue Dec 1, 2017
…amed arguments (PabloKowalczyk)

This PR was squashed before being merged into the 4.1-dev branch (closes #24937).

Discussion
----------

[DependencyInjection] Added support for variadics in named arguments

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24935
| License       | MIT

Commits
-------

b5c0e89 [DependencyInjection] Added support for variadics in named arguments
@Wirone
Copy link
Contributor

Wirone commented Oct 14, 2020

Would be great if this could work with !tagged so variadic arguments could be injected automatically, without listing them explicitly in arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants