Skip to content

[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

Conversation

alamirault
Copy link
Contributor

@alamirault alamirault commented Jun 12, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #46643
License MIT
Doc PR TODO

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

@carsonbot
Copy link

Hey!

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

Cheers!

Carsonbot

Copy link
Contributor

@ruudk ruudk 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 use empty

@@ -100,6 +101,10 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
$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 use empty

*
* @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

ruudk commented Jul 26, 2022

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

@alamirault
Copy link
Contributor Author

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

chalasr commented Jul 26, 2022

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

fabpot commented Jul 26, 2022

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 commented Jul 27, 2022

I'm also moot on this one. #[When('never')] is nicely read.
If we don't like the magic value, could we use null 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 request Aug 5, 2022
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
ruudk added a commit to ruudk/symfony that referenced this pull request Aug 5, 2022
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
@ruudk
Copy link
Contributor

ruudk commented Aug 5, 2022

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 request Aug 5, 2022
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
fabpot added a commit that referenced this pull request Aug 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:

```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
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull request Aug 5, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DependencyInjection] Add attribute #[Exclude]
6 participants