Skip to content

Commit dbc99b8

Browse files
committed
Move #[When] attribute to Service Container docs
1 parent 61f5000 commit dbc99b8

File tree

2 files changed

+56
-41
lines changed

2 files changed

+56
-41
lines changed

configuration.rst

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,13 @@ files directly in the ``config/packages/`` directory.
412412

413413
.. tip::
414414

415-
You can also define options for different environments in a single configuration file.
416-
417415
.. versionadded:: 5.3
418416

419-
The ability to defined different environments in a single file was introduced in Symfony 5.3.
417+
The ability to defined different environments in a single file was
418+
introduced in Symfony 5.3.
419+
420+
You can also define options for different environments in a single
421+
configuration file using the special ``when`` keyword:
420422

421423
.. configuration-block::
422424

@@ -429,10 +431,12 @@ files directly in the ``config/packages/`` directory.
429431
strict_mode: true
430432
cache: false
431433
434+
# cache is enabled only in the "prod" environment
432435
when@prod:
433436
webpack_encore:
434437
cache: true
435438
439+
# disable strict mode only in the "test" environment
436440
when@test:
437441
webpack_encore:
438442
strict_mode: false
@@ -447,64 +451,46 @@ files directly in the ``config/packages/`` directory.
447451
https://symfony.com/schema/dic/services/services-1.0.xsd
448452
http://symfony.com/schema/dic/symfony
449453
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
450-
<webpack-encore:config>
451-
<!-- ... -->
452-
</webpack-encore:config>
454+
<webpack-encore:config
455+
output-path="%kernel.project_dir%/public/build"
456+
strict-mode="true"
457+
cache="false"
458+
/>
453459
460+
<!-- cache is enabled only in the "test" environment -->
454461
<when env="prod">
455-
<webpack-encore:config>
456-
<!-- ... -->
457-
</webpack-encore:config>
462+
<webpack-encore:config cache="true"/>
458463
</when>
459464
465+
<!-- disable strict mode only in the "test" environment -->
460466
<when env="test">
461-
<webpack-encore:config>
462-
<!-- ... -->
463-
</webpack-encore:config>
467+
<webpack-encore:config strict-mode="false"/>
464468
</when>
465469
</container>
466470
467471
.. code-block:: php
468472
469473
// config/packages/framework.php
470474
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
471-
use Symfony\Config\FrameworkConfig;
475+
use Symfony\Config\WebpackEncoreConfig;
472476
473-
return static function (FrameworkConfig $framework, ContainerConfigurator $container) {
474-
// ...
477+
return static function (WebpackEncoreConfig $webpackEncore, ContainerConfigurator $container) {
478+
$webpackEncore
479+
->outputPath('%kernel.project_dir%/public/build')
480+
->strictMode(true)
481+
->cache(false)
482+
;
475483
484+
// cache is enabled only in the "prod" environment
476485
if ('prod' === $container->env()) {
477-
// ...
486+
$webpackEncore->cache(true);
478487
}
479488
489+
// disable strict mode only in the "test" environment
480490
if ('test' === $container->env()) {
481-
$framework->test(true);
482-
$framework->session()->storageFactoryId('session.storage.mock_file');
491+
$webpackEncore->strictMode(false);
483492
}
484493
};
485-
486-
Also, if you are using PHP 8.0 or later, you can use the PHP attribute ``#[When]`` to tell that a class should only be registered as services in some environments :
487-
488-
.. configuration-block::
489-
490-
.. code-block:: php-attributes
491-
492-
use Symfony\Component\DependencyInjection\Attribute\When;
493-
494-
#[When(env: 'dev')]
495-
class SomeClass
496-
{
497-
// ...
498-
}
499-
500-
// you can apply more than one attribute to the same class:
501-
502-
#[When(env: 'dev')]
503-
#[When(env: 'test')]
504-
class AnotherClass
505-
{
506-
// ...
507-
}
508494
509495
.. seealso::
510496

service_container.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,35 @@ each time you ask for it.
222222
If you'd prefer to manually wire your service, that's totally possible: see
223223
:ref:`services-explicitly-configure-wire-services`.
224224

225+
Limiting Services to a specific Symfony Environment
226+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227+
228+
.. versionadded:: 5.3
229+
230+
The ``#[When]`` attribute was introduced in Symfony 5.3.
231+
232+
If you are using PHP 8.0 or later, you can use the ``#[When]`` PHP
233+
attribute to only register the class as a service in some environments::
234+
235+
use Symfony\Component\DependencyInjection\Attribute\When;
236+
237+
// SomeClass is only registered in the "dev" environment
238+
239+
#[When(env: 'dev')]
240+
class SomeClass
241+
{
242+
// ...
243+
}
244+
245+
// you can also apply more than one When attribute to the same class
246+
247+
#[When(env: 'dev')]
248+
#[When(env: 'test')]
249+
class AnotherClass
250+
{
251+
// ...
252+
}
253+
225254
.. _services-constructor-injection:
226255

227256
Injecting Services/Config into a Service

0 commit comments

Comments
 (0)