Quantcast
Channel: Symfony Blog
Viewing all articles
Browse latest Browse all 3098

New in Symfony 7.2: WhenNot Attribute

$
0
0
Alexandre Daubois
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 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:

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
{
    // ...
}

Sponsor the Symfony project.

Viewing all articles
Browse latest Browse all 3098

Latest Images

Trending Articles



Latest Images