Skip to content

Commit 61f5000

Browse files
MaximePinotwouterj
authored andcommitted
[Configuration] Multiple environments in a single file
1 parent 764d67c commit 61f5000

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

configuration.rst

+96
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,102 @@ In reality, each environment differs only somewhat from others. This means that
410410
all environments share a large base of common configuration, which is put in
411411
files directly in the ``config/packages/`` directory.
412412

413+
.. tip::
414+
415+
You can also define options for different environments in a single configuration file.
416+
417+
.. versionadded:: 5.3
418+
419+
The ability to defined different environments in a single file was introduced in Symfony 5.3.
420+
421+
.. configuration-block::
422+
423+
.. code-block:: yaml
424+
425+
# config/packages/webpack_encore.yaml
426+
webpack_encore:
427+
# ...
428+
output_path: '%kernel.project_dir%/public/build'
429+
strict_mode: true
430+
cache: false
431+
432+
when@prod:
433+
webpack_encore:
434+
cache: true
435+
436+
when@test:
437+
webpack_encore:
438+
strict_mode: false
439+
440+
.. code-block:: xml
441+
442+
<!-- config/packages/webpack_encore.xml -->
443+
<?xml version="1.0" encoding="UTF-8" ?>
444+
<container xmlns="http://symfony.com/schema/dic/services"
445+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
446+
xsi:schemaLocation="http://symfony.com/schema/dic/services
447+
https://symfony.com/schema/dic/services/services-1.0.xsd
448+
http://symfony.com/schema/dic/symfony
449+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
450+
<webpack-encore:config>
451+
<!-- ... -->
452+
</webpack-encore:config>
453+
454+
<when env="prod">
455+
<webpack-encore:config>
456+
<!-- ... -->
457+
</webpack-encore:config>
458+
</when>
459+
460+
<when env="test">
461+
<webpack-encore:config>
462+
<!-- ... -->
463+
</webpack-encore:config>
464+
</when>
465+
</container>
466+
467+
.. code-block:: php
468+
469+
// config/packages/framework.php
470+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
471+
use Symfony\Config\FrameworkConfig;
472+
473+
return static function (FrameworkConfig $framework, ContainerConfigurator $container) {
474+
// ...
475+
476+
if ('prod' === $container->env()) {
477+
// ...
478+
}
479+
480+
if ('test' === $container->env()) {
481+
$framework->test(true);
482+
$framework->session()->storageFactoryId('session.storage.mock_file');
483+
}
484+
};
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+
413509
.. seealso::
414510

415511
See the ``configureContainer()`` method of

0 commit comments

Comments
 (0)