New in Symfony 7.2: WhenNot Attribute

Contributed by
Alexandre Daubois
in
#57379

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

#[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:

use SymfonyComponentDependencyInjectionAttributeWhenNot;

#[WhenNot(env: ‚dev‘)]
class SomeClass
{
// …
}

// add the attribute multiple times to exclude it from several environments

#[WhenNot(env: ‚dev‘)]
#[WhenNot(env: ‚test‘)]
class AnotherClass
{
// …
}

Sponsor the Symfony project.

Symfony Blog

Read More

Latest News

PHP-Releases

PHP 8.2.24 released!

PHP 8.3.12 released!

Generated by Feedzy