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 support for autowiring services as closures using attributes#49628

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

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedMar 7, 2023
edited
Loading

QA
Branch?6.3
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

When dealing with laziness on the consumer side, a common pattern is to wrap a heavy service in a closure and call it only when needed.

This PR adds attribute#[AutowireServiceClosure] for this purpose:

// generate a closure that returns service "foo"publicfunction __construct(    #[AutowireServiceClosure('foo')]\Closure$foo,)

It also adds support for turning callables into closures with a new#[AutowireCallable] attribute:

// generate a closure that calls "foo"::someMethod()publicfunction __construct(    #[AutowireCallable(service:'foo', method:'someMethod')]\Closure$foo,)

Of course, this fully leverages autowiring aliases, so that instead of "foo", you can useMyInterface::class.

kaznovac, tucksaun, Neirda24, jdreesen, and rela589n reacted with thumbs up emoji
@Neirda24
Copy link
Contributor

Would this also be supported through yaml auto wiring ?

@GromNaN

This comment was marked as resolved.

@derrabus

This comment was marked as resolved.

@nicolas-grekasnicolas-grekas changed the title[DependencyInjection] Add support for autowiring services as closures when using#[Autowire][DependencyInjection] Add support for autowiring services as closures using attributesMar 8, 2023
@nicolas-grekas
Copy link
MemberAuthor

Thanks for the feedback. PR (and description) updated with two new attributes:#[AutowireCallable] and#[AutowireServiceClosure].

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 8, 2023
edited
Loading

Would this also be supported through yaml auto wiring ?

@Neirda24 this is already supported, see eg#41176 and#45878 which introduced!service_closure and!closure YAML tags respectively.

Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

This is much more expressive and self-documented.

@Neirda24
Copy link
Contributor

Would this also be supported through yaml auto wiring ?

@Neirda24 this is already supported, see eg#41176 and#45878 which introduced!service_closure and!closure YAML tags respectively.

Missed it ! Thanks 👍

@nicolas-grekas
Copy link
MemberAuthor

Merging as this blocks other PRs right now, but feel free to review after merge of course!

@nicolas-grekasnicolas-grekas merged commit2f1ccef intosymfony:6.3Mar 9, 2023
@nicolas-grekasnicolas-grekas deleted the di-autowire-closure branchMarch 9, 2023 16:09
nicolas-grekas added a commit that referenced this pull requestMar 10, 2023
…edLocator]` on controller arguments (HypeMC)This PR was merged into the 6.3 branch.Discussion----------[HttpKernel] Add tests for `#[TaggedIterator]` & `#[TaggedLocator]` on controller arguments| Q             | A| ------------- | ---| Branch?       | 6.3| Bug fix?      | no| New feature?  | no| Deprecations? | no| Tickets       |Fix#49083| License       | MIT| Doc PR        | -~~I think this doesn't qualify as a bug fix, but an improvement. If I'm wrong please let me know.~~#49628 fixed the issue, this PR only adds tests now.Commits-------121e072 [DependencyInjection] Add tests for #[TaggedIterator] & #[TaggedLocator] on controller arguments
fabpot added a commit that referenced this pull requestMar 10, 2023
…avor of `#[AutowireDecorated]` (nicolas-grekas)This PR was merged into the 6.3 branch.Discussion----------[DependencyInjection] Deprecate `#[MapDecorated]` in favor of `#[AutowireDecorated]`| Q             | A| ------------- | ---| Branch?       | 6.3| Bug fix?      | no| New feature?  | no| Deprecations? | yes| Tickets       | -| License       | MIT| Doc PR        | -`#[Map*]` are for controller argument resolvers.`#[Autowire*]` should be used for autowiring (see#49628).We could also rename `#[TaggedLocator]` and `#[TaggedIterator]` but I feel like the need is less obvious and the cost more important.Commits-------b653adf [DependencyInjection] Deprecate `#[MapDecorated]` in favor of `#[AutowireDecorated]`
@fabpotfabpot mentioned this pull requestMay 1, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@GromNaNGromNaNGromNaN approved these changes

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

6.3

Development

Successfully merging this pull request may close these issues.

5 participants

@nicolas-grekas@Neirda24@GromNaN@derrabus@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp