@@ -154,7 +154,7 @@ each time you ask for it.
154
154
# this creates a service per class whose id is the fully-qualified class name
155
155
App\ :
156
156
resource : ' ../src/*'
157
- exclude : ' ../src/{Entity,Migrations,Tests,Kernel.php}'
157
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php}'
158
158
159
159
# ...
160
160
@@ -171,7 +171,7 @@ each time you ask for it.
171
171
<!-- Default configuration for services in *this* file -->
172
172
<defaults autowire =" true" autoconfigure =" true" public =" false" />
173
173
174
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
174
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
175
175
</services >
176
176
</container >
177
177
@@ -190,7 +190,23 @@ each time you ask for it.
190
190
;
191
191
192
192
// $this is a reference to the current loader
193
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
193
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
194
+
195
+ .. code-block :: php-fluent-di
196
+
197
+ // config/services.php
198
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
199
+
200
+ return function(ContainerConfigurator $configurator) {
201
+ $container = $configurator->services()
202
+ ->defaults()
203
+ ->autowire()
204
+ ->autoconfigure()
205
+ ->private();
206
+
207
+ $container->load('App\\', '../src/*')
208
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
209
+ };
194
210
195
211
.. tip ::
196
212
@@ -393,7 +409,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
393
409
# same as before
394
410
App\ :
395
411
resource : ' ../src/*'
396
- exclude : ' ../src/{Entity,Migrations,Tests}'
412
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
397
413
398
414
# explicitly configure the service
399
415
App\Updates\SiteUpdateManager :
@@ -413,7 +429,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
413
429
<!-- ... -->
414
430
415
431
<!-- Same as before -->
416
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
432
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
417
433
418
434
<!-- Explicitly configure the service -->
419
435
<service id =" App\Updates\SiteUpdateManager" >
@@ -437,12 +453,32 @@ pass here. No problem! In your configuration, you can explicitly set this argume
437
453
->setPublic(false)
438
454
;
439
455
440
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
456
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }');
441
457
442
458
// Explicitly configure the service
443
459
$container->getDefinition(SiteUpdateManager::class)
444
460
->setArgument('$adminEmail', 'manager@example.com');
445
461
462
+ .. code-block :: php-fluent-di
463
+
464
+ // config/services.php
465
+ use App\Updates\SiteUpdateManager;
466
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
467
+
468
+ return function(ContainerConfigurator $configurator) {
469
+ $container = $configurator->services()
470
+ ->defaults()
471
+ ->autowire()
472
+ ->autoconfigure()
473
+ ->private();
474
+
475
+ $container->load('App\\', '../src/*')
476
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
477
+
478
+ $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
479
+ };
480
+
481
+
446
482
Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
447
483
argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
448
484
other arguments will still be autowired.
@@ -507,6 +543,22 @@ and reference it with the ``%parameter_name%`` syntax:
507
543
// ...
508
544
->setArgument('$adminEmail', '%admin_email%');
509
545
546
+ .. code-block :: php-fluent-di
547
+
548
+ // config/services.php
549
+ use App\Updates\SiteUpdateManager;
550
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
551
+
552
+ return function(ContainerConfigurator $configurator) {
553
+ $configurator->parameters()->set('admin_email', 'manager@example.com');
554
+
555
+ $container = $configurator->services();
556
+ $container->set(SiteUpdateManager::class)
557
+ ->autowire()
558
+ ->arg('$adminEmail', '%admin_email%');
559
+ };
560
+
561
+
510
562
Actually, once you define a parameter, it can be referenced via the
511
563
``%parameter_name% `` syntax in *any * other configuration file. Many parameters
512
564
are defined in the ``config/services.yaml `` file.
@@ -617,6 +669,22 @@ But, you can control this and pass in a different logger:
617
669
->setPublic(false)
618
670
->setArgument('$logger', new Reference('monolog.logger.request'));
619
671
672
+ .. code-block :: php-fluent-di
673
+
674
+ // config/services.php
675
+ use App\Service\MessageGenerator;
676
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
677
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
678
+
679
+ return function(ContainerConfigurator $configurator) {
680
+ $container = $configurator->services();
681
+ $container->set(SiteUpdateManager::class)
682
+ ->autowire()
683
+ ->autoconfigure()
684
+ ->private();
685
+ ->arg('$logger', ref('monolog.logger.request'));
686
+ };
687
+
620
688
This tells the container that the ``$logger `` argument to ``__construct `` should use
621
689
service whose id is ``monolog.logger.request ``.
622
690
@@ -713,6 +781,25 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
713
781
))
714
782
;
715
783
784
+ .. code-block :: php-fluent-di
785
+
786
+ // config/services.php
787
+ use App\Controller\LuckyController;
788
+ use Psr\Log\LoggerInterface;
789
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
790
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
791
+
792
+ return function(ContainerConfigurator $configurator) {
793
+ $container = $configurator->services()->defaults()
794
+ ->bind('$adminEmail', 'manager@example.com')
795
+ ->bind('$requestLogger', ref('monolog.logger.request'))
796
+ ->bind(LoggerInterface::class, ref('monolog.logger.request'))
797
+ ->bind('string $adminEmail', 'manager@example.com')
798
+ ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
799
+
800
+ // ...
801
+ };
802
+
716
803
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
717
804
argument for *any * service defined in this file! You can bind arguments by name
718
805
(e.g. ``$adminEmail ``), by type (e.g. ``Psr\Log\LoggerInterface ``) or both
@@ -852,6 +939,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
852
939
</services >
853
940
</container >
854
941
942
+ .. code-block :: php-fuent-di
943
+
944
+ // config/services.php
945
+
946
+ use App\Service\MessageGenerator;
947
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
948
+
949
+ return function(ContainerConfigurator $configurator) {
950
+ // ... same as code before
951
+
952
+ $container->set(MessageGenerator::class)
953
+ ->public();
954
+ };
955
+
855
956
.. _service-psr4-loader :
856
957
857
958
Importing Many Services at once with resource
@@ -872,7 +973,7 @@ key. For example, the default Symfony configuration contains this:
872
973
# this creates a service per class whose id is the fully-qualified class name
873
974
App\ :
874
975
resource : ' ../src/*'
875
- exclude : ' ../src/{Entity,Migrations,Tests}'
976
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
876
977
877
978
.. code-block :: xml
878
979
@@ -886,7 +987,7 @@ key. For example, the default Symfony configuration contains this:
886
987
<services >
887
988
<!-- ... -->
888
989
889
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
990
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
890
991
</services >
891
992
</container >
892
993
@@ -904,7 +1005,19 @@ key. For example, the default Symfony configuration contains this:
904
1005
->setPublic(false)
905
1006
;
906
1007
907
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
1008
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1009
+
1010
+ .. code-block :: php-fluent-di
1011
+
1012
+ // config/services.php
1013
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1014
+
1015
+ return function(ContainerConfigurator $configurator) {
1016
+ // ...
1017
+
1018
+ $container->load('App\\', '../src/*')
1019
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1020
+ };
908
1021
909
1022
.. tip ::
910
1023
@@ -1063,6 +1176,35 @@ admin email. In this case, each needs to have a unique service id:
1063
1176
1064
1177
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1065
1178
1179
+ .. code-block :: php-fluent-di
1180
+
1181
+ // config/services.php
1182
+ use App\Updates\SiteUpdateManager;
1183
+ use App\Service\MessageGenerator;
1184
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1185
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
1186
+
1187
+ return function(ContainerConfigurator $configurator) {
1188
+ // ...
1189
+
1190
+ $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1191
+ ->autowire(false)
1192
+ ->args([
1193
+ ref(MessageGenerator::class),
1194
+ ref('mailer'),
1195
+ 'superadmin@example.com'
1196
+ ]);
1197
+ $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1198
+ ->autowire(false)
1199
+ ->args([
1200
+ ref(MessageGenerator::class),
1201
+ ref('mailer'),
1202
+ 'contact@example.com'
1203
+ ]);
1204
+ $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1205
+ };
1206
+
1207
+
1066
1208
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1067
1209
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
1068
1210
``SiteUpdateManager `` the first (``site_update_manager.superadmin ``) will be passed.
0 commit comments