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 aService attribute to explicitly choose the injected service id#45573

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

Closed
kbond wants to merge1 commit intosymfony:6.1fromkbond:service-attribute

Conversation

@kbond
Copy link
Member

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

This is a sister PR to#44780. This removes the need to reach for a separate config file when needing to inject a service with a specific service id. This could be used as aless brittle alternative to theTarget attribute.

usePsr\Log\LoggerInterface;useSymfony\Component\DependencyInjection\Attribute\Service;class MyClass{publicfunction__construct(        #[Service('monolog.logger.request')]privateLoggerInterface$logger    ) {}}

TODO

  • Controller argument support

@kbond
Copy link
MemberAuthor

kbond commentedFeb 27, 2022
edited
Loading

I think it could be nice to have aBind attribute that allows any yaml syntax to be used:

class MyService{publicfunction__construct(        #[Bind('@=service("App\\Mail\\MailerConfiguration").getMailerMethod()')private$service1,        #[Bind('@some_service')]private$service2,        #[Service('some_service')]private$service3,// equivalent to above        #[Bind('%env(json:file:resolve:AUTH_FILE)%')]private$parameter1,        #[Bind('%kernel.project_dir%/config/dir')]private$parameter2,        #[Bind('%kernel.project_dir%')]private$parameter3,        #[Parameter('kernel.project_dir')]private$parameter4,// equivalent to above    ) {}}

Service andParameter (from#44780) could extend from this.

Kocal, IonBazan, and OskarStark reacted with heart emoji

@carsonbot
Copy link

Hey!

I think@ruudk has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

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

The implementation and the feature looks fine to me, but there is still this to resolve:

I think it could be nice to have a Bind attribute that allows any yaml syntax to be used:

I would name this#[Autowire], because this wouldn't work like a binding.

If we go with a parameter that supports this yaml-like syntax, we don't need neither Service nor Parameter so both proposals are exclusive to me.

With#44780, we're going to support pretty much everything that this yaml syntax would provide. Only expressions would be missing, and supporting them is only a matter of sending a PR adding a new#[Expression] attribute.

So we have a choice to make to me, which supporting#[Autowire]+yaml-like XORParameter+Service+Expression.

If we go with the latter, I'm wondering if we shouldn't prefix them with Autowire for clarity and disambiguation: AutowireService, AutowireParameter and AutowireExpression.

Note that we already have#[TaggedIterator] and#[TaggedLocator].

@kbond
Copy link
MemberAuthor

I personally prefer a single#[Autowire] attribute that supports all yaml syntax.

I still think service/parameter/expression sub-classes could be useful/more explicit but don't feel very strongly about this. Discussion for the future.

@nicolas-grekas
Copy link
Member

Closing in favor of#45657

fabpot added a commit that referenced this pull requestMar 18, 2022
…te (kbond)This PR was merged into the 6.1 branch.Discussion----------[DependencyInjection] add `Autowire` parameter attribute| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | n/a| License       | MIT| Doc PR        | todoReplaces#45573 &#44780 with a single new `Autowire` attribute:```phpclass MyService{    public function __construct(        #[Autowire(service: 'some_service')]        private $service1,        #[Autowire(expression: 'service("App\\Mail\\MailerConfiguration").getMailerMethod()')        private $service2,        #[Autowire(value: '%env(json:file:resolve:AUTH_FILE)%')]        private $parameter1,        #[Autowire(value: '%kernel.project_dir%/config/dir')]        private $parameter2,    ) {}}```Works with controller arguments as well:```phpclass MyController{    public function someAction(        #[Autowire(service: 'some_service')]        $service1,        #[Autowire(expression: 'service("App\\Mail\\MailerConfiguration").getMailerMethod()')        $service2,        #[Autowire(value: '%env(json:file:resolve:AUTH_FILE)%')]        $parameter1,        #[Autowire(value: '%kernel.project_dir%/config/dir')]        $parameter2,    ): Response {}}```Commits-------d43fe42 [DependencyInjection] add `Autowire` parameter attribute
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

6.1

Development

Successfully merging this pull request may close these issues.

3 participants

@kbond@carsonbot@nicolas-grekas

[8]ページ先頭

©2009-2025 Movatter.jp