Skip to content

[DependencyInjection] Add #[WhenNot] attribute #57379

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
Jun 25, 2024

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Jun 12, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

When dealing with many environment and concrete+test implementations of many services, it can be tricky/verbose to repeat #[When] for each and every existing env. By using #[NotWhen], it becomes handy to deal with different implementations of a service across different env:

// ConcreteService.php
#[WhenNot(env: 'test')]
class ConcreteService implements MyServiceInterface
{
    public function call()
    {
        dump("I'm a concrete service!");
    }
}

// TestService.php
#[When(env: 'test')]
class TestService implements MyServiceInterface
{
    public function call()
    {
        dump("I'm a test service!");
    }
}

// MyCommand.php
class MyCommand extends Command
{
    public function __construct(private MyServiceInterface $service)
    {
        parent::__construct();
    }

    // ...
}

It also eases the creation of a new env: no more need to go across all #[When] occurrences to update service definitions.

You cannot use When and NotWhen at the same time:

#[WhenNot(env: 'dev')]
#[When(env: 'test')]
class TestService implements MyServiceInterface
{
    public function call()
    {
        dump("I'm a test service!");
    }
}

// Throws a LogicException: The "App\Service\TestService" class cannot have both #[When] and #[NotWhen] attributes.

@alexandre-daubois
Copy link
Member Author

Tests added on both attribute on classes and functions.

@nicolas-grekas
Copy link
Member

What about WhenNot? would that help with DX + IDE autocompletion?

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Jun 13, 2024

I was not sure at first it makes sense in English but thinking about it again, seems fine when writing it. This is way better for discovery indeed:

Capture d’écran 2024-06-13 à 10 50 24

So it's a 👍 to me

@alexandre-daubois alexandre-daubois changed the title [DependencyInjection] Add #[NotWhen] attribute [DependencyInjection] Add #[WhenNot] attribute Jun 13, 2024
@dkarlovi
Copy link
Contributor

Note that the Choice validator was inverted not by adding NotChoice (suggested in #26074) but by adding match=false to the original validator in #45977.

It might make sense to streamline here?

@alexandre-daubois
Copy link
Member Author

I kind of find it hard to understand at first glance personally. I'm afraid it gives "yoga style" kind of confusion when reading it.
But I'd understand if we want to keep some consistency (I'm not sure I fully read the discussion about Choice), just my thought 🙂

@nicolas-grekas
Copy link
Member

What's the status of this PR?

@alexandre-daubois
Copy link
Member Author

Status: Needs Review

I still think that would be a nice addition for DX 👍

@fabpot
Copy link
Member

fabpot commented Jun 25, 2024

Thank you @alexandre-daubois.

@fabpot fabpot merged commit 0455a84 into symfony:7.2 Jun 25, 2024
5 of 10 checks passed
@alexandre-daubois alexandre-daubois deleted the not-when branch June 25, 2024 20:00
@fabpot fabpot mentioned this pull request Oct 27, 2024
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.

6 participants