Skip to content

[DependencyInjection] Add #[WhenNot] attribute #19994

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 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Dependency Injection
* :ref:`TaggedLocator <service-subscribers-locators_defining-service-locator>`
* :ref:`Target <autowiring-multiple-implementations-same-type>`
* :ref:`When <service-container_limiting-to-env>`
* :ref:`WhenNot <service-container_limiting-to-env>`

EventDispatcher
~~~~~~~~~~~~~~~
Expand Down
26 changes: 26 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,32 @@ as a service in some environments::
// ...
}

If you want to exclude a service from being registered in a specific
environment, you can use the ``#[WhenNot]`` attribute::

use Symfony\Component\DependencyInjection\Attribute\WhenNot;

// SomeClass is registered in all environments except "dev"

#[WhenNot(env: 'dev')]
class SomeClass
{
// ...
}

// you can apply more than one WhenNot attribute to the same class

#[WhenNot(env: 'dev')]
#[WhenNot(env: 'test')]
class AnotherClass
{
// ...
}

.. versionadded:: 7.2

The ``#[WhenNot]`` attribute was introduced in Symfony 7.2.

.. _services-constructor-injection:

Injecting Services/Config into a Service
Expand Down
Loading