@@ -158,7 +158,7 @@ each time you ask for it.
158
158
# this creates a service per class whose id is the fully-qualified class name
159
159
App\ :
160
160
resource : ' ../src/*'
161
- exclude : ' ../src/{Entity,Migrations,Tests,Kernel.php}'
161
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php}'
162
162
163
163
# ...
164
164
@@ -175,7 +175,7 @@ each time you ask for it.
175
175
<!-- Default configuration for services in *this* file -->
176
176
<defaults autowire =" true" autoconfigure =" true" public =" false" />
177
177
178
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
178
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
179
179
</services >
180
180
</container >
181
181
@@ -194,7 +194,23 @@ each time you ask for it.
194
194
;
195
195
196
196
// $this is a reference to the current loader
197
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
197
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
198
+
199
+ .. code-block :: php-fluent-di
200
+
201
+ // config/services.php
202
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
203
+
204
+ return function(ContainerConfigurator $configurator) {
205
+ $container = $configurator->services()
206
+ ->defaults()
207
+ ->autowire()
208
+ ->autoconfigure()
209
+ ->private();
210
+
211
+ $container->load('App\\', '../src/*')
212
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
213
+ };
198
214
199
215
.. tip ::
200
216
@@ -396,7 +412,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396
412
# same as before
397
413
App\ :
398
414
resource : ' ../src/*'
399
- exclude : ' ../src/{Entity,Migrations,Tests}'
415
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
400
416
401
417
# explicitly configure the service
402
418
App\Updates\SiteUpdateManager :
@@ -416,7 +432,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416
432
<!-- ... -->
417
433
418
434
<!-- Same as before -->
419
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
435
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
420
436
421
437
<!-- Explicitly configure the service -->
422
438
<service id =" App\Updates\SiteUpdateManager" >
@@ -440,12 +456,32 @@ pass here. No problem! In your configuration, you can explicitly set this argume
440
456
->setPublic(false)
441
457
;
442
458
443
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
459
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }');
444
460
445
461
// Explicitly configure the service
446
462
$container->getDefinition(SiteUpdateManager::class)
447
463
->setArgument('$adminEmail', 'manager@example.com');
448
464
465
+ .. code-block :: php-fluent-di
466
+
467
+ // config/services.php
468
+ use App\Updates\SiteUpdateManager;
469
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
470
+
471
+ return function(ContainerConfigurator $configurator) {
472
+ $container = $configurator->services()
473
+ ->defaults()
474
+ ->autowire()
475
+ ->autoconfigure()
476
+ ->private();
477
+
478
+ $container->load('App\\', '../src/*')
479
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
480
+
481
+ $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
482
+ };
483
+
484
+
449
485
Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
450
486
argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
451
487
other arguments will still be autowired.
@@ -510,6 +546,22 @@ and reference it with the ``%parameter_name%`` syntax:
510
546
// ...
511
547
->setArgument('$adminEmail', '%admin_email%');
512
548
549
+ .. code-block :: php-fluent-di
550
+
551
+ // config/services.php
552
+ use App\Updates\SiteUpdateManager;
553
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
554
+
555
+ return function(ContainerConfigurator $configurator) {
556
+ $configurator->parameters()->set('admin_email', 'manager@example.com');
557
+
558
+ $container = $configurator->services();
559
+ $container->set(SiteUpdateManager::class)
560
+ ->autowire()
561
+ ->arg('$adminEmail', '%admin_email%');
562
+ };
563
+
564
+
513
565
Actually, once you define a parameter, it can be referenced via the
514
566
``%parameter_name% `` syntax in *any * other configuration file. Many parameters
515
567
are defined in the ``config/services.yaml `` file.
@@ -620,6 +672,22 @@ But, you can control this and pass in a different logger:
620
672
->setPublic(false)
621
673
->setArgument('$logger', new Reference('monolog.logger.request'));
622
674
675
+ .. code-block :: php-fluent-di
676
+
677
+ // config/services.php
678
+ use App\Service\MessageGenerator;
679
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
680
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
681
+
682
+ return function(ContainerConfigurator $configurator) {
683
+ $container = $configurator->services();
684
+ $container->set(SiteUpdateManager::class)
685
+ ->autowire()
686
+ ->autoconfigure()
687
+ ->private();
688
+ ->arg('$logger', ref('monolog.logger.request'));
689
+ };
690
+
623
691
This tells the container that the ``$logger `` argument to ``__construct `` should use
624
692
service whose id is ``monolog.logger.request ``.
625
693
@@ -716,6 +784,25 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
716
784
])
717
785
;
718
786
787
+ .. code-block :: php-fluent-di
788
+
789
+ // config/services.php
790
+ use App\Controller\LuckyController;
791
+ use Psr\Log\LoggerInterface;
792
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
793
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
794
+
795
+ return function(ContainerConfigurator $configurator) {
796
+ $container = $configurator->services()->defaults()
797
+ ->bind('$adminEmail', 'manager@example.com')
798
+ ->bind('$requestLogger', ref('monolog.logger.request'))
799
+ ->bind(LoggerInterface::class, ref('monolog.logger.request'))
800
+ ->bind('string $adminEmail', 'manager@example.com')
801
+ ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
802
+
803
+ // ...
804
+ };
805
+
719
806
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
720
807
argument for *any * service defined in this file! You can bind arguments by name
721
808
(e.g. ``$adminEmail ``), by type (e.g. ``Psr\Log\LoggerInterface ``) or both
@@ -849,6 +936,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
849
936
</services >
850
937
</container >
851
938
939
+ .. code-block :: php-fuent-di
940
+
941
+ // config/services.php
942
+
943
+ use App\Service\MessageGenerator;
944
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
945
+
946
+ return function(ContainerConfigurator $configurator) {
947
+ // ... same as code before
948
+
949
+ $container->set(MessageGenerator::class)
950
+ ->public();
951
+ };
952
+
852
953
.. _service-psr4-loader :
853
954
854
955
Importing Many Services at once with resource
@@ -869,7 +970,7 @@ key. For example, the default Symfony configuration contains this:
869
970
# this creates a service per class whose id is the fully-qualified class name
870
971
App\ :
871
972
resource : ' ../src/*'
872
- exclude : ' ../src/{Entity,Migrations,Tests}'
973
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
873
974
874
975
.. code-block :: xml
875
976
@@ -883,7 +984,7 @@ key. For example, the default Symfony configuration contains this:
883
984
<services >
884
985
<!-- ... -->
885
986
886
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
987
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
887
988
</services >
888
989
</container >
889
990
@@ -901,7 +1002,19 @@ key. For example, the default Symfony configuration contains this:
901
1002
->setPublic(false)
902
1003
;
903
1004
904
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
1005
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1006
+
1007
+ .. code-block :: php-fluent-di
1008
+
1009
+ // config/services.php
1010
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1011
+
1012
+ return function(ContainerConfigurator $configurator) {
1013
+ // ...
1014
+
1015
+ $container->load('App\\', '../src/*')
1016
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1017
+ };
905
1018
906
1019
.. tip ::
907
1020
@@ -1060,6 +1173,35 @@ admin email. In this case, each needs to have a unique service id:
1060
1173
1061
1174
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1062
1175
1176
+ .. code-block :: php-fluent-di
1177
+
1178
+ // config/services.php
1179
+ use App\Updates\SiteUpdateManager;
1180
+ use App\Service\MessageGenerator;
1181
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1182
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
1183
+
1184
+ return function(ContainerConfigurator $configurator) {
1185
+ // ...
1186
+
1187
+ $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1188
+ ->autowire(false)
1189
+ ->args([
1190
+ ref(MessageGenerator::class),
1191
+ ref('mailer'),
1192
+ 'superadmin@example.com'
1193
+ ]);
1194
+ $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1195
+ ->autowire(false)
1196
+ ->args([
1197
+ ref(MessageGenerator::class),
1198
+ ref('mailer'),
1199
+ 'contact@example.com'
1200
+ ]);
1201
+ $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1202
+ };
1203
+
1204
+
1063
1205
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1064
1206
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
1065
1207
``SiteUpdateManager `` the first (``site_update_manager.superadmin ``) will be passed.
0 commit comments