@@ -412,11 +412,13 @@ files directly in the ``config/packages/`` directory.
412
412
413
413
.. tip ::
414
414
415
- You can also define options for different environments in a single configuration file.
416
-
417
415
.. versionadded :: 5.3
418
416
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:
420
422
421
423
.. configuration-block ::
422
424
@@ -429,10 +431,12 @@ files directly in the ``config/packages/`` directory.
429
431
strict_mode : true
430
432
cache : false
431
433
434
+ # cache is enabled only in the "prod" environment
432
435
when@prod :
433
436
webpack_encore :
434
437
cache : true
435
438
439
+ # disable strict mode only in the "test" environment
436
440
when@test :
437
441
webpack_encore :
438
442
strict_mode : false
@@ -447,64 +451,46 @@ files directly in the ``config/packages/`` directory.
447
451
https://symfony.com/schema/dic/services/services-1.0.xsd
448
452
http://symfony.com/schema/dic/symfony
449
453
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
+ />
453
459
460
+ <!-- cache is enabled only in the "test" environment -->
454
461
<when env =" prod" >
455
- <webpack-encore : config >
456
- <!-- ... -->
457
- </webpack-encore : config >
462
+ <webpack-encore : config cache =" true" />
458
463
</when >
459
464
465
+ <!-- disable strict mode only in the "test" environment -->
460
466
<when env =" test" >
461
- <webpack-encore : config >
462
- <!-- ... -->
463
- </webpack-encore : config >
467
+ <webpack-encore : config strict-mode =" false" />
464
468
</when >
465
469
</container >
466
470
467
471
.. code-block :: php
468
472
469
473
// config/packages/framework.php
470
474
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
471
- use Symfony\Config\FrameworkConfig ;
475
+ use Symfony\Config\WebpackEncoreConfig ;
472
476
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
+ ;
475
483
484
+ // cache is enabled only in the "prod" environment
476
485
if ('prod' === $container->env()) {
477
- // ...
486
+ $webpackEncore->cache(true);
478
487
}
479
488
489
+ // disable strict mode only in the "test" environment
480
490
if ('test' === $container->env()) {
481
- $framework->test(true);
482
- $framework->session()->storageFactoryId('session.storage.mock_file');
491
+ $webpackEncore->strictMode(false);
483
492
}
484
493
};
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
- }
508
494
509
495
.. seealso ::
510
496
0 commit comments