@@ -120,13 +120,16 @@ configuration files, even if they use a different format:
120
120
.. code-block :: php
121
121
122
122
// config/services.php
123
- $loader->import('legacy_config.xml');
124
- // the third optional argument of import() is 'ignore_errors', which
125
- // silently discards errors if the loaded file doesn't exist
126
- $loader->import('my_config_file.yaml', null, true);
127
- // glob expressions are also supported to load multiple files
128
- $loader->import('/etc/myapp/*.yaml');
129
-
123
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
124
+
125
+ return static function (ContainerConfigurator $container) {
126
+ $container->import('legacy_config.php');
127
+ // ignore_errors (3rd parameter) silently discards errors if the loaded file doesn't exist
128
+ $container->import('my_config_file.xml', null, true);
129
+ // glob expressions are also supported to load multiple files
130
+ $container->import('/etc/myapp/*.yaml');
131
+ };
132
+
130
133
// ...
131
134
132
135
.. _config-parameter-intro :
@@ -209,24 +212,25 @@ reusable configuration value. By convention, parameters are defined under the
209
212
.. code-block :: php
210
213
211
214
// config/services.php
212
- // the parameter name is an arbitrary string (the 'app.' prefix is recommended
213
- // to better differentiate your parameters from Symfony parameters).
214
- $container->setParameter('app.admin_email', 'something@example.com');
215
-
216
- // boolean parameters
217
- $container->setParameter('app.enable_v2_protocol', true);
218
-
219
- // array/collection parameters
220
- $container->setParameter('app.supported_locales', ['en', 'es', 'fr']);
215
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
221
216
222
- // binary content parameters (use the PHP escape sequences)
223
- $container->setParameter('app.some_parameter', 'This is a Bell char: \x07');
224
-
225
- // PHP constants as parameter values
226
217
use App\Entity\BlogPost;
227
218
228
- $container->setParameter('app.some_constant', GLOBAL_CONSTANT);
229
- $container->setParameter('app.another_constant', BlogPost::MAX_ITEMS);
219
+ return static function (ContainerConfigurator $container) {
220
+ $container->parameters()
221
+ // the parameter name is an arbitrary string (the 'app.' prefix is recommended
222
+ // to better differentiate your parameters from Symfony parameters).
223
+ ->set('app.admin_email', 'something@example.com')
224
+ // boolean parameters
225
+ ->set('app.enable_v2_protocol', true)
226
+ // array/collection parameters
227
+ ->set('app.supported_locales', ['en', 'es', 'fr'])
228
+ // binary content parameters (use the PHP escape sequences)
229
+ ->set('app.some_parameter', 'This is a Bell char: \x07')
230
+ // PHP constants as parameter values
231
+ ->set('app.some_constant', GLOBAL_CONSTANT)
232
+ ->set('app.another_constant', BlogPost::MAX_ITEMS);
233
+ };
230
234
231
235
// ...
232
236
@@ -278,12 +282,17 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
278
282
.. code-block :: php
279
283
280
284
// config/packages/some_package.php
281
- $container->loadFromExtension('some_package', [
282
- // any string surrounded by two % is replaced by that parameter value
283
- 'email_address' => '%app.admin_email%',
285
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
286
+
287
+ return static function (ContainerConfigurator $container) {
288
+ $container->extension('some_package', [
289
+ // any string surrounded by two % is replaced by that parameter value
290
+ 'email_address' => '%app.admin_email%',
291
+
292
+ // ...
293
+ ]);
294
+ };
284
295
285
- // ...
286
- ]);
287
296
288
297
.. note ::
289
298
@@ -310,7 +319,12 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
310
319
.. code-block :: php
311
320
312
321
// config/services.php
313
- $container->setParameter('url_pattern', 'http://symfony.com/?foo=%%s& ; bar=%%d');
322
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
323
+
324
+ return static function (ContainerConfigurator $container) {
325
+ $container->parameters()
326
+ ->set('url_pattern', 'http://symfony.com/?foo=%%s& ; bar=%%d');
327
+ };
314
328
315
329
.. include :: /components/dependency_injection/_imports-parameters-note.rst.inc
316
330
@@ -478,12 +492,16 @@ This example shows how you could configure the database connection using an env
478
492
.. code-block :: php
479
493
480
494
// config/packages/doctrine.php
481
- $container->loadFromExtension('doctrine', [
482
- 'dbal' => [
483
- // by convention the env var names are always uppercase
484
- 'url' => '%env(resolve:DATABASE_URL)%',
485
- ]
486
- ]);
495
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
496
+
497
+ return static function (ContainerConfigurator $container) {
498
+ $container->extension('doctrine', [
499
+ 'dbal' => [
500
+ // by convention the env var names are always uppercase
501
+ 'url' => '%env(resolve:DATABASE_URL)%',
502
+ ]
503
+ ]);
504
+ };
487
505
488
506
.. seealso ::
489
507
@@ -780,13 +798,18 @@ doesn't work for parameters:
780
798
.. code-block :: php
781
799
782
800
// config/services.php
801
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
802
+
783
803
use App\Service\MessageGenerator;
784
- use Symfony\Component\DependencyInjection\Reference;
785
804
786
- $container->setParameter('app.contents_dir', '...');
805
+ return static function (ContainerConfigurator $container) {
806
+ $container->parameters()
807
+ ->set('app.contents_dir', '...');
787
808
788
- $container->getDefinition(MessageGenerator::class)
789
- ->setArgument('$contentsDir', '%app.contents_dir%');
809
+ $container->services()
810
+ ->get(MessageGenerator::class)
811
+ ->arg('$contentsDir', '%app.contents_dir%');
812
+ };
790
813
791
814
If you inject the same parameters over and over again, use the
792
815
``services._defaults.bind `` option instead. The arguments defined in that option are
@@ -832,18 +855,22 @@ whenever a service/controller defines a ``$projectDir`` argument, use this:
832
855
.. code-block :: php
833
856
834
857
// config/services.php
858
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
859
+
835
860
use App\Controller\LuckyController;
836
861
use Psr\Log\LoggerInterface;
837
862
use Symfony\Component\DependencyInjection\Reference;
838
863
839
- $container->register(LuckyController::class)
840
- ->setPublic(true)
841
- ->setBindings([
842
- // pass this value to any $projectDir argument for any service
843
- // that's created in this file (including controller arguments)
844
- '$projectDir' => '%kernel.project_dir%',
845
- ])
846
- ;
864
+ return static function (ContainerConfigurator $container) {
865
+ $container->services()
866
+ ->set(LuckyController::class)
867
+ ->public()
868
+ ->args([
869
+ // pass this value to any $projectDir argument for any service
870
+ // that's created in this file (including controller arguments)
871
+ '$projectDir' => '%kernel.project_dir%',
872
+ ]);
873
+ };
847
874
848
875
.. seealso ::
849
876
0 commit comments