@@ -158,7 +158,7 @@ each time you ask for it.
158
158
# this creates a service per class whose id is the fully-qualified class name
159
159
App\ :
160
160
resource : ' ../src/*'
161
- exclude : ' ../src/{Entity,Migrations,Tests,Kernel.php}'
161
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php}'
162
162
163
163
# ...
164
164
@@ -175,26 +175,25 @@ each time you ask for it.
175
175
<!-- Default configuration for services in *this* file -->
176
176
<defaults autowire =" true" autoconfigure =" true" public =" false" />
177
177
178
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
178
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
179
179
</services >
180
180
</container >
181
181
182
182
.. code-block :: php
183
183
184
184
// config/services.php
185
- use Symfony\Component\DependencyInjection\Definition ;
185
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
186
186
187
- // To use as default template
188
- $definition = new Definition();
187
+ return function(ContainerConfigurator $configurator) {
188
+ $container = $configurator->services()
189
+ ->defaults()
190
+ ->autowire()
191
+ ->autoconfigure()
192
+ ->private();
189
193
190
- $definition
191
- ->setAutowired(true)
192
- ->setAutoconfigured(true)
193
- ->setPublic(false)
194
- ;
195
-
196
- // $this is a reference to the current loader
197
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
194
+ $container->load('App\\', '../src/*')
195
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
196
+ };
198
197
199
198
.. tip ::
200
199
@@ -396,7 +395,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396
395
# same as before
397
396
App\ :
398
397
resource : ' ../src/*'
399
- exclude : ' ../src/{Entity,Migrations,Tests}'
398
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
400
399
401
400
# explicitly configure the service
402
401
App\Updates\SiteUpdateManager :
@@ -416,7 +415,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416
415
<!-- ... -->
417
416
418
417
<!-- Same as before -->
419
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
418
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
420
419
421
420
<!-- Explicitly configure the service -->
422
421
<service id =" App\Updates\SiteUpdateManager" >
@@ -428,23 +427,23 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428
427
.. code-block :: php
429
428
430
429
// config/services.php
430
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
431
+
431
432
use App\Updates\SiteUpdateManager;
432
- use Symfony\Component\DependencyInjection\Definition;
433
433
434
- // Same as before
435
- $definition = new Definition();
434
+ return function(ContainerConfigurator $configurator) {
435
+ $container = $configurator->services()
436
+ ->defaults()
437
+ ->autowire()
438
+ ->autoconfigure()
439
+ ->private();
436
440
437
- $definition
438
- ->setAutowired(true)
439
- ->setAutoconfigured(true)
440
- ->setPublic(false)
441
- ;
441
+ $container->load('App\\', '../src/*')
442
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
442
443
443
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
444
+ $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
445
+ };
444
446
445
- // Explicitly configure the service
446
- $container->getDefinition(SiteUpdateManager::class)
447
- ->setArgument('$adminEmail', 'manager@example.com');
448
447
449
448
Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
450
449
argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
@@ -503,12 +502,19 @@ and reference it with the ``%parameter_name%`` syntax:
503
502
.. code-block :: php
504
503
505
504
// config/services.php
505
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
506
+
506
507
use App\Updates\SiteUpdateManager;
507
- $container->setParameter('admin_email', 'manager@example.com');
508
508
509
- $container->autowire(SiteUpdateManager::class)
510
- // ...
511
- ->setArgument('$adminEmail', '%admin_email%');
509
+ return function(ContainerConfigurator $configurator) {
510
+ $configurator->parameters()->set('admin_email', 'manager@example.com');
511
+
512
+ $container = $configurator->services();
513
+ $container->set(SiteUpdateManager::class)
514
+ ->autowire()
515
+ ->arg('$adminEmail', '%admin_email%');
516
+ };
517
+
512
518
513
519
Actually, once you define a parameter, it can be referenced via the
514
520
``%parameter_name% `` syntax in *any * other configuration file. Many parameters
@@ -612,13 +618,18 @@ But, you can control this and pass in a different logger:
612
618
.. code-block :: php
613
619
614
620
// config/services.php
621
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
622
+
615
623
use App\Service\MessageGenerator;
616
- use Symfony\Component\DependencyInjection\Reference;
617
624
618
- $container->autowire(MessageGenerator::class)
619
- ->setAutoconfigured(true)
620
- ->setPublic(false)
621
- ->setArgument('$logger', new Reference('monolog.logger.request'));
625
+ return function(ContainerConfigurator $configurator) {
626
+ $container = $configurator->services();
627
+ $container->set(SiteUpdateManager::class)
628
+ ->autowire()
629
+ ->autoconfigure()
630
+ ->private();
631
+ ->arg('$logger', ref('monolog.logger.request'));
632
+ };
622
633
623
634
This tells the container that the ``$logger `` argument to ``__construct `` should use
624
635
service whose id is ``monolog.logger.request ``.
@@ -700,21 +711,21 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
700
711
.. code-block :: php
701
712
702
713
// config/services.php
714
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
715
+
703
716
use App\Controller\LuckyController;
704
- use Symfony\Component\DependencyInjection\Reference;
705
717
use Psr\Log\LoggerInterface;
706
718
707
- $container->register(LuckyController::class)
708
- ->setPublic(true)
709
- ->setBindings([
710
- '$adminEmail' => 'manager@example.com',
711
- '$requestLogger' => new Reference('monolog.logger.request'),
712
- LoggerInterface::class => new Reference('monolog.logger.request'),
713
- // optionally you can define both the name and type of the argument to match
714
- 'string $adminEmail' => 'manager@example.com',
715
- LoggerInterface::class.' $requestLogger' => new Reference('monolog.logger.request'),
716
- ])
717
- ;
719
+ return function(ContainerConfigurator $configurator) {
720
+ $container = $configurator->services()->defaults()
721
+ ->bind('$adminEmail', 'manager@example.com')
722
+ ->bind('$requestLogger', ref('monolog.logger.request'))
723
+ ->bind(LoggerInterface::class, ref('monolog.logger.request'))
724
+ ->bind('string $adminEmail', 'manager@example.com')
725
+ ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
726
+
727
+ // ...
728
+ };
718
729
719
730
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
720
731
argument for *any * service defined in this file! You can bind arguments by name
@@ -849,6 +860,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
849
860
</services >
850
861
</container >
851
862
863
+ .. code-block :: php
864
+
865
+ // config/services.php
866
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
867
+
868
+ use App\Service\MessageGenerator;
869
+
870
+ return function(ContainerConfigurator $configurator) {
871
+ // ... same as code before
872
+
873
+ $container->set(MessageGenerator::class)
874
+ ->public();
875
+ };
876
+
852
877
.. _service-psr4-loader :
853
878
854
879
Importing Many Services at once with resource
@@ -869,7 +894,7 @@ key. For example, the default Symfony configuration contains this:
869
894
# this creates a service per class whose id is the fully-qualified class name
870
895
App\ :
871
896
resource : ' ../src/*'
872
- exclude : ' ../src/{Entity,Migrations,Tests}'
897
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
873
898
874
899
.. code-block :: xml
875
900
@@ -883,25 +908,21 @@ key. For example, the default Symfony configuration contains this:
883
908
<services >
884
909
<!-- ... -->
885
910
886
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
911
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
887
912
</services >
888
913
</container >
889
914
890
915
.. code-block :: php
891
916
892
917
// config/services.php
893
- use Symfony\Component\DependencyInjection\Definition;
894
-
895
- // To use as default template
896
- $definition = new Definition();
918
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
897
919
898
- $definition
899
- ->setAutowired(true)
900
- ->setAutoconfigured(true)
901
- ->setPublic(false)
902
- ;
920
+ return function(ContainerConfigurator $configurator) {
921
+ // ...
903
922
904
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
923
+ $container->load('App\\', '../src/*')
924
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
925
+ };
905
926
906
927
.. tip ::
907
928
@@ -1038,27 +1059,31 @@ admin email. In this case, each needs to have a unique service id:
1038
1059
.. code-block :: php
1039
1060
1040
1061
// config/services.php
1062
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1063
+
1041
1064
use App\Updates\SiteUpdateManager;
1042
1065
use App\Service\MessageGenerator;
1043
- use Symfony\Component\DependencyInjection\Reference;
1044
-
1045
- $container->register('site_update_manager.superadmin', SiteUpdateManager::class)
1046
- ->setAutowired(false)
1047
- ->setArguments([
1048
- new Reference(MessageGenerator::class),
1049
- new Reference('mailer'),
1050
- 'superadmin@example.com'
1051
- ]);
1052
-
1053
- $container->register('site_update_manager.normal_users', SiteUpdateManager::class)
1054
- ->setAutowired(false)
1055
- ->setArguments([
1056
- new Reference(MessageGenerator::class),
1057
- new Reference('mailer'),
1058
- 'contact@example.com'
1059
- ]);
1060
-
1061
- $container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1066
+
1067
+ return function(ContainerConfigurator $configurator) {
1068
+ // ...
1069
+
1070
+ $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1071
+ ->autowire(false)
1072
+ ->args([
1073
+ ref(MessageGenerator::class),
1074
+ ref('mailer'),
1075
+ 'superadmin@example.com'
1076
+ ]);
1077
+ $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1078
+ ->autowire(false)
1079
+ ->args([
1080
+ ref(MessageGenerator::class),
1081
+ ref('mailer'),
1082
+ 'contact@example.com'
1083
+ ]);
1084
+ $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1085
+ };
1086
+
1062
1087
1063
1088
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1064
1089
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
0 commit comments