@@ -182,19 +182,18 @@ each time you ask for it.
182182 ..code-block ::php
183183
184184 // config/services.php
185- use Symfony\Component\DependencyInjection\Definition ;
185+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
186186
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();
189193
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}');
194+ $container->load('App\\', '../src/*')
195+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
196+ };
198197
199198 ..tip ::
200199
@@ -396,7 +395,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396395# same as before
397396App\ :
398397resource :' ../src/*'
399- exclude :' ../src/{Entity,Migrations,Tests}'
398+ exclude :' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
400399
401400# explicitly configure the service
402401App\Updates\SiteUpdateManager :
@@ -416,6 +415,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416415<!-- ...-->
417416
418417<!-- Same as before-->
418+
419419 <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
420420
421421<!-- Explicitly configure the service-->
@@ -428,23 +428,23 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428428 ..code-block ::php
429429
430430 // config/services.php
431+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
432+
431433 use App\Updates\SiteUpdateManager;
432- use Symfony\Component\DependencyInjection\Definition;
433434
434- // Same as before
435- $definition = new Definition();
435+ return function(ContainerConfigurator $configurator) {
436+ $container = $configurator->services()
437+ ->defaults()
438+ ->autowire()
439+ ->autoconfigure()
440+ ->private();
436441
437- $definition
438- ->setAutowired(true)
439- ->setAutoconfigured(true)
440- ->setPublic(false)
441- ;
442+ $container->load('App\\', '../src/*')
443+ ->exclude('../src/{Entity,Migrations,Tests}');
442444
443- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
445+ $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
446+ };
444447
445- // Explicitly configure the service
446- $container->getDefinition(SiteUpdateManager::class)
447- ->setArgument('$adminEmail', 'manager@example.com');
448448
449449 Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
450450argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
@@ -503,13 +503,16 @@ parameter and in PHP config use the ``Reference`` class:
503503 ..code-block ::php
504504
505505 // config/services.php
506+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
507+
506508 use App\Service\MessageGenerator;
507- use Symfony\Component\DependencyInjection\Reference;
508509
509- $container->autowire(MessageGenerator::class)
510- ->setAutoconfigured(true)
511- ->setPublic(false)
512- ->setArgument(0, new Reference('logger'));
510+ return function(ContainerConfigurator $configurator) {
511+ $container = $configurator->services();
512+ $container->set(MessageGenerator::class)
513+ ->autoconfigure()
514+ ->args([ref('logger')]]);
515+ };
513516
514517 Working with container parameters is straightforward using the container's
515518accessor methods for parameters::
@@ -605,13 +608,18 @@ But, you can control this and pass in a different logger:
605608 ..code-block ::php
606609
607610 // config/services.php
611+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
612+
608613 use App\Service\MessageGenerator;
609- use Symfony\Component\DependencyInjection\Reference;
610614
611- $container->autowire(MessageGenerator::class)
612- ->setAutoconfigured(true)
613- ->setPublic(false)
614- ->setArgument('$logger', new Reference('monolog.logger.request'));
615+ 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'));
622+ };
615623
616624 This tells the container that the ``$logger `` argument to ``__construct `` should use
617625service whose id is ``monolog.logger.request ``.
@@ -693,21 +701,22 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
693701 ..code-block ::php
694702
695703 // config/services.php
704+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
705+
696706 use App\Controller\LuckyController;
697707 use Psr\Log\LoggerInterface;
698708 use Symfony\Component\DependencyInjection\Reference;
699709
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- ;
710+ 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'));
717+
718+ // ...
719+ };
711720
712721 By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
713722argument for *any * service defined in this file! You can bind arguments by name
@@ -809,6 +818,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
809818 </services >
810819 </container >
811820
821+ ..code-block ::php
822+
823+ // config/services.php
824+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
825+
826+ use App\Service\MessageGenerator;
827+
828+ return function(ContainerConfigurator $configurator) {
829+ // ... same as code before
830+
831+ $container->set(MessageGenerator::class)
832+ ->public();
833+ };
834+
812835 .. _service-psr4-loader :
813836
814837Importing Many Services at once with resource
@@ -829,7 +852,7 @@ key. For example, the default Symfony configuration contains this:
829852# this creates a service per class whose id is the fully-qualified class name
830853App\ :
831854resource :' ../src/*'
832- exclude :' ../src/{Entity,Migrations,Tests}'
855+ exclude :' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
833856
834857 ..code-block ::xml
835858
@@ -850,18 +873,14 @@ key. For example, the default Symfony configuration contains this:
850873 ..code-block ::php
851874
852875 // config/services.php
853- use Symfony\Component\DependencyInjection\Definition ;
876+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
854877
855- // To use as default template
856- $definition = new Definition();
857-
858- $definition
859- ->setAutowired(true)
860- ->setAutoconfigured(true)
861- ->setPublic(false)
862- ;
878+ return function(ContainerConfigurator $configurator) {
879+ // ...
863880
864- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
881+ $container->load('App\\', '../src/*')
882+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
883+ };
865884
866885 ..tip ::
867886
@@ -998,27 +1017,30 @@ admin email. In this case, each needs to have a unique service id:
9981017 ..code-block ::php
9991018
10001019 // config/services.php
1020+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1021+
10011022 use App\Service\MessageGenerator;
10021023 use App\Updates\SiteUpdateManager;
1003- use Symfony\Component\DependencyInjection\Reference;
10041024
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')
1025+ return function(ContainerConfigurator $configurator) {
1026+ // ...
1027+
1028+ $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1029+ ->autowire(false)
1030+ ->args([
1031+ ref(MessageGenerator::class),
1032+ ref('mailer'),
1033+ 'superadmin@example.com'
1034+ ]);
1035+ $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1036+ ->autowire(false)
1037+ ->args([
1038+ ref(MessageGenerator::class),
1039+ ref('mailer'),
1040+ 'contact@example.com'
1041+ ]);
1042+ $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1043+ };
10221044
10231045 In this case, *two * services are registered: ``site_update_manager.superadmin ``
10241046and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint