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#[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@alamirault
Copy link
Contributor

@alamiraultalamirault commentedJun 12, 2022
edited
Loading

QA
Branch?6.2
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix#46643
LicenseMIT
Doc PRTODO

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:

  • PR symfony doc

TomasVotruba reacted with thumbs up emoji
@carsonbot
Copy link

Hey!

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

Cheers!

Carsonbot

Copy link
Contributor

@ruudkruudk left a 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))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

never useempty

$configBuilders = [];
$r =new \ReflectionFunction($callback);

if (!empty($r->getAttributes(Exclude::class))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

never useempty

*
* @author Antoine Lamirault <lamiraultantoine@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)]
Copy link
Contributor

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?

@ruudk
Copy link
Contributor

@alamirault Do you need help with finishing this PR? It would be amazing to have this in Symfony 6.2

@alamirault
Copy link
ContributorAuthor

Note sure if is a good idea after discussion in#46643 (comment)

do we really want to put an attribute imported from the DependencyInjection component to all non-service classes

If it's ok for core contributors, I can continue work on it

@chalasr
Copy link
Member

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 :)

@fabpot
Copy link
Member

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, ...).

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedJul 27, 2022
edited
Loading

I'm also moot on this one.#[When('never')] is nicely read.
If we don't like the magic value, could we usenull for that purpose:#[When(null)]?

@nicolas-grekas
Copy link
Member

Closing as explained and to keep as is for history. Please submit another PR if you'd like to follow up with another approach.

@nicolas-grekas
Copy link
Member

Thank you for the PR@alamirault!

ruudk added a commit to ruudk/symfony that referenced this pull requestAug 5, 2022
This allows people to extend the When attribute with something like this:```phpuse 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
ruudk added a commit to ruudk/symfony that referenced this pull requestAug 5, 2022
This allows people to extend the When attribute with something like this:```phpuse 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
@ruudk
Copy link
Contributor

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:

ruudk added a commit to ruudk/symfony that referenced this pull requestAug 5, 2022
This allows people to extend the When attribute with something like this:```phpuse 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
fabpot added a commit that referenced this pull requestAug 5, 2022
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:```phpnamespace 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-#46655Commits-------481be92 Allow extending When attribute
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull requestAug 5, 2022
This allows people to extend the When attribute with something like this:```phpuse 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
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@ruudkruudkruudk requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

6.2

Development

Successfully merging this pull request may close these issues.

[DependencyInjection] Add attribute #[Exclude]

6 participants

@alamirault@carsonbot@ruudk@chalasr@fabpot@nicolas-grekas

[8]ページ先頭

©2009-2025 Movatter.jp