From 61f50005ebc8e2cde512e0632ffba9e4ce3bc894 Mon Sep 17 00:00:00 2001 From: Maxime Pinot Date: Wed, 22 Dec 2021 14:05:29 +0100 Subject: [PATCH 1/2] [Configuration] Multiple environments in a single file --- configuration.rst | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/configuration.rst b/configuration.rst index 4e31c03a3f9..f70f0fa2b61 100644 --- a/configuration.rst +++ b/configuration.rst @@ -410,6 +410,102 @@ In reality, each environment differs only somewhat from others. This means that all environments share a large base of common configuration, which is put in files directly in the ``config/packages/`` directory. +.. tip:: + + You can also define options for different environments in a single configuration file. + + .. versionadded:: 5.3 + + The ability to defined different environments in a single file was introduced in Symfony 5.3. + + .. configuration-block:: + + .. code-block:: yaml + + # config/packages/webpack_encore.yaml + webpack_encore: + # ... + output_path: '%kernel.project_dir%/public/build' + strict_mode: true + cache: false + + when@prod: + webpack_encore: + cache: true + + when@test: + webpack_encore: + strict_mode: false + + .. code-block:: xml + + + + + + + + + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/framework.php + use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + use Symfony\Config\FrameworkConfig; + + return static function (FrameworkConfig $framework, ContainerConfigurator $container) { + // ... + + if ('prod' === $container->env()) { + // ... + } + + if ('test' === $container->env()) { + $framework->test(true); + $framework->session()->storageFactoryId('session.storage.mock_file'); + } + }; + + 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 : + + .. configuration-block:: + + .. code-block:: php-attributes + + use Symfony\Component\DependencyInjection\Attribute\When; + + #[When(env: 'dev')] + class SomeClass + { + // ... + } + + // you can apply more than one attribute to the same class: + + #[When(env: 'dev')] + #[When(env: 'test')] + class AnotherClass + { + // ... + } + .. seealso:: See the ``configureContainer()`` method of From dbc99b8347b1367c98f1ceb2de6f2e26d9366425 Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sun, 16 Jan 2022 13:44:07 +0100 Subject: [PATCH 2/2] Move #[When] attribute to Service Container docs --- configuration.rst | 68 +++++++++++++++++-------------------------- service_container.rst | 29 ++++++++++++++++++ 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/configuration.rst b/configuration.rst index f70f0fa2b61..569e70a57f9 100644 --- a/configuration.rst +++ b/configuration.rst @@ -412,11 +412,13 @@ files directly in the ``config/packages/`` directory. .. tip:: - You can also define options for different environments in a single configuration file. - .. versionadded:: 5.3 - The ability to defined different environments in a single file was introduced in Symfony 5.3. + The ability to defined different environments in a single file was + introduced in Symfony 5.3. + + You can also define options for different environments in a single + configuration file using the special ``when`` keyword: .. configuration-block:: @@ -429,10 +431,12 @@ files directly in the ``config/packages/`` directory. strict_mode: true cache: false + # cache is enabled only in the "prod" environment when@prod: webpack_encore: cache: true + # disable strict mode only in the "test" environment when@test: webpack_encore: strict_mode: false @@ -447,20 +451,20 @@ files directly in the ``config/packages/`` directory. https://symfony.com/schema/dic/services/services-1.0.xsd http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> - - - + + - - - + + - - - + @@ -468,43 +472,25 @@ files directly in the ``config/packages/`` directory. // config/packages/framework.php use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; - use Symfony\Config\FrameworkConfig; + use Symfony\Config\WebpackEncoreConfig; - return static function (FrameworkConfig $framework, ContainerConfigurator $container) { - // ... + return static function (WebpackEncoreConfig $webpackEncore, ContainerConfigurator $container) { + $webpackEncore + ->outputPath('%kernel.project_dir%/public/build') + ->strictMode(true) + ->cache(false) + ; + // cache is enabled only in the "prod" environment if ('prod' === $container->env()) { - // ... + $webpackEncore->cache(true); } + // disable strict mode only in the "test" environment if ('test' === $container->env()) { - $framework->test(true); - $framework->session()->storageFactoryId('session.storage.mock_file'); + $webpackEncore->strictMode(false); } }; - - 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 : - - .. configuration-block:: - - .. code-block:: php-attributes - - use Symfony\Component\DependencyInjection\Attribute\When; - - #[When(env: 'dev')] - class SomeClass - { - // ... - } - - // you can apply more than one attribute to the same class: - - #[When(env: 'dev')] - #[When(env: 'test')] - class AnotherClass - { - // ... - } .. seealso:: diff --git a/service_container.rst b/service_container.rst index 0e44401bef2..ff1758b1bb3 100644 --- a/service_container.rst +++ b/service_container.rst @@ -222,6 +222,35 @@ each time you ask for it. If you'd prefer to manually wire your service, that's totally possible: see :ref:`services-explicitly-configure-wire-services`. +Limiting Services to a specific Symfony Environment +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 5.3 + + The ``#[When]`` attribute was introduced in Symfony 5.3. + +If you are using PHP 8.0 or later, you can use the ``#[When]`` PHP +attribute to only register the class as a service in some environments:: + + use Symfony\Component\DependencyInjection\Attribute\When; + + // SomeClass is only registered in the "dev" environment + + #[When(env: 'dev')] + class SomeClass + { + // ... + } + + // you can also apply more than one When attribute to the same class + + #[When(env: 'dev')] + #[When(env: 'test')] + class AnotherClass + { + // ... + } + .. _services-constructor-injection: Injecting Services/Config into a Service