@@ -185,13 +185,16 @@ each time you ask for it.
185185 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
186186
187187 return function(ContainerConfigurator $configurator) {
188- $container = $configurator->services()
188+ // default configuration for services in *this* file
189+ $services = $configurator->services()
189190 ->defaults()
190- ->autowire()
191- ->autoconfigure()
192- ->private();
193-
194- $container->load('App\\', '../src/*')
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/*')
195198 ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
196199 };
197200
@@ -416,7 +419,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416419
417420 <!-- Same as before -->
418421
419- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
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" >
@@ -433,16 +436,15 @@ pass here. No problem! In your configuration, you can explicitly set this argume
433436 use App\Updates\SiteUpdateManager;
434437
435438 return function(ContainerConfigurator $configurator) {
436- $container = $configurator->services()
437- ->defaults()
438- ->autowire()
439- ->autoconfigure()
440- ->private();
439+ // ...
441440
442- $container->load('App\\', '../src/*')
443- ->exclude('../src/{Entity,Migrations,Tests}');
441+ // same as before
442+ $services->load('App\\', '../src/*')
443+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
444444
445- $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
445+ $services->set(SiteUpdateManager::class)
446+ ->arg('$adminEmail', 'manager@example.com')
447+ ;
446448 };
447449
448450
@@ -508,10 +510,11 @@ parameter and in PHP config use the ``Reference`` class:
508510 use App\Service\MessageGenerator;
509511
510512 return function(ContainerConfigurator $configurator) {
511- $container = $configurator->services();
512- $container->set(MessageGenerator::class)
513- ->autoconfigure()
514- ->args([ref('logger')]]);
513+ $services = $configurator->services();
514+
515+ $services->set(MessageGenerator::class)
516+ ->args([ref('logger')])
517+ ;
515518 };
516519
517520 Working with container parameters is straightforward using the container's
@@ -613,12 +616,12 @@ But, you can control this and pass in a different logger:
613616 use App\Service\MessageGenerator;
614617
615618 return function(ContainerConfigurator $configurator) {
616- $container = $configurator->services();
617- $container->set(SiteUpdateManager::class)
618- ->autowire()
619- ->autoconfigure( )
620- ->private();
621- ->arg('$logger', ref('monolog.logger.request')) ;
619+ // ... same code as before
620+
621+ // explicitly configure the service
622+ $services->set(SiteUpdateManager::class )
623+ ->arg('$logger', ref('monolog.logger.request'))
624+ ;
622625 };
623626
624627 This tells the container that the ``$logger `` argument to ``__construct `` should use
@@ -708,12 +711,24 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
708711 use Symfony\Component\DependencyInjection\Reference;
709712
710713 return function(ContainerConfigurator $configurator) {
711- $container = $configurator->services()->defaults()
712- ->bind('$adminEmail', 'manager@example.com')
713- ->bind('$requestLogger', ref('monolog.logger.request'))
714- ->bind(LoggerInterface::class, ref('monolog.logger.request'))
715- ->bind('string $adminEmail', 'manager@example.com')
716- ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
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+ ;
717732
718733 // ...
719734 };
@@ -828,8 +843,10 @@ But, if you *do* need to make a service public, override the ``public`` setting:
828843 return function(ContainerConfigurator $configurator) {
829844 // ... same as code before
830845
831- $container->set(MessageGenerator::class)
832- ->public();
846+ // explicitly configure the service
847+ $services->set(MessageGenerator::class)
848+ ->public()
849+ ;
833850 };
834851
835852 .. _service-psr4-loader :
@@ -866,7 +883,7 @@ key. For example, the default Symfony configuration contains this:
866883 <services >
867884 <!-- ... -->
868885
869- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
886+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
870887 </services >
871888 </container >
872889
@@ -878,7 +895,9 @@ key. For example, the default Symfony configuration contains this:
878895 return function(ContainerConfigurator $configurator) {
879896 // ...
880897
881- $container->load('App\\', '../src/*')
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/*')
882901 ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
883902 };
884903
@@ -1025,21 +1044,28 @@ admin email. In this case, each needs to have a unique service id:
10251044 return function(ContainerConfigurator $configurator) {
10261045 // ...
10271046
1028- $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
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
10291050 ->autowire(false)
1051+ // manually wire all arguments
10301052 ->args([
10311053 ref(MessageGenerator::class),
10321054 ref('mailer'),
1033- 'superadmin@example.com'
1055+ 'superadmin@example.com',
10341056 ]);
1035- $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1057+
1058+ $services->set('site_update_manager.normal_users', SiteUpdateManager::class)
10361059 ->autowire(false)
10371060 ->args([
10381061 ref(MessageGenerator::class),
10391062 ref('mailer'),
1040- 'contact@example.com'
1063+ 'contact@example.com',
10411064 ]);
1042- $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
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');
10431069 };
10441070
10451071 In this case, *two * services are registered: ``site_update_manager.superadmin ``
0 commit comments