New in Symfony 7.2: WhenNot Attribute
In Symfony 5.3, we introduced the #[When] attribute as a way to limit services
to specific config environments:
use SymfonyComponentDependencyInjectionAttributeWhen;
// this class is only registered in the „dev“ environment
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:
use SymfonyComponentDependencyInjectionAttributeWhenNot;
env: ‚dev‘)
class SomeClass
{
// …
}
// add the attribute multiple times to exclude it from several environments
env: ‚dev‘)
(env: ‚test‘)
class AnotherClass
{
// …
}
Symfony Blog