Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DependencyInjection] add#[AutowireLocator] attribute#51392

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

Merged
nicolas-grekas merged 1 commit intosymfony:6.4fromkbond:locator-attribute
Aug 21, 2023

Conversation

@kbond
Copy link
Member

@kbondkbond commentedAug 15, 2023
edited
Loading

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
Ticketsn/a
LicenseMIT
Doc PRtodo

TheAutowireLocator attribute allows configuring service locators inline:

useSymfony\Component\DependencyInjection\Attribute\AutowireLocator;publicfunctionsomeAction(    #[AutowireLocator(RouterInterface::class, SluggerInterface::class)]ContainerInterface$container,):Response {$container->get(RouterInterface::class);$container->get(SluggerInterface::class);}

You can customize the key and have optional services:

useSymfony\Component\DependencyInjection\Attribute\AutowireLocator;publicfunctionsomeAction(    #[AutowireLocator(        router: RouterInterface::class,        slugger:'?'.SluggerInterface::class,    )]ContainerInterface$container,):Response {$container->get('router');if ($container->has('slugger')) {$container->get('slugger');    }}

dvdknaap and lyrixx reacted with heart emojiderrabus, Jibbarth, and dvdknaap reacted with rocket emojiderrabus reacted with eyes emoji
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

Love it, I didn't expect the implementation to be so simple :)

kbond and derrabus reacted with thumbs up emoji
@derrabus
Copy link
Member

In order to address the failing Psalm job, I've opened#51395.

@kbondkbond changed the title[DependencyInjection] add#[Locator] attribute[DependencyInjection] add#[AutowireLocator] attributeAug 16, 2023
@kbondkbondforce-pushed thelocator-attribute branch 2 times, most recently from7d2e338 to0e0583fCompareAugust 16, 2023 12:50
@nicolas-grekas
Copy link
Member

nicolas-grekas commentedAug 17, 2023
edited
Loading

I'm wondering how we could make these work:

#[AutowireLocator(['cache' =>newSubscribedService(type: CacheInterface::class: attributes: [newTarget('my.cache')]),'blobs' =>newTaggedLocator('blob'),)]ContainerInterface$container,

Any idea?

@kbond
Copy link
MemberAuthor

kbond commentedAug 17, 2023
edited by nicolas-grekas
Loading

I thought that too! I was going to take a look at this possibility after this was merged.

BTW, your example can't be an array, it needs to be:

#[AutowireLocator(    cache:newSubscribedService(type: CacheInterface::class: attributes: [newTarget('my.cache')]),    blobs:newTaggedLocator('blob'),)]ContainerInterface$container,

(just making sure you're ok with this syntax)

nicolas-grekas reacted with thumbs up emoji

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedAug 21, 2023
edited
Loading

I just noticed the failures on high+low deps, can you please have a look?

@kbond
Copy link
MemberAuthor

I believe the test suite will be fixed once merged because of the http-kernel's requirement on thisAutowireLocator. Or should I add a conditional in the test?

@nicolas-grekas
Copy link
Member

Thank you@kbond.

@nicolas-grekasnicolas-grekas merged commitc64214c intosymfony:6.4Aug 21, 2023
@kbondkbond deleted the locator-attribute branchAugust 21, 2023 12:40
nicolas-grekas added a commit that referenced this pull requestOct 6, 2023
…ute and improve `#[AutowireLocator]` (nicolas-grekas, kbond)This PR was merged into the 6.4 branch.Discussion----------[DependencyInjection] Add `#[AutowireIterator]` attribute and improve `#[AutowireLocator]`| Q             | A| ------------- | ---| Branch?       | 6.4| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | -| License       | MITThis PR is building on#51392 to add an `#[AutowireIterator]` attribute and improve `#[AutowireLocator]`.The new `#[AutowireIterator]` attribute can be used to describe what `#[AutowireLocator]` can do, except that we get an iterator instead of a container.And  `#[AutowireLocator]` can now be used instead of `#[TaggedLocator]`: `#[AutowireLocator('foo')]` and done.In order to describe that you want a list of services, we cannot use named arguments anymore so we have to pass an array now: `#[AutowireLocator(['foo' => 'F', 'bar' => 'B'])]` should be used instead of `#[AutowireLocator(foo: 'F', bar: 'B')]`.Last but not least, this adds support for nesting `SubscribedService` objects in the list of described services. This provides feature-parity with what we can do when implementing `ServiceSubscriberInterface`.I didn't deprecate `TaggedIterator` nor `TaggedLocator`. We could, but maybe we should wait for 7.1?TODO:- [x] add tests - thanks `@kbond`!~PS: while writing this, I realize that we may merge both tags in one, and let `AutowirePass` decide if it should build a locator or an iterator based on the type of the argument that has the attribute. We'd "just" need to find a name that'd work for that.~Commits-------a87f2e0 [DependencyInjection] Add tests for `AutowireLocator`/`AutowireIterator`78018de [DependencyInjection] Add `#[AutowireIterator]` attribute and improve `#[AutowireLocator]`
@fabpotfabpot mentioned this pull requestOct 21, 2023
@fabpotfabpot mentioned this pull requestOct 21, 2023
fabpot added a commit that referenced this pull requestJun 20, 2025
…#[TaggedLocator]` attributes (GromNaN)This PR was merged into the 8.0 branch.Discussion----------[DependencyInjection] Remove `#[TaggedIterator]` and `#[TaggedLocator]` attributes| Q             | A| ------------- | ---| Branch?       | 8.0| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        | -| License       | MITThe new attributes `#[AutowireLocator]` and `#[AutowireIterator]` were introduced in Symfony 6.4 by#51392 and#51832.These replace the previous attributes `#[TaggedIterator]` and `#[TaggedLocator]` which were introduced in Symfony 5.4 by#40406 and subsequently deprecated in 7.1 by#54371Commits-------c096714 Remove TaggedIterator and TaggedLocator attributes
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

6.4

Development

Successfully merging this pull request may close these issues.

4 participants

@kbond@derrabus@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp