Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0ba536

Browse filesBrowse files
committed
feature #16313 [Configuration] Multiple environments in a single file (MaximePinot)
This PR was squashed before being merged into the 5.3 branch. Discussion ---------- [Configuration] Multiple environments in a single file As `@BafS` mentioned in the Symfony slack, the new Symfony 5.3 feature about defining options for different environments in a single file is not documented, apart from [this blog post](https://symfony.com/blog/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file). I could not find anything about it in the documentation either. Here is a proposal to include the blog post in the documentation. I put it in a `tip` section. Perhaps it is worth putting it in a new section below `Configuration Environments` (or as a subsection) ? 🤔 Commits ------- dbc99b8 Move #[When] attribute to Service Container docs 61f5000 [Configuration] Multiple environments in a single file
2 parents 0aea09a + dbc99b8 commit e0ba536
Copy full SHA for e0ba536

File tree

Expand file treeCollapse file tree

2 files changed

+111
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+111
-0
lines changed

‎configuration.rst

Copy file name to clipboardExpand all lines: configuration.rst
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,88 @@ 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+
.. versionadded:: 5.3
416+
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:
422+
423+
.. configuration-block::
424+
425+
.. code-block:: yaml
426+
427+
# config/packages/webpack_encore.yaml
428+
webpack_encore:
429+
# ...
430+
output_path: '%kernel.project_dir%/public/build'
431+
strict_mode: true
432+
cache: false
433+
434+
# cache is enabled only in the "prod" environment
435+
when@prod:
436+
webpack_encore:
437+
cache: true
438+
439+
# disable strict mode only in the "test" environment
440+
when@test:
441+
webpack_encore:
442+
strict_mode: false
443+
444+
.. code-block:: xml
445+
446+
<!-- config/packages/webpack_encore.xml -->
447+
<?xml version="1.0" encoding="UTF-8" ?>
448+
<container xmlns="http://symfony.com/schema/dic/services"
449+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
450+
xsi:schemaLocation="http://symfony.com/schema/dic/services
451+
https://symfony.com/schema/dic/services/services-1.0.xsd
452+
http://symfony.com/schema/dic/symfony
453+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
454+
<webpack-encore:config
455+
output-path="%kernel.project_dir%/public/build"
456+
strict-mode="true"
457+
cache="false"
458+
/>
459+
460+
<!-- cache is enabled only in the "test" environment -->
461+
<when env="prod">
462+
<webpack-encore:config cache="true"/>
463+
</when>
464+
465+
<!-- disable strict mode only in the "test" environment -->
466+
<when env="test">
467+
<webpack-encore:config strict-mode="false"/>
468+
</when>
469+
</container>
470+
471+
.. code-block:: php
472+
473+
// config/packages/framework.php
474+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
475+
use Symfony\Config\WebpackEncoreConfig;
476+
477+
return static function (WebpackEncoreConfig $webpackEncore, ContainerConfigurator $container) {
478+
$webpackEncore
479+
->outputPath('%kernel.project_dir%/public/build')
480+
->strictMode(true)
481+
->cache(false)
482+
;
483+
484+
// cache is enabled only in the "prod" environment
485+
if ('prod' === $container->env()) {
486+
$webpackEncore->cache(true);
487+
}
488+
489+
// disable strict mode only in the "test" environment
490+
if ('test' === $container->env()) {
491+
$webpackEncore->strictMode(false);
492+
}
493+
};
494+
413495
.. seealso::
414496

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

‎service_container.rst

Copy file name to clipboardExpand all lines: service_container.rst
+29Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.