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

Skip to content

[DependencyInjection] Allow array for the value of Autowire attribute #47801

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

Merged
merged 1 commit into from
Oct 18, 2022
Merged

[DependencyInjection] Allow array for the value of Autowire attribute #47801

merged 1 commit into from
Oct 18, 2022

Conversation

willemverspyck
Copy link
Contributor

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
License MIT
Doc PR symfony/symfony-docs#...

Since Symfony 6.1 it's possible to use #[Autowire] for the service autowiring logic. Array as autowire parameters is not supported yet. In YAML or the attribute Autoconfigure it worked.

#[Autoconfigure(bind: [
    '$parameters' => [
        'PARAMETER_DAY_START' => '%env(string:PARAMETER_DAY_START)%',
        'PARAMETER_DAY_END' => '%env(string:PARAMETER_DAY_END)%',
        'FILTER_LIMIT' => '%env(int:FILTER_LIMIT)%',
        'FILTER_OFFSET' => '%env(int:FILTER_OFFSET)%',
    ],
])]
class WidgetService
{
    public function __construct(private readonly array $parameters)
    {
    }
}

After this PR there is support for an array (including some extra tests):

class WidgetService
{
    public function __construct(#[Autowire([
        'PARAMETER_DAY_START' => '%env(string:PARAMETER_DAY_START)%',
        'PARAMETER_DAY_END' => '%env(string:PARAMETER_DAY_END)%',
        'FILTER_LIMIT' => '%env(int:FILTER_LIMIT)%',
        'FILTER_OFFSET' => '%env(int:FILTER_OFFSET)%',
    ])] private readonly array $parameters)
    {
    }
}

@carsonbot carsonbot added this to the 6.2 milestone Oct 6, 2022
@nicolas-grekas nicolas-grekas changed the title [DependencyInjecten] Allow array for the value of Autowire attribute [DependencyInjection] Allow array for the value of Autowire attribute Oct 17, 2022
@carsonbot carsonbot changed the title [DependencyInjection] Allow array for the value of Autowire attribute Allow array for the value of Autowire attribute Oct 17, 2022
@carsonbot carsonbot changed the title Allow array for the value of Autowire attribute [DependencyInjection] Allow array for the value of Autowire attribute Oct 17, 2022
@fabpot
Copy link
Member

fabpot commented Oct 18, 2022

Thank you @willemverspyck.

string $service = null,
string $expression = null,
) {
if (!($service xor $expression xor null !== $value)) {
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
}

if (null !== $value && str_starts_with($value, '@')) {
if (\is_string($value) && str_starts_with($value, '@')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an array, wouldn't this need to process the strings inside that array to convert their prefixes too ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to allow nesting services into arrays.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do allow that in Yaml or XML (which is how you passed an array of objects indexed by some keys for instance)

@willemverspyck
Copy link
Contributor Author

Thanks for reviewing @fabpot and @nicolas-grekas 👍

@fabpot fabpot mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants