Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] Add#[Autoconfigure] to help define autoconfiguration rules#39804
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
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
8c5ba49 to1e1b86eCompareUh oh!
There was an error while loading.Please reload this page.
1e1b86e tobd4ece5Comparero0NL commentedJan 12, 2021
is there any reason for the "auto discovered services only" actually? |
Uh oh!
There was an error while loading.Please reload this page.
apfelbox commentedJan 13, 2021
Quick nitpick question: should the attribute be named The recently proposed |
nicolas-grekas commentedJan 13, 2021
I feel like I missed explaining correctly how this works.Yes, this attribute declares a configuration rule for autoconfiguration. |
#[Autoconfigure] to help define autoconfiguration rulesbd4ece5 toac88c9eCompare3a1e8b6 to5b98e9dCompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
5b98e9d tofdee7f2Comparefdee7f2 to37a5d26Comparesrc/Symfony/Component/DependencyInjection/Compiler/RegisterAutoconfigureAttributesPass.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
wouterj commentedFeb 4, 2021
IIUC, we now have 2 competing PRs here. As far as I know, it was decided that progress continues in#39897? Please correct me if I'm wrong, but otherwise this one should be closed I think. |
nicolas-grekas commentedFeb 4, 2021
@wouterj this one provides something else. Both should be merged to me in the end. This one before#39897 since it's ready and might be used by#39897 as explained in#39897 (comment) |
afe384e toe66a3baComparenicolas-grekas commentedFeb 7, 2021
/cc @symfony/mergers anyone? |
src/Symfony/Component/DependencyInjection/Compiler/RegisterAutoconfigureAttributesPass.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
wouterj commentedFeb 11, 2021 • 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.
As requested, I'll post some of my comment in the other PR (ref:#39897 (comment) ) here: 😃 What I like about this PR is that it reuses the autoconfigure system (using the Yaml file loader seems a bit hacky, but I'm surely not the one to make a decision about that). |
nicolas-grekas commentedFeb 11, 2021 • 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.
What matters here is how one can describe something using an array data structure. YamlLoader does exactly this: turn arrays (from yaml) into actual calls. Here, we just reuse that logic, so that the same array-structures can also be used in attributes. E.g.:
All ppl that use
There is a critical difference with#39897: the attribute in this PR allows defining rules that areinherited.#39897 defines attributes that arenot inherited. That'sTHE critical difference between both PRs, and why they fill a different need.
Can you clarify? To me, it looks like you're arguing against empowering ppl, which I'm sure you aren't.
I do not understand this "coupling" argument. To me, as I explained in#39776 (comment), there is zero difference between this PR,#39776 or#39897 in this regard. Maybe you can help me understand what you mean with actual examples of when this "coupling" would happen?
See "the critical difference" I mention above. |
e66a3ba toc8ace88Comparenicolas-grekas commentedFeb 11, 2021 • 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.
TL;DR of my previous comment:
Note that these two lines are orthogonal. |
derrabus left a comment
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.
I understand the intention of this generic attribute as a nicer way to configure autoconfiguration rules for userland base types.
Instead of cluttering my kernel with…
$container->registerForAutoconfiguration(MyInterface::class) ->addTag('my_tag');
… I annotate this rule to my interface instead.
#[Autoconfigure(tags: ['my_tag'])]interface MyInterface {}
Did I get this right?
I think, this is the use case we can agree on. But since attributes are a new feature to most users, it is imperative that we distinguish the use-cases of this PR and#39897 in the documentation.
c8ace88 to64ab6a2Comparenicolas-grekas commentedFeb 16, 2021
@derrabus you've got it right. I agree about the doc. |
…r defining the index and priority of classes found in tagged iterators/locators (nicolas-grekas)This PR was merged into the 5.3-dev branch.Discussion----------[DependencyInjection] Add `#[TaggedItem]` attribute for defining the index and priority of classes found in tagged iterators/locators| Q | A| ------------- | ---| Branch? | 5.x| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets | -| License | MIT| Doc PR | -Next to#39804, this PR adds a new `#[TaggedItem]` attribute that ppl can use to define the index of their service classes when they're used in tagged collections (iterators/locators.This replaces the `public static getDefaultName()` and `getDefaultPriority()` methods that ppl could use for this purpose:```php#[TaggedItem(index: 'api.logger', priority: 123)]class MyApiLogger implements LoggerInterface{}```This will ship the corresponding service at index `api.logger`, priority=123 when building locators/iterators.Commits-------252f2ca [DependencyInjection] Add `#[TaggedItem]` attribute for defining the index and priority of classes found in tagged iterators/locators
kozlice commentedMay 13, 2021
This is a really cool feature, but I've just tried it and there seems to be an issue: #[Autoconfigure(tags: ['some.tag'])]interface MyServiceInterface {}class MyServiceimplements MyServiceInterface {}$class =newReflectionClass(MyService::class);// That's how RegisterAutoconfigureAttributesPass does it$attributes =$class->getAttributes(Autoconfigure::class, \ReflectionAttribute::IS_INSTANCEOF);// Whoopsie, empty arrayvar_dump($attributes); Running PHP 8.0.6. Looks like the |
nicolas-grekas commentedMay 13, 2021
You might have missed how this works. I can't really help because I don't know what you want todo, but of course, requesting the attributes on class |
kozlice commentedMay 13, 2021
@nicolas-grekas Yeah, I might've misunderstood something. The announcement in blog says that So, I've tried this: #[Autoconfigure(tags: ['some.tag'])]interface ServiceInterface {}class ServiceAimplements ServiceInterface {}class ServiceBimplements ServiceInterface {} services:_defaults:autowire:trueautoconfigure:trueApp\ServiceA:~App\ServiceB:~ And I was expecting both services to be tagged, but they are not. |
nicolas-grekas commentedMay 13, 2021
Please open an issue if you think you found a bug, with a reproducer ideally. |
Uh oh!
There was an error while loading.Please reload this page.
Being inspired by the discussion with@derrabus in#39776.
This PR allows declaring autoconfiguration rules using an attribute on classes/interfaces, eg:
#[Autoconfigure(bind: ['$foo' => 'bar'], tags: [...], calls: [...])]This should typically be added on a base class/interface to tellhow implementations of such a base type should be autoconfigured. The attribute is parsed when autoconfiguration is enabled, except when a definition has the
container.ignore_attributestag, which allows opting out from this behavior.As usual, the corresponding rules are applied only to services that have autoconfiguration enabled.
In practice, this means that this enables auto-tagging of all implementations of this interface:
Of course, all auto-configurable settings are handled (calls, bindings, etc.)
This PR adds another attribute:
#[AutoconfigureTag()].It extends
#[Autoconfigure]and allows for specifically defining tags to attach by autoconfiguration.The name of the tag is optional and defaults to the name of the tagged type (typically the FQCN of an interface). This should ease with writing locators/iterators of tagged services.
#[AutoconfigureTag()]interface MyInterface {...}