-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] add #[Exclude]
in order to not register as a service
#46655
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 #[Exclude]
in order to not register as a service
#46655
Conversation
Hey! I think @ruudk has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
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 would love to see this feature merged 😍
} | ||
if (null !== $attribute) { | ||
|
||
if (!empty($r->getAttributes(Exclude::class))) { |
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.
never use empty
@@ -100,6 +101,10 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont | |||
$configBuilders = []; | |||
$r = new \ReflectionFunction($callback); | |||
|
|||
if (!empty($r->getAttributes(Exclude::class))) { |
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.
never use empty
* | ||
* @author Antoine Lamirault <lamiraultantoine@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)] |
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 wonder how this can be used on methods / functions? Wouldn't it be better to only exclude services?
@alamirault Do you need help with finishing this PR? It would be amazing to have this in Symfony 6.2 |
Note sure if is a good idea after discussion in #46643 (comment)
If it's ok for core contributors, I can continue work on it |
I won't block this as it does not hurt and it seems to be desired by the community, so I'm just +0 here :) |
As there is already a way to do it (via When) and because it's not a widespread use case, I tend to be 👎 on this one. Adding attributes comes with a cost (maintenance, docs, complexity, learning curve, ...). |
I'm also moot on this one. |
Closing as explained and to keep as is for history. Please submit another PR if you'd like to follow up with another approach. |
Thank you for the PR @alamirault! |
This allows people to extend the When attribute with something like this: ```php use Attribute; use Symfony\Component\DependencyInjection\Attribute\When; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)] final class Exclude extends When { public function __construct() { parent::__construct('never'); } } ``` Then they can use `#[Exclude]` instead of `#[When(env: 'never')]`. References: - symfony#46643 - symfony#46655
This allows people to extend the When attribute with something like this: ```php use Attribute; use Symfony\Component\DependencyInjection\Attribute\When; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)] final class Exclude extends When { public function __construct() { parent::__construct('never'); } } ``` Then they can use `#[Exclude]` instead of `#[When(env: 'never')]`. References: - symfony#46643 - symfony#46655
Too bad this was not merged but I understand the reasonings. I created a PR with a different and maybe more flexibel approach. Curious what you all think: |
This allows people to extend the When attribute with something like this: ```php use Attribute; use Symfony\Component\DependencyInjection\Attribute\When; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)] final class Exclude extends When { public function __construct() { parent::__construct('never'); } } ``` Then they can use `#[Exclude]` instead of `#[When(env: 'never')]`. References: - symfony#46643 - symfony#46655
This PR was merged into the 6.2 branch. Discussion ---------- Allow extending `#[When]` attribute | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #46643 | License | MIT | Doc PR | This allows people to extend the `#[When]` attribute with something like this: ```php namespace App\Symfony; use Attribute; use Symfony\Component\DependencyInjection\Attribute\When; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)] final class Exclude extends When { public function __construct() { parent::__construct('never'); } } ``` Then they can use `#[Exclude]` instead of `#[When(env: 'never')]`. Or they can create `#[ProductionOnly]` instead of `#[When(env: 'prod')]`. I hoped that #46655 would be merged but it turns out that is not going to happen. References: - #46643 - #46655 Commits ------- 481be92 Allow extending When attribute
This allows people to extend the When attribute with something like this: ```php use Attribute; use Symfony\Component\DependencyInjection\Attribute\When; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)] final class Exclude extends When { public function __construct() { parent::__construct('never'); } } ``` Then they can use `#[Exclude]` instead of `#[When(env: 'never')]`. References: - symfony/symfony#46643 - symfony/symfony#46655
This PR allow to use
#[Exclude]
attribute (as suggested by @AlikDex) when we don't want a class/method/function as service.It can be useful in some cases and avoid use
#[When(env: 'never')]
for bad reason and improve DX.TODO: