@@ -120,13 +120,17 @@ 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
124
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
125
+
126
+ return static function (ContainerConfigurator $container) {
127
+ $container->import('legacy_config.php');
128
+ // ignore_errors (3rd parameter) silently discards errors if the loaded file doesn't exist
129
+ $container->import('my_config_file.xml', null, true);
130
+ // glob expressions are also supported to load multiple files
131
+ $container->import('/etc/myapp/*.yaml');
132
+ };
133
+
130
134
// ...
131
135
132
136
.. _config-parameter-intro :
@@ -209,24 +213,26 @@ reusable configuration value. By convention, parameters are defined under the
209
213
.. code-block :: php
210
214
211
215
// 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', '[email protected] ');
215
-
216
- // boolean parameters
217
- $container->setParameter('app.enable_v2_protocol', true);
218
216
219
- // array/collection parameters
220
- $container->setParameter('app.supported_locales', ['en', 'es', 'fr']);
217
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
221
218
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
219
use App\Entity\BlogPost;
227
220
228
- $container->setParameter('app.some_constant', GLOBAL_CONSTANT);
229
- $container->setParameter('app.another_constant', BlogPost::MAX_ITEMS);
221
+ return static function (ContainerConfigurator $container) {
222
+ $container->parameters()
223
+ // the parameter name is an arbitrary string (the 'app.' prefix is recommended
224
+ // to better differentiate your parameters from Symfony parameters).
225
+ ->set('app.admin_email', '[email protected] ')
226
+ // boolean parameters
227
+ ->set('app.enable_v2_protocol', true)
228
+ // array/collection parameters
229
+ ->set('app.supported_locales', ['en', 'es', 'fr'])
230
+ // binary content parameters (use the PHP escape sequences)
231
+ ->set('app.some_parameter', 'This is a Bell char: \x07')
232
+ // PHP constants as parameter values
233
+ ->set('app.some_constant', GLOBAL_CONSTANT)
234
+ ->set('app.another_constant', BlogPost::MAX_ITEMS);
235
+ };
230
236
231
237
// ...
232
238
@@ -278,12 +284,18 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
278
284
.. code-block :: php
279
285
280
286
// 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%',
284
287
285
- // ...
286
- ]);
288
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
289
+
290
+ return static function (ContainerConfigurator $container) {
291
+ $container->extension('some_package', [
292
+ // any string surrounded by two % is replaced by that parameter value
293
+ 'email_address' => '%app.admin_email%',
294
+
295
+ // ...
296
+ ]);
297
+ };
298
+
287
299
288
300
.. note ::
289
301
@@ -310,7 +322,13 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
310
322
.. code-block :: php
311
323
312
324
// config/services.php
313
- $container->setParameter('url_pattern', 'http://symfony.com/?foo=%%s& ; bar=%%d');
325
+
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,17 @@ 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
+
500
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
501
+
502
+ return static function (ContainerConfigurator $container) {
503
+ $container->extension('doctrine', [
504
+ 'dbal' => [
505
+ // by convention the env var names are always uppercase
506
+ 'url' => '%env(resolve:DATABASE_URL)%',
507
+ ]
508
+ ]);
509
+ };
487
510
488
511
.. seealso ::
489
512
@@ -780,13 +803,19 @@ doesn't work for parameters:
780
803
.. code-block :: php
781
804
782
805
// config/services.php
806
+
807
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
808
+
783
809
use App\Service\MessageGenerator;
784
- use Symfony\Component\DependencyInjection\Reference;
785
810
786
- $container->setParameter('app.contents_dir', '...');
811
+ return static function (ContainerConfigurator $container) {
812
+ $container->parameters()
813
+ ->set('app.contents_dir', '...');
787
814
788
- $container->getDefinition(MessageGenerator::class)
789
- ->setArgument('$contentsDir', '%app.contents_dir%');
815
+ $container->services()
816
+ ->get(MessageGenerator::class)
817
+ ->arg('$contentsDir', '%app.contents_dir%');
818
+ };
790
819
791
820
If you inject the same parameters over and over again, use the
792
821
``services._defaults.bind `` option instead. The arguments defined in that option are
@@ -832,18 +861,23 @@ whenever a service/controller defines a ``$projectDir`` argument, use this:
832
861
.. code-block :: php
833
862
834
863
// config/services.php
864
+
865
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
866
+
835
867
use App\Controller\LuckyController;
836
868
use Psr\Log\LoggerInterface;
837
869
use Symfony\Component\DependencyInjection\Reference;
838
870
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
- ;
871
+ return static function (ContainerConfigurator $container) {
872
+ $container->services()
873
+ ->set(LuckyController::class)
874
+ ->public()
875
+ ->args([
876
+ // pass this value to any $projectDir argument for any service
877
+ // that's created in this file (including controller arguments)
878
+ '$projectDir' => '%kernel.project_dir%',
879
+ ]);
880
+ };
847
881
848
882
.. seealso ::
849
883
0 commit comments