-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DependencyInjection] add AsDecorator class attribute and InnerService parameter attribute #45834
Conversation
Thanks for the PR. I see several issues with the current approach:
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. |
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/RegisterDecoratorAttributesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/RegisterDecoratorAttributesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/RegisterDecoratorAttributesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/RegisterDecoratorAttributesPass.php
Outdated
Show resolved
Hide resolved
(please mind the PR title+description also ;)) |
Thanks for your reviews ! Code and description have been updated :) |
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.
LGTM. Can you please confirm that you ran this on a small project and that it worked as expected?
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 tried from a new demo application with : final class Foo implements FooInterface
{
public function __invoke(): string
{
return 'new '.__CLASS__.'()';
}
}
#[AsDecorator(decorates: Foo::class, decorationPriority: 10)]
final class Bar10 implements FooInterface
{
private FooInterface $foo;
public function __construct(#[InnerService] FooInterface $foo)
{
$this->foo = $foo;
}
public function __invoke(): string
{
return 'new '.__CLASS__.'('.($this->foo)().')';
}
}
#[AsDecorator(decorates: Foo::class, decorationPriority: 20)]
final class Bar20 implements FooInterface
{
private FooInterface $foo;
public function __construct(#[InnerServicce] FooInterface $foo)
{
$this->foo = $foo;
}
public function __invoke(): string
{
return 'new '.__CLASS__.'('.($this->foo)().')';
}
}
#[AsController]
class TestController
{
private FooInterface $foo;
public function __construct(#[Autowire(service: Foo::class)] FooInterface $foo)
{
$this->foo = $foo;
}
#[Route('/test/')]
public function index(): Response
{
return new Response(($this->foo)());
}
} Calling |
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.
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)
src/Symfony/Component/DependencyInjection/Attribute/AsDecorator.php
Outdated
Show resolved
Hide resolved
…e parameter attribute
c0c5468
to
d8a4680
Compare
Thank you @Jean-Beru. |
…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]`
The aim f this PR is to allow decorator declaration with PHP8 attributes by using
AsDecorator
attribute on class and, optionally,InnerService
parameter attribute to inject decorated service.To use it :