@@ -182,19 +182,21 @@ each time you ask for it.
182
182
.. code-block :: php
183
183
184
184
// config/services.php
185
- use Symfony\Component\DependencyInjection\Definition;
186
-
187
- // To use as default template
188
- $definition = new Definition();
189
-
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/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
185
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
186
+
187
+ return function(ContainerConfigurator $configurator) {
188
+ // default configuration for services in *this* file
189
+ $services = $configurator->services()
190
+ ->defaults()
191
+ ->autowire() // Automatically injects dependencies in your services.
192
+ ->autoconfigure() // Automatically registers your services as commands, event subscribers, etc.
193
+ ;
194
+
195
+ // makes classes in src/ available to be used as services
196
+ // this creates a service per class whose id is the fully-qualified class name
197
+ $services->load('App\\', '../src/*')
198
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
199
+ };
198
200
199
201
.. tip ::
200
202
@@ -396,7 +398,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396
398
# same as before
397
399
App\ :
398
400
resource : ' ../src/*'
399
- exclude : ' ../src/{Entity,Migrations,Tests}'
401
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
400
402
401
403
# explicitly configure the service
402
404
App\Updates\SiteUpdateManager :
@@ -416,7 +418,8 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416
418
<!-- ... -->
417
419
418
420
<!-- Same as before -->
419
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
421
+
422
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
420
423
421
424
<!-- Explicitly configure the service -->
422
425
<service id =" App\Updates\SiteUpdateManager" >
@@ -428,23 +431,22 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428
431
.. code-block :: php
429
432
430
433
// config/services.php
434
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
435
+
431
436
use App\Updates\SiteUpdateManager;
432
- use Symfony\Component\DependencyInjection\Definition;
433
437
434
- // Same as before
435
- $definition = new Definition();
438
+ return function(ContainerConfigurator $configurator) {
439
+ // ...
436
440
437
- $definition
438
- ->setAutowired(true)
439
- ->setAutoconfigured(true)
440
- ->setPublic(false)
441
- ;
441
+ // same as before
442
+ $services->load('App\\', '../src/*')
443
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
442
444
443
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
445
+ $services->set(SiteUpdateManager::class)
446
+ ->arg('$adminEmail', '[email protected] ')
447
+ ;
448
+ };
444
449
445
- // Explicitly configure the service
446
- $container->getDefinition(SiteUpdateManager::class)
447
- ->setArgument('$adminEmail', '[email protected] ');
448
450
449
451
Thanks to this, the container will pass ``
[email protected] `` to the ``
$adminEmail ``
450
452
argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
@@ -503,13 +505,17 @@ parameter and in PHP config use the ``Reference`` class:
503
505
.. code-block :: php
504
506
505
507
// config/services.php
508
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
509
+
506
510
use App\Service\MessageGenerator;
507
- use Symfony\Component\DependencyInjection\Reference;
508
511
509
- $container->autowire(MessageGenerator::class)
510
- ->setAutoconfigured(true)
511
- ->setPublic(false)
512
- ->setArgument(0, new Reference('logger'));
512
+ return function(ContainerConfigurator $configurator) {
513
+ $services = $configurator->services();
514
+
515
+ $services->set(MessageGenerator::class)
516
+ ->args([ref('logger')])
517
+ ;
518
+ };
513
519
514
520
Working with container parameters is straightforward using the container's
515
521
accessor methods for parameters::
@@ -605,13 +611,18 @@ But, you can control this and pass in a different logger:
605
611
.. code-block :: php
606
612
607
613
// config/services.php
614
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
615
+
608
616
use App\Service\MessageGenerator;
609
- use Symfony\Component\DependencyInjection\Reference;
610
617
611
- $container->autowire(MessageGenerator::class)
612
- ->setAutoconfigured(true)
613
- ->setPublic(false)
614
- ->setArgument('$logger', new Reference('monolog.logger.request'));
618
+ return function(ContainerConfigurator $configurator) {
619
+ // ... same code as before
620
+
621
+ // explicitly configure the service
622
+ $services->set(SiteUpdateManager::class)
623
+ ->arg('$logger', ref('monolog.logger.request'))
624
+ ;
625
+ };
615
626
616
627
This tells the container that the ``$logger `` argument to ``__construct `` should use
617
628
service whose id is ``monolog.logger.request ``.
@@ -693,21 +704,34 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
693
704
.. code-block :: php
694
705
695
706
// config/services.php
707
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
708
+
696
709
use App\Controller\LuckyController;
697
710
use Psr\Log\LoggerInterface;
698
711
use Symfony\Component\DependencyInjection\Reference;
699
712
700
- $container->register(LuckyController::class)
701
- ->setPublic(true)
702
- ->setBindings([
703
- '$adminEmail' => '[email protected] ',
704
- '$requestLogger' => new Reference('monolog.logger.request'),
705
- LoggerInterface::class => new Reference('monolog.logger.request'),
706
- // optionally you can define both the name and type of the argument to match
707
- 'string $adminEmail' => '[email protected] ',
708
- LoggerInterface::class.' $requestLogger' => new Reference('monolog.logger.request'),
709
- ])
710
- ;
713
+ return function(ContainerConfigurator $configurator) {
714
+ $services = $configurator->services()
715
+ ->defaults()
716
+ // pass this value to any $adminEmail argument for any service
717
+ // that's defined in this file (including controller arguments)
718
+ ->bind('$adminEmail', '[email protected] ')
719
+
720
+ // pass this service to any $requestLogger argument for any
721
+ // service that's defined in this file
722
+ ->bind('$requestLogger', ref('monolog.logger.request'))
723
+
724
+ // pass this service for any LoggerInterface type-hint for any
725
+ // service that's defined in this file
726
+ ->bind(LoggerInterface::class, ref('monolog.logger.request'))
727
+
728
+ // optionally you can define both the name and type of the argument to match
729
+ ->bind('string $adminEmail', '[email protected] ')
730
+ ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'))
731
+ ;
732
+
733
+ // ...
734
+ };
711
735
712
736
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
713
737
argument for *any * service defined in this file! You can bind arguments by name
@@ -809,6 +833,22 @@ But, if you *do* need to make a service public, override the ``public`` setting:
809
833
</services >
810
834
</container >
811
835
836
+ .. code-block :: php
837
+
838
+ // config/services.php
839
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
840
+
841
+ use App\Service\MessageGenerator;
842
+
843
+ return function(ContainerConfigurator $configurator) {
844
+ // ... same as code before
845
+
846
+ // explicitly configure the service
847
+ $services->set(MessageGenerator::class)
848
+ ->public()
849
+ ;
850
+ };
851
+
812
852
.. _service-psr4-loader :
813
853
814
854
Importing Many Services at once with resource
@@ -829,7 +869,7 @@ key. For example, the default Symfony configuration contains this:
829
869
# this creates a service per class whose id is the fully-qualified class name
830
870
App\ :
831
871
resource : ' ../src/*'
832
- exclude : ' ../src/{Entity,Migrations,Tests}'
872
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
833
873
834
874
.. code-block :: xml
835
875
@@ -843,25 +883,23 @@ key. For example, the default Symfony configuration contains this:
843
883
<services >
844
884
<!-- ... -->
845
885
846
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
886
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
847
887
</services >
848
888
</container >
849
889
850
890
.. code-block :: php
851
891
852
892
// config/services.php
853
- use Symfony\Component\DependencyInjection\Definition ;
893
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
854
894
855
- // To use as default template
856
- $definition = new Definition();
857
-
858
- $definition
859
- ->setAutowired(true)
860
- ->setAutoconfigured(true)
861
- ->setPublic(false)
862
- ;
895
+ return function(ContainerConfigurator $configurator) {
896
+ // ...
863
897
864
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
898
+ // makes classes in src/ available to be used as services
899
+ // this creates a service per class whose id is the fully-qualified class name
900
+ $services->load('App\\', '../src/*')
901
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
902
+ };
865
903
866
904
.. tip ::
867
905
@@ -998,27 +1036,37 @@ admin email. In this case, each needs to have a unique service id:
998
1036
.. code-block :: php
999
1037
1000
1038
// config/services.php
1039
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1040
+
1001
1041
use App\Service\MessageGenerator;
1002
1042
use App\Updates\SiteUpdateManager;
1003
- use Symfony\Component\DependencyInjection\Reference;
1004
1043
1005
- $container->register('site_update_manager.superadmin', SiteUpdateManager::class)
1006
- ->setAutowired(false)
1007
- ->setArguments([
1008
- new Reference(MessageGenerator::class),
1009
- new Reference('mailer'),
1010
-
1011
- ]);
1012
-
1013
- $container->register('site_update_manager.normal_users', SiteUpdateManager::class)
1014
- ->setAutowired(false)
1015
- ->setArguments([
1016
- new Reference(MessageGenerator::class),
1017
- new Reference('mailer'),
1018
-
1019
- ]);
1020
-
1021
- $container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1044
+ return function(ContainerConfigurator $configurator) {
1045
+ // ...
1046
+
1047
+ // site_update_manager.superadmin is the service's id
1048
+ $services->set('site_update_manager.superadmin', SiteUpdateManager::class)
1049
+ // you CAN still use autowiring: we just want to show what it looks like without
1050
+ ->autowire(false)
1051
+ // manually wire all arguments
1052
+ ->args([
1053
+ ref(MessageGenerator::class),
1054
+ ref('mailer'),
1055
+
1056
+ ]);
1057
+
1058
+ $services->set('site_update_manager.normal_users', SiteUpdateManager::class)
1059
+ ->autowire(false)
1060
+ ->args([
1061
+ ref(MessageGenerator::class),
1062
+ ref('mailer'),
1063
+
1064
+ ]);
1065
+
1066
+ // Create an alias, so that - by default - if you type-hint SiteUpdateManager,
1067
+ // the site_update_manager.superadmin will be used
1068
+ $services->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1069
+ };
1022
1070
1023
1071
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1024
1072
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
0 commit comments