@@ -182,19 +182,18 @@ each time you ask for it.
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/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
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,6 +415,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416
415
<!-- ... -->
417
416
418
417
<!-- Same as before -->
418
+
419
419
<prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
420
420
421
421
<!-- Explicitly configure the service -->
@@ -428,23 +428,23 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428
428
.. code-block :: php
429
429
430
430
// config/services.php
431
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
432
+
431
433
use App\Updates\SiteUpdateManager;
432
- use Symfony\Component\DependencyInjection\Definition;
433
434
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();
436
441
437
- $definition
438
- ->setAutowired(true)
439
- ->setAutoconfigured(true)
440
- ->setPublic(false)
441
- ;
442
+ $container->load('App\\', '../src/*')
443
+ ->exclude('../src/{Entity,Migrations,Tests}');
442
444
443
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
445
+ $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
446
+ };
444
447
445
- // Explicitly configure the service
446
- $container->getDefinition(SiteUpdateManager::class)
447
- ->setArgument('$adminEmail', 'manager@example.com');
448
448
449
449
Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
450
450
argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
@@ -503,13 +503,16 @@ parameter and in PHP config use the ``Reference`` class:
503
503
.. code-block :: php
504
504
505
505
// config/services.php
506
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
507
+
506
508
use App\Service\MessageGenerator;
507
- use Symfony\Component\DependencyInjection\Reference;
508
509
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
+ };
513
516
514
517
Working with container parameters is straightforward using the container's
515
518
accessor methods for parameters::
@@ -605,13 +608,18 @@ But, you can control this and pass in a different logger:
605
608
.. code-block :: php
606
609
607
610
// config/services.php
611
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
612
+
608
613
use App\Service\MessageGenerator;
609
- use Symfony\Component\DependencyInjection\Reference;
610
614
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
+ };
615
623
616
624
This tells the container that the ``$logger `` argument to ``__construct `` should use
617
625
service 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
693
701
.. code-block :: php
694
702
695
703
// config/services.php
704
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
705
+
696
706
use App\Controller\LuckyController;
697
707
use Psr\Log\LoggerInterface;
698
708
use Symfony\Component\DependencyInjection\Reference;
699
709
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
+ };
711
720
712
721
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
713
722
argument 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:
809
818
</services >
810
819
</container >
811
820
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
+
812
835
.. _service-psr4-loader :
813
836
814
837
Importing Many Services at once with resource
@@ -829,7 +852,7 @@ key. For example, the default Symfony configuration contains this:
829
852
# this creates a service per class whose id is the fully-qualified class name
830
853
App\ :
831
854
resource : ' ../src/*'
832
- exclude : ' ../src/{Entity,Migrations,Tests}'
855
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
833
856
834
857
.. code-block :: xml
835
858
@@ -850,18 +873,14 @@ key. For example, the default Symfony configuration contains this:
850
873
.. code-block :: php
851
874
852
875
// config/services.php
853
- use Symfony\Component\DependencyInjection\Definition ;
876
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
854
877
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
+ // ...
863
880
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
+ };
865
884
866
885
.. tip ::
867
886
@@ -998,27 +1017,30 @@ admin email. In this case, each needs to have a unique service id:
998
1017
.. code-block :: php
999
1018
1000
1019
// config/services.php
1020
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1021
+
1001
1022
use App\Service\MessageGenerator;
1002
1023
use App\Updates\SiteUpdateManager;
1003
- use Symfony\Component\DependencyInjection\Reference;
1004
1024
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
+ };
1022
1044
1023
1045
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1024
1046
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
0 commit comments