Skip to content

Commit 39be3e6

Browse files
[DependencyInjection] Add #[WhenNot] attribute
1 parent 21065ac commit 39be3e6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

reference/attributes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Dependency Injection
4343
* :ref:`TaggedLocator <service-subscribers-locators_defining-service-locator>`
4444
* :ref:`Target <autowiring-multiple-implementations-same-type>`
4545
* :ref:`When <service-container_limiting-to-env>`
46+
* :ref:`WhenNot <service-container_limiting-to-env>`
4647

4748
EventDispatcher
4849
~~~~~~~~~~~~~~~

service_container.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,32 @@ as a service in some environments::
260260
// ...
261261
}
262262

263+
If you want to exclude a service from being registered in a specific
264+
environment, you can use the ``#[WhenNot]`` attribute::
265+
266+
use Symfony\Component\DependencyInjection\Attribute\WhenNot;
267+
268+
// SomeClass is registered in all environments except "dev"
269+
270+
#[WhenNot(env: 'dev')]
271+
class SomeClass
272+
{
273+
// ...
274+
}
275+
276+
// you can apply more than one WhenNot attribute to the same class
277+
278+
#[WhenNot(env: 'dev')]
279+
#[WhenNot(env: 'test')]
280+
class AnotherClass
281+
{
282+
// ...
283+
}
284+
285+
.. versionadded:: 7.2
286+
287+
The ``#[WhenNot]`` attribute was introduced in Symfony 7.2.
288+
263289
.. _services-constructor-injection:
264290

265291
Injecting Services/Config into a Service

0 commit comments

Comments
 (0)