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

Skip to content

[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

Merged
merged 1 commit into from
May 21, 2018

Conversation

nicolas-grekas
Copy link
Member

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.:

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.

@ogizanagi
Copy link
Contributor

ogizanagi commented May 5, 2018

I guess that's what you had in mind for solving #27054 differently?
Not sure how I feel between changing the typehint vs the constructor argument name for such cases.

Your solution requires less work (no extra class to create & register) but I think is also more error prone (naming the argument against the right name to get the right bus). You might end up using the wrong one without any warning.

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

And iterable?

Copy link
Member Author

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.

Copy link
Contributor

@ogizanagi ogizanagi May 6, 2018

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?

Copy link
Member Author

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
Copy link
Contributor

I think RegisterControllerArgumentLocatorsPass misses an update to support typhinted bindings with scalars.

@nicolas-grekas
Copy link
Member Author

@ogizanagi good catch, fixed thanks.

@nicolas-grekas nicolas-grekas merged commit 32fc58d into symfony:master May 21, 2018
nicolas-grekas added a commit that referenced this pull request May 21, 2018
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
@nicolas-grekas nicolas-grekas deleted the bind-tuple branch May 21, 2018 18:18
nicolas-grekas added a commit that referenced this pull request Aug 24, 2018
…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
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.1 Nov 1, 2018
This was referenced Nov 3, 2018
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.

4 participants