@@ -120,12 +120,15 @@ 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');
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
+ };
129
132
130
133
// ...
131
134
@@ -209,24 +212,29 @@ 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
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
215
216
216
- // boolean parameters
217
- $container->setParameter('app.enable_v2_protocol', true);
217
+ use App\Entity\BlogPost;
218
218
219
- // array/collection parameters
220
- $container->setParameter('app.supported_locales', ['en', 'es', 'fr']);
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')
221
224
222
- // binary content parameters (use the PHP escape sequences)
223
- $container->setParameter ('app.some_parameter ', 'This is a Bell char: \x07');
225
+ // boolean parameters
226
+ ->set ('app.enable_v2_protocol ', true)
224
227
225
- // PHP constants as parameter values
226
- use App\Entity\BlogPost;
228
+ // array/collection parameters
229
+ ->set('app.supported_locales', ['en', 'es', 'fr'])
227
230
228
- $container->setParameter('app.some_constant', GLOBAL_CONSTANT);
229
- $container->setParameter('app.another_constant', BlogPost::MAX_ITEMS);
231
+ // binary content parameters (use the PHP escape sequences)
232
+ ->set('app.some_parameter', 'This is a Bell char: \x07')
233
+
234
+ // PHP constants as parameter values
235
+ ->set('app.some_constant', GLOBAL_CONSTANT)
236
+ ->set('app.another_constant', BlogPost::MAX_ITEMS);
237
+ };
230
238
231
239
// ...
232
240
@@ -278,12 +286,17 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
278
286
.. code-block :: php
279
287
280
288
// 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%',
289
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
290
+
291
+ return static function (ContainerConfigurator $container) {
292
+ $container->extension('some_package', [
293
+ // any string surrounded by two % is replaced by that parameter value
294
+ 'email_address' => '%app.admin_email%',
295
+
296
+ // ...
297
+ ]);
298
+ };
284
299
285
- // ...
286
- ]);
287
300
288
301
.. note ::
289
302
@@ -310,7 +323,12 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
310
323
.. code-block :: php
311
324
312
325
// config/services.php
313
- $container->setParameter('url_pattern', 'http://symfony.com/?foo=%%s& ; bar=%%d');
326
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
327
+
328
+ return static function (ContainerConfigurator $container) {
329
+ $container->parameters()
330
+ ->set('url_pattern', 'http://symfony.com/?foo=%%s& ; bar=%%d');
331
+ };
314
332
315
333
.. include :: /components/dependency_injection/_imports-parameters-note.rst.inc
316
334
@@ -478,12 +496,16 @@ This example shows how you could configure the database connection using an env
478
496
.. code-block :: php
479
497
480
498
// 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
- ]);
499
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
500
+
501
+ return static function (ContainerConfigurator $container) {
502
+ $container->extension('doctrine', [
503
+ 'dbal' => [
504
+ // by convention the env var names are always uppercase
505
+ 'url' => '%env(resolve:DATABASE_URL)%',
506
+ ]
507
+ ]);
508
+ };
487
509
488
510
.. seealso ::
489
511
@@ -772,13 +794,18 @@ doesn't work for parameters:
772
794
.. code-block :: php
773
795
774
796
// config/services.php
797
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
798
+
775
799
use App\Service\MessageGenerator;
776
- use Symfony\Component\DependencyInjection\Reference;
777
800
778
- $container->setParameter('app.contents_dir', '...');
801
+ return static function (ContainerConfigurator $container) {
802
+ $container->parameters()
803
+ ->set('app.contents_dir', '...');
779
804
780
- $container->getDefinition(MessageGenerator::class)
781
- ->setArgument('$contentsDir', '%app.contents_dir%');
805
+ $container->services()
806
+ ->get(MessageGenerator::class)
807
+ ->arg('$contentsDir', '%app.contents_dir%');
808
+ };
782
809
783
810
If you inject the same parameters over and over again, use the
784
811
``services._defaults.bind `` option instead. The arguments defined in that option are
@@ -824,18 +851,22 @@ whenever a service/controller defines a ``$projectDir`` argument, use this:
824
851
.. code-block :: php
825
852
826
853
// config/services.php
854
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
855
+
827
856
use App\Controller\LuckyController;
828
857
use Psr\Log\LoggerInterface;
829
858
use Symfony\Component\DependencyInjection\Reference;
830
859
831
- $container->register(LuckyController::class)
832
- ->setPublic(true)
833
- ->setBindings([
834
- // pass this value to any $projectDir argument for any service
835
- // that's created in this file (including controller arguments)
836
- '$projectDir' => '%kernel.project_dir%',
837
- ])
838
- ;
860
+ return static function (ContainerConfigurator $container) {
861
+ $container->services()
862
+ ->set(LuckyController::class)
863
+ ->public()
864
+ ->args([
865
+ // pass this value to any $projectDir argument for any service
866
+ // that's created in this file (including controller arguments)
867
+ '$projectDir' => '%kernel.project_dir%',
868
+ ]);
869
+ };
839
870
840
871
.. seealso ::
841
872
0 commit comments