-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Add attribute #[Exclude] #46643
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
Comments
Nice idea, but I don't like the name |
Is it really? I quite like it: it both teaches/builds on existing knowledge about the |
I think so too. Having something like |
I agree it's too much of a hack :) |
That depends. It's often easy to exclude directories when you group files by type (form, controller, etc). But when you are not following that approach, it becomes very tedious to exclude certain files one by one. Especially in larger applications. I think it makes perfect sense to be able to exclude by attribute as we can almost do everything already via attributes. |
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
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
Hello, actually #47196 did not fix this issue. So I'm re-opening it. There was 7 👍🏼 reaction on this issue, and I would like to see it in the core. I can submit a PR if you think it's OK.
That's not an issue for me. I usually ignore all Exception, Model, Dto, etc folders. But for time to time I have static helpers that are not Symfony services. And I want to exclude them from the DIC. |
@lyrixx When you say "did not fix this issue" what do you mean? Because we are using this and it works great: <?php
declare(strict_types=1);
namespace App;
use Attribute;
use Symfony\Component\DependencyInjection\Attribute\When;
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)]
final class SkipAutowire extends When
{
public function __construct()
{
parent::__construct('never');
}
} However, I would love to see this become a Framework attribute. |
The title of this PR is :
And there not |
…e (lyrixx) This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Add support for Exclude attribute | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #46643 | License | MIT | Doc PR | Commits ------- cd64c67 [DependencyInjection] Add support for Exclude attribute
Description
Maybe we should add an Exclude attribute to exclude dto\vo\entity and other classes from registration in the service container?
Now I can do it with the When attribute. For example:
But that seems like bad practice
Example
The text was updated successfully, but these errors were encountered: