@@ -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
0 commit comments