@@ -182,19 +182,21 @@ each time you ask for it.
182182 .. code-block :: php
183183
184184 // 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+ };
198200
199201 .. tip ::
200202
@@ -396,7 +398,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396398 # same as before
397399 App\ :
398400 resource : ' ../src/*'
399- exclude : ' ../src/{Entity,Migrations,Tests}'
401+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
400402
401403 # explicitly configure the service
402404 App\Updates\SiteUpdateManager :
@@ -416,7 +418,8 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416418 <!-- ... -->
417419
418420 <!-- 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}" />
420423
421424 <!-- Explicitly configure the service -->
422425 <service id =" App\Updates\SiteUpdateManager" >
@@ -428,23 +431,22 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428431 .. code-block :: php
429432
430433 // config/services.php
434+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
435+
431436 use App\Updates\SiteUpdateManager;
432- use Symfony\Component\DependencyInjection\Definition;
433437
434- // Same as before
435- $definition = new Definition();
438+ return function(ContainerConfigurator $configurator) {
439+ // ...
436440
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}');
442444
443- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
445+ $services->set(SiteUpdateManager::class)
446+ ->arg('$adminEmail', 'manager@example.com')
447+ ;
448+ };
444449
445- // Explicitly configure the service
446- $container->getDefinition(SiteUpdateManager::class)
447- ->setArgument('$adminEmail', 'manager@example.com');
448450
449451 Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
450452argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
@@ -503,13 +505,17 @@ parameter and in PHP config use the ``Reference`` class:
503505 .. code-block :: php
504506
505507 // config/services.php
508+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
509+
506510 use App\Service\MessageGenerator;
507- use Symfony\Component\DependencyInjection\Reference;
508511
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+ };
513519
514520 Working with container parameters is straightforward using the container's
515521accessor methods for parameters::
@@ -605,13 +611,18 @@ But, you can control this and pass in a different logger:
605611 .. code-block :: php
606612
607613 // config/services.php
614+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
615+
608616 use App\Service\MessageGenerator;
609- use Symfony\Component\DependencyInjection\Reference;
610617
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+ };
615626
616627 This tells the container that the ``$logger `` argument to ``__construct `` should use
617628service 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
693704 .. code-block :: php
694705
695706 // config/services.php
707+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
708+
696709 use App\Controller\LuckyController;
697710 use Psr\Log\LoggerInterface;
698711 use Symfony\Component\DependencyInjection\Reference;
699712
700- $container->register(LuckyController::class)
701- ->setPublic(true)
702- ->setBindings([
703- '$adminEmail' => 'manager@example.com',
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' => 'manager@example.com',
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', 'manager@example.com')
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', 'manager@example.com')
730+ ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'))
731+ ;
732+
733+ // ...
734+ };
711735
712736 By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
713737argument 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:
809833 </services >
810834 </container >
811835
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+
812852 .. _service-psr4-loader :
813853
814854Importing Many Services at once with resource
@@ -829,7 +869,7 @@ key. For example, the default Symfony configuration contains this:
829869 # this creates a service per class whose id is the fully-qualified class name
830870 App\ :
831871 resource : ' ../src/*'
832- exclude : ' ../src/{Entity,Migrations,Tests}'
872+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
833873
834874 .. code-block :: xml
835875
@@ -843,25 +883,23 @@ key. For example, the default Symfony configuration contains this:
843883 <services >
844884 <!-- ... -->
845885
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 }" />
847887 </services >
848888 </container >
849889
850890 .. code-block :: php
851891
852892 // config/services.php
853- use Symfony\Component\DependencyInjection\Definition ;
893+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
854894
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+ // ...
863897
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+ };
865903
866904 .. tip ::
867905
@@ -998,27 +1036,37 @@ admin email. In this case, each needs to have a unique service id:
9981036 .. code-block :: php
9991037
10001038 // config/services.php
1039+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1040+
10011041 use App\Service\MessageGenerator;
10021042 use App\Updates\SiteUpdateManager;
1003- use Symfony\Component\DependencyInjection\Reference;
10041043
1005- $container->register('site_update_manager.superadmin', SiteUpdateManager::class)
1006- ->setAutowired(false)
1007- ->setArguments([
1008- new Reference(MessageGenerator::class),
1009- new Reference('mailer'),
1010- 'superadmin@example.com'
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- 'contact@example.com'
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+ 'superadmin@example.com',
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+ 'contact@example.com',
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+ };
10221070
10231071 In this case, *two * services are registered: ``site_update_manager.superadmin ``
10241072and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
0 commit comments