-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
NVM..., this is not actually required for the mentioned issue... Just regular binding does. (but anyway I'm 👍 for this) |
@@ -74,7 +74,7 @@ protected function processValue($value, $isRoot = false) | |||
$this->unusedBindings[$bindingId] = array($key, $this->currentId); | |||
} | |||
|
|||
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.
And iterable
?
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 guess yes 😃)
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)
I think |
@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.: ```yaml bind: 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#10206 In #27165, we introduced the possibility to bind by type+name: ```yaml bind: 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: ```yaml services: 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.