Skip to content

Allow extending #[When] attribute #47196

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

Merged
merged 1 commit into from
Aug 5, 2022
Merged

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Aug 5, 2022

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:

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:

@derrabus
Copy link
Member

derrabus commented Aug 5, 2022

I like that change. Please add tests though.

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 ruudk force-pushed the instance-of-when branch from 4aa2fca to 481be92 Compare August 5, 2022 09:08
@ruudk
Copy link
Contributor Author

ruudk commented Aug 5, 2022

@derrabus Done!

@fabpot
Copy link
Member

fabpot commented Aug 5, 2022

Thank you @ruudk.

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]
5 participants