@@ -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
@@ -389,7 +405,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
389
405
# same as before
390
406
App\ :
391
407
resource : ' ../src/*'
392
- exclude : ' ../src/{Entity,Migrations,Tests}'
408
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
393
409
394
410
# explicitly configure the service
395
411
App\Updates\SiteUpdateManager :
@@ -409,7 +425,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
409
425
<!-- ... -->
410
426
411
427
<!-- Same as before -->
412
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
428
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
413
429
414
430
<!-- Explicitly configure the service -->
415
431
<service id =" App\Updates\SiteUpdateManager" >
@@ -433,12 +449,32 @@ pass here. No problem! In your configuration, you can explicitly set this argume
433
449
->setPublic(false)
434
450
;
435
451
436
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
452
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }');
437
453
438
454
// Explicitly configure the service
439
455
$container->getDefinition(SiteUpdateManager::class)
440
456
->setArgument('$adminEmail', 'manager@example.com');
441
457
458
+ .. code-block :: php-fluent-di
459
+
460
+ // config/services.php
461
+ use App\Updates\SiteUpdateManager;
462
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
463
+
464
+ return function(ContainerConfigurator $configurator) {
465
+ $container = $configurator->services()
466
+ ->defaults()
467
+ ->autowire()
468
+ ->autoconfigure()
469
+ ->private();
470
+
471
+ $container->load('App\\', '../src/*')
472
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
473
+
474
+ $container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
475
+ };
476
+
477
+
442
478
Thanks to this, the container will pass ``manager@example.com `` to the ``$adminEmail ``
443
479
argument of ``__construct `` when creating the ``SiteUpdateManager `` service. The
444
480
other arguments will still be autowired.
@@ -503,6 +539,22 @@ and reference it with the ``%parameter_name%`` syntax:
503
539
// ...
504
540
->setArgument('$adminEmail', '%admin_email%');
505
541
542
+ .. code-block :: php-fluent-di
543
+
544
+ // config/services.php
545
+ use App\Updates\SiteUpdateManager;
546
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
547
+
548
+ return function(ContainerConfigurator $configurator) {
549
+ $configurator->parameters()->set('admin_email', 'manager@example.com');
550
+
551
+ $container = $configurator->services();
552
+ $container->set(SiteUpdateManager::class)
553
+ ->autowire()
554
+ ->arg('$adminEmail', '%admin_email%');
555
+ };
556
+
557
+
506
558
Actually, once you define a parameter, it can be referenced via the
507
559
``%parameter_name% `` syntax in *any * other configuration file. Many parameters
508
560
are defined in the ``config/services.yaml `` file.
@@ -613,6 +665,22 @@ But, you can control this and pass in a different logger:
613
665
->setPublic(false)
614
666
->setArgument('$logger', new Reference('monolog.logger.request'));
615
667
668
+ .. code-block :: php-fluent-di
669
+
670
+ // config/services.php
671
+ use App\Service\MessageGenerator;
672
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
673
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
674
+
675
+ return function(ContainerConfigurator $configurator) {
676
+ $container = $configurator->services();
677
+ $container->set(SiteUpdateManager::class)
678
+ ->autowire()
679
+ ->autoconfigure()
680
+ ->private();
681
+ ->arg('$logger', ref('monolog.logger.request'));
682
+ };
683
+
616
684
This tells the container that the ``$logger `` argument to ``__construct `` should use
617
685
service whose id is ``monolog.logger.request ``.
618
686
@@ -709,6 +777,25 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
709
777
])
710
778
;
711
779
780
+ .. code-block :: php-fluent-di
781
+
782
+ // config/services.php
783
+ use App\Controller\LuckyController;
784
+ use Psr\Log\LoggerInterface;
785
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
786
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
787
+
788
+ return function(ContainerConfigurator $configurator) {
789
+ $container = $configurator->services()->defaults()
790
+ ->bind('$adminEmail', 'manager@example.com')
791
+ ->bind('$requestLogger', ref('monolog.logger.request'))
792
+ ->bind(LoggerInterface::class, ref('monolog.logger.request'))
793
+ ->bind('string $adminEmail', 'manager@example.com')
794
+ ->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
795
+
796
+ // ...
797
+ };
798
+
712
799
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
713
800
argument for *any * service defined in this file! You can bind arguments by name
714
801
(e.g. ``$adminEmail ``), by type (e.g. ``Psr\Log\LoggerInterface ``) or both
@@ -842,6 +929,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
842
929
</services >
843
930
</container >
844
931
932
+ .. code-block :: php-fuent-di
933
+
934
+ // config/services.php
935
+
936
+ use App\Service\MessageGenerator;
937
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
938
+
939
+ return function(ContainerConfigurator $configurator) {
940
+ // ... same as code before
941
+
942
+ $container->set(MessageGenerator::class)
943
+ ->public();
944
+ };
945
+
845
946
.. _service-psr4-loader :
846
947
847
948
Importing Many Services at once with resource
@@ -862,7 +963,7 @@ key. For example, the default Symfony configuration contains this:
862
963
# this creates a service per class whose id is the fully-qualified class name
863
964
App\ :
864
965
resource : ' ../src/*'
865
- exclude : ' ../src/{Entity,Migrations,Tests}'
966
+ exclude : ' ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }'
866
967
867
968
.. code-block :: xml
868
969
@@ -876,7 +977,7 @@ key. For example, the default Symfony configuration contains this:
876
977
<services >
877
978
<!-- ... -->
878
979
879
- <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{Entity,Migrations,Tests}" />
980
+ <prototype namespace =" App\" resource =" ../src/*" exclude =" ../src/{DependencyInjection, Entity,Migrations,Tests,Kernel.php }" />
880
981
</services >
881
982
</container >
882
983
@@ -894,7 +995,19 @@ key. For example, the default Symfony configuration contains this:
894
995
->setPublic(false)
895
996
;
896
997
897
- $this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
998
+ $this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
999
+
1000
+ .. code-block :: php-fluent-di
1001
+
1002
+ // config/services.php
1003
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1004
+
1005
+ return function(ContainerConfigurator $configurator) {
1006
+ // ...
1007
+
1008
+ $container->load('App\\', '../src/*')
1009
+ ->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1010
+ };
898
1011
899
1012
.. tip ::
900
1013
@@ -1053,6 +1166,35 @@ admin email. In this case, each needs to have a unique service id:
1053
1166
1054
1167
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1055
1168
1169
+ .. code-block :: php-fluent-di
1170
+
1171
+ // config/services.php
1172
+ use App\Updates\SiteUpdateManager;
1173
+ use App\Service\MessageGenerator;
1174
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1175
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
1176
+
1177
+ return function(ContainerConfigurator $configurator) {
1178
+ // ...
1179
+
1180
+ $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1181
+ ->autowire(false)
1182
+ ->args([
1183
+ ref(MessageGenerator::class),
1184
+ ref('mailer'),
1185
+ 'superadmin@example.com'
1186
+ ]);
1187
+ $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1188
+ ->autowire(false)
1189
+ ->args([
1190
+ ref(MessageGenerator::class),
1191
+ ref('mailer'),
1192
+ 'contact@example.com'
1193
+ ]);
1194
+ $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1195
+ };
1196
+
1197
+
1056
1198
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1057
1199
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
1058
1200
``SiteUpdateManager `` the first (``site_update_manager.superadmin ``) will be passed.
0 commit comments