Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DI] Allow binding by type+name#27165
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
ogizanagi commentedMay 5, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
NVM..., this is not actually required for the mentioned issue... Just regular binding does. (but anyway I'm 👍 for this) |
| } | ||
| if (isset($key[0]) &&'$' ===$key[0]) { | ||
| if (preg_match('/^(?:(?:array|bool|float|int|string) )?\$/',$key)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Anditerable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This needs to contain what we can put in parameters. So not iterable nor callable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Theoretically, a factory definition could return a callable or iterable (or any other type) and be used in a binding though. Do we care? (is it actually officially supported? => Edit: I guessyes 😃)
Also, a class might have an iterable typehint but expected injected value be an array. Shouldn't bindings support this too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It's supported since it always worked yes, and it works here also, this just skips the "must be a ref" check for scalar|array types.
a class might have an iterable typehint but expected injected value be an array
That's not supported right now. Could be added in another PR for sure (if worth it)
ogizanagi commentedMay 6, 2018
I think |
nicolas-grekas commentedMay 6, 2018
@ogizanagi good catch, fixed thanks. |
This PR was merged into the 4.2-dev branch.Discussion----------[DI] Allow binding by type+name| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | -| License | MIT| Doc PR | -This would allow to bind by type + argument name, e.g.:```yamlbind: Psr\Log\LoggerInterface $logger:@logger```Allows more precise targets for bindings as it will match only if both the type and the name match.Works with scalar/array types also for consistency.Commits-------32fc58d [DI] Allow binding by type+name
…s-grekas)This PR was merged into the 4.2-dev branch.Discussion----------[DI] Allow autowiring by type + parameter name| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | -| License | MIT| Doc PR |symfony/symfony-docs#10206In#27165, we introduced the possibility to bind by type+name:```yamlbind: Psr\Log\LoggerInterface $myLogger: @monolog.logger.my_logger```But we forgot about aliases. For consistency, they could and should allow doing the same. More importantly, this will open up interesting use cases where bundles could provide default values for typed+named arguments (using the new `ContainerBuilder::registerAliasForArgument()` method). E.g:```yamlservices: Psr\Cache\CacheItemPoolInterface $appCacheForecast: @app.cache.forecast```Works also for controller actions and service subscribers (using the real service id as the key).Commits-------c0b8f53 [DI] Allow autowiring by type + parameter name
This would allow to bind by type + argument name, e.g.:
Allows more precise targets for bindings as it will match only if both the type and the name match.
Works with scalar/array types also for consistency.