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 AsDecorator class attribute and InnerService parameter attribute#45834

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

@Jean-Beru
Copy link
Contributor

@Jean-BeruJean-Beru commentedMar 24, 2022
edited by nicolas-grekas
Loading

QA
Branch?6.1
Bug fix?no
New feature?yes
Deprecations?no
Tickets
LicenseMIT
Doc PRWIP

The aim f this PR is to allow decorator declaration with PHP8 attributes by usingAsDecorator attribute on class and, optionally,InnerService parameter attribute to inject decorated service.

To use it :

interface FooInterface{publicfunctionmyMethod():string;}finalclass Fooimplements FooInterface{publicfunctionmyMethod():string    {return'foo';    }}#[AsDecorator(    decorates: Foo::class,     priority:5,     onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE,)]finalclass Barimplements FooInterface{privatestring$arg1;private ?FooInterface$foo;publicfunction__construct(string$arg1, #[InnerService]FooInterface$foo =null)    {$this->arg1 =$arg1;$this->foo =$foo;    }publicfunctionmyMethod():string    {if (null ===$this->foo) {return'bar';        }return$this->foo->myMethod().' bar';    }}

kaznovac, alexandre-daubois, and zmitic reacted with thumbs up emoji
@nicolas-grekas
Copy link
Member

nicolas-grekas commentedMar 24, 2022
edited
Loading

Thanks for the PR.

I see several issues with the current approach:

  • loadingall classes in the container is no-go (this could have a huge perf impact at compile-time)
  • unconditionally looking for attributes is no-go (this breaks their declarative property by making them imperative configuration)
  • we should avoid using a generic name (Service) - future needs can be handled by new attributes. We could use#[AsDecorator] maybe?
  • decorationArgumentKey anddecorationInnerName don't make sense to me. We could use another attribute instead, put on an argument, to tell where the decorated service should be injected, egfunction __construct(#[DecoratedService] $decorated), or#[Inner].

Instead of adding a new pass, I think the attributes should be looked for in AutowirePass, where there is already some logic that deals with decoration. This would fix all listed issues I believe.

@nicolas-grekas
Copy link
Member

(please mind the PR title+description also ;))

@Jean-BeruJean-Beru changed the title[DependencyInjection] add Service class attribute[DependencyInjection] add AsDecorator class attribute and Inner parameter attributeApr 14, 2022
@Jean-Beru
Copy link
ContributorAuthor

Thanks for your reviews ! Code and description have been updated :)

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.

LGTM. Can you please confirm that you ran this on a small project and that it worked as expected?

Copy link
Member

@dunglasdunglas left a comment

Choose a reason for hiding this comment

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

👍

@Jean-Beru
Copy link
ContributorAuthor

Jean-Beru commentedApr 15, 2022
edited by nicolas-grekas
Loading

I tried from a new demo application with :

finalclass Fooimplements FooInterface{publicfunction__invoke():string    {return'new'.__CLASS__.'()';    }}#[AsDecorator(decorates: Foo::class, decorationPriority:10)]finalclass Bar10implements FooInterface{privateFooInterface$foo;publicfunction__construct(#[InnerService]FooInterface$foo)    {$this->foo =$foo;    }publicfunction__invoke():string    {return'new'.__CLASS__.'('.($this->foo)().')';    }}#[AsDecorator(decorates: Foo::class, decorationPriority:20)]finalclass Bar20implements FooInterface{privateFooInterface$foo;publicfunction__construct(#[InnerServicce]FooInterface$foo)    {$this->foo =$foo;    }publicfunction__invoke():string    {return'new'.__CLASS__.'('.($this->foo)().')';    }}#[AsController]class TestController{privateFooInterface$foo;publicfunction__construct(#[Autowire(service: Foo::class)]FooInterface$foo)    {$this->foo =$foo;    }    #[Route('/test/')]publicfunctionindex():Response    {returnnewResponse(($this->foo)());    }}

Calling/test/ returnsnew App\Services\Bar10(new App\Services\Bar20(new App\Services\Foo())).

nicolas-grekas reacted with thumbs up emoji

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.

Thanks! I'm just bike-shedding on the naming, WDYT?
Esp Inner vs DecoratedService? (or just Decorated)?
Any opinion @symfony/mergers? (see#45834 (comment) for examples)

@Jean-BeruJean-Beru changed the title[DependencyInjection] add AsDecorator class attribute and Inner parameter attribute[DependencyInjection] add AsDecorator class attribute and InnerService parameter attributeApr 15, 2022
@nicolas-grekas
Copy link
Member

Thank you@Jean-Beru.

@nicolas-grekasnicolas-grekas merged commit84d35a2 intosymfony:6.1Apr 19, 2022
@Jean-BeruJean-Beru deleted the di/add-service-attribute branchApril 19, 2022 16:23
fabpot added a commit that referenced this pull requestApr 22, 2022
…apDecorated]` (chalasr)This PR was merged into the 6.1 branch.Discussion----------[DependencyInjection] Rename `#[InnerService]` to `#[MapDecorated]`| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | no but tweaks one#45834| Deprecations? | no| Tickets       | -| License       | MIT| Doc PR        | -When talking about the decorator pattern outside of Symfony, the term `inner` is pretty much (if not totally) inexistent. Worse, it makes the concept harder to explain. Take "there is a decorator and a decorated object" vs "there is a decorator and an inner object": using the later form, one has to explain both what is the decorator and what is the inner. While using the former, explaining what the decorator makes it obvious what the decorated is.I propose to take the addition of this attribute as an opportunity to start making this special term go away.Also I removed the `Service` suffix. It is tempting to keep it for explicitness, but it feels kinda redundant and AFAIK no other core attribute has such redundant suffix, maybe because their namespace is enough to indicate their target.Commits-------c0a7979 [DependencyInjection] Rename `#[InnerService]` to `#[MapDecorated]`
@fabpotfabpot mentioned this pull requestApr 27, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@kbondkbondkbond left review comments

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@dunglasdunglasdunglas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

6.1

Development

Successfully merging this pull request may close these issues.

5 participants

@Jean-Beru@nicolas-grekas@dunglas@kbond@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp