In Symfony 5.3, we introduced the #[When] attribute as a way to limit services to specific config environments:
1 2 3 4 5 6 7 8 9
use Symfony\Component\DependencyInjection\Attribute\When;
// this class is only registered in the "dev" environment
#[When(env: 'dev')]
class SomeClass
{
// ...
}
This works well, but when dealing with numerous service implementations (e.g. for tests)
it can be cumbersome to define the #[When]
attribute on each service. That's why
in Symfony 7.2, we're introducing the opposite attribute: #[WhenNot]
.
This new attribute allows you to exclude a service from certain environments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use Symfony\Component\DependencyInjection\Attribute\WhenNot;
#[WhenNot(env: 'dev')]
class SomeClass
{
// ...
}
// add the attribute multiple times to exclude it from several environments
#[WhenNot(env: 'dev')]
#[WhenNot(env: 'test')]
class AnotherClass
{
// ...
}
Nice!