Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
AlikDex opened this issue Jun 11, 2022 · 8 comments · Fixed by #47196 or #49492
Closed

[DependencyInjection] Add attribute #[Exclude] #46643

AlikDex opened this issue Jun 11, 2022 · 8 comments · Fixed by #47196 or #49492

Comments

@AlikDex
Copy link

AlikDex commented Jun 11, 2022

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:

<?php

use Symfony\Component\DependencyInjection\Attribute\When;

#[When(env: 'never')]
class SomeClass
{
    // ...
}

But that seems like bad practice

Example

<?php

use Symfony\Component\DependencyInjection\Attribute\Exclude;

#[Exclude]
class SomeClass
{
    // ...
}
@Eydamos
Copy link

Eydamos commented Jun 13, 2022

Nice idea, but I don't like the name Exclude for that. Excluded from what? It is not very speaking and hard to follow for beginners. I would propose something like NoService

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 13, 2022

#[When(env: 'never')]
But that seems like bad practice

Is it really? I quite like it: it both teaches/builds on existing knowledge about the When attr and reads nice to me.

@Eydamos
Copy link

Eydamos commented Jun 13, 2022

Is it really? I quite like it: it both teaches/builds on existing knowledge about the When attr and reads nice to me.

I think so too. Having something like env:'never' while the environment with the name never will actually never exist really feels like bad practice.
It is like: "Hey let me just put some random string here that might potentially give a hint that this will not exist just because I have no other way to excluded it from being a service"
Other people might be lazy and just do env:"foobarbaz" and a year later no one in the team knows any more why this was added.

@chalasr
Copy link
Member

chalasr commented Jun 13, 2022

I agree it's too much of a hack :)
Anyway, do we really want to put an attribute imported from the DependencyInjection component to all non-service classes? There's no plan to get rid of the default services.yaml|php file that enables PSR-4 based service discovery. Isn't adding a one-liner to that file simpler than putting that attribute to every single non-service class?

@ruudk
Copy link
Contributor

ruudk commented Jul 26, 2022

Isn't adding a one-liner to that file simpler than putting that attribute to every single non-service class?

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.

ruudk added a commit to ruudk/symfony that referenced this issue 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 issue 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 issue 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 fabpot closed this as completed Aug 5, 2022
fabpot added a commit that referenced this issue 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 issue 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
@lyrixx
Copy link
Member

lyrixx commented Feb 10, 2023

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.

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

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 lyrixx reopened this Feb 10, 2023
@ruudk
Copy link
Contributor

ruudk commented Feb 10, 2023

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

@lyrixx
Copy link
Member

lyrixx commented Feb 10, 2023

The title of this PR is :

[DependencyInjection] Add attribute #[Exclude]

And there not Exclude attribute in the Framework (yet?). So, IMHO, the issue is not fixed.

nicolas-grekas added a commit that referenced this issue Feb 23, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment