@@ -220,20 +220,19 @@ each time you ask for it.
220
220
221
221
.. code-block :: php
222
222
223
- // app/ config/services.php
224
- use Symfony\Component\DependencyInjection\Definition ;
223
+ // config/services.php
224
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator ;
225
225
226
- // To use as default template
227
- $definition = new Definition();
226
+ return function(ContainerConfigurator $configurator) {
227
+ $container = $configurator->services()
228
+ ->defaults()
229
+ ->autowire()
230
+ ->autoconfigure()
231
+ ->private();
228
232
229
- $definition
230
- ->setAutowired(true)
231
- ->setAutoconfigured(true)
232
- ->setPublic(false)
233
- ;
234
-
235
- // $this is a reference to the current loader
236
- $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
233
+ $container->load('AppBundle\\', '../src/AppBundle/*')
234
+ ->exclude('../src/AppBundle/{Entity,Repository}');
235
+ };
237
236
238
237
.. tip ::
239
238
@@ -491,24 +490,18 @@ pass here. No problem! In your configuration, you can explicitly set this argume
491
490
492
491
.. code-block :: php
493
492
494
- // app/config/services.php
495
- use AppBundle\Updates\SiteUpdateManager;
496
- use Symfony\Component\DependencyInjection\Definition;
497
-
498
- // Same as before
499
- $definition = new Definition();
500
-
501
- $definition
502
- ->setAutowired(true)
503
- ->setAutoconfigured(true)
504
- ->setPublic(false)
505
- ;
493
+ // config/services.php
494
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
506
495
507
- $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
496
+ return function(ContainerConfigurator $configurator) {
497
+ $container = $configurator->services()
498
+ ->defaults()
499
+ ->autowire()
500
+ ->autoconfigure()
501
+ ->private();
508
502
509
- // Explicitly configure the service
510
- $container->getDefinition(SiteUpdateManager::class)
511
- ->setArgument('$adminEmail', 'manager@example.com');
503
+ $container->load('AppBundle\\', '../src/AppBundle/*')
504
+ ->exclude('../src/{Entity,Repository}');
512
505
513
506
.. versionadded :: 3.3
514
507
@@ -573,12 +566,19 @@ and reference it with the ``%parameter_name%`` syntax:
573
566
.. code-block :: php
574
567
575
568
// app/config/services.php
569
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
570
+
576
571
use AppBundle\Updates\SiteUpdateManager;
577
- $container->setParameter('admin_email', 'manager@example.com');
578
572
579
- $container->autowire(SiteUpdateManager::class)
580
- // ...
581
- ->setArgument('$adminEmail', '%admin_email%');
573
+ return function(ContainerConfigurator $configurator) {
574
+ $configurator->parameters()->set('admin_email', 'manager@example.com');
575
+
576
+ $container = $configurator->services();
577
+ $container->set(SiteUpdateManager::class)
578
+ ->autowire()
579
+ ->arg('$adminEmail', '%admin_email%');
580
+ };
581
+
582
582
583
583
Actually, once you define a parameter, it can be referenced via the ``%parameter_name% ``
584
584
syntax in *any * other service configuration file - like ``config.yml ``. Many parameters
@@ -679,13 +679,18 @@ But, you can control this and pass in a different logger:
679
679
.. code-block :: php
680
680
681
681
// app/config/services.php
682
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
683
+
682
684
use AppBundle\Service\MessageGenerator;
683
- use Symfony\Component\DependencyInjection\Reference;
684
685
685
- $container->autowire(MessageGenerator::class)
686
- ->setAutoconfigured(true)
687
- ->setPublic(false)
688
- ->setArgument('$logger', new Reference('monolog.logger.request'));
686
+ return function(ContainerConfigurator $configurator) {
687
+ $container = $configurator->services();
688
+ $container->set(MessageGenerator::class)
689
+ ->autowire()
690
+ ->autoconfigure()
691
+ ->private();
692
+ ->arg('$logger', ref('monolog.logger.request'));
693
+ };
689
694
690
695
This tells the container that the ``$logger `` argument to ``__construct `` should use
691
696
service whose id is ``monolog.logger.request ``.
@@ -753,19 +758,20 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
753
758
754
759
.. code-block :: php
755
760
756
- // config/services.php
757
- use App\Controller\LuckyController;
761
+ // app/config/services.php
762
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
763
+
758
764
use Psr\Log\LoggerInterface;
759
- use Symfony\Component\DependencyInjection\Reference;
760
765
761
- $container->register(LuckyController::class)
762
- ->setPublic(true)
763
- ->setBindings([
764
- '$adminEmail' => 'manager@example.com',
765
- '$requestLogger' => new Reference('monolog.logger.request'),
766
- LoggerInterface::class => new Reference('monolog.logger.request'),
767
- ])
768
- ;
766
+ return function(ContainerConfigurator $configurator) {
767
+ $container = $configurator->services()
768
+ ->defaults()
769
+ ->bind('$adminEmail', 'manager@example.com')
770
+ ->bind('$requestLogger', ref('monolog.logger.request'))
771
+ ->bind(LoggerInterface::class, ref('monolog.logger.request'))
772
+ ;
773
+ // ...
774
+ };
769
775
770
776
By putting the ``bind `` key under ``_defaults ``, you can specify the value of *any *
771
777
argument for *any * service defined in this file! You can bind arguments by name
@@ -930,6 +936,20 @@ need to make your service public, just override this setting:
930
936
</services >
931
937
</container >
932
938
939
+ .. code-block :: php
940
+
941
+ // app/config/services.php
942
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
943
+
944
+ use App\Service\MessageGenerator;
945
+
946
+ return function(ContainerConfigurator $configurator) {
947
+ // ... same as code before
948
+
949
+ $container->set(MessageGenerator::class)
950
+ ->public();
951
+ };
952
+
933
953
.. _service-psr4-loader :
934
954
935
955
Importing Many Services at once with resource
@@ -983,27 +1003,13 @@ key. For example, the default Symfony configuration contains this:
983
1003
.. code-block :: php
984
1004
985
1005
// app/config/services.php
986
- use Symfony\Component\DependencyInjection\Definition;
987
-
988
- // To use as default template
989
- $definition = new Definition();
1006
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
990
1007
991
- $definition
992
- ->setAutowired(true)
993
- ->setAutoconfigured(true)
994
- ->setPublic(false)
995
- ;
996
-
997
- $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
998
-
999
- // Changes default config
1000
- $definition
1001
- ->setPublic(true)
1002
- ->addTag('controller.service_arguments')
1003
- ;
1004
-
1005
- // $this is a reference to the current loader
1006
- $this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
1008
+ return function(ContainerConfigurator $configurator) {
1009
+ // ...
1010
+ $container->load('AppBundle\\', '../src/AppBundle/*')
1011
+ ->exclude('../src/AppBundle/{Entity,Repository}');
1012
+ };
1007
1013
1008
1014
.. tip ::
1009
1015
@@ -1144,27 +1150,30 @@ admin email. In this case, each needs to have a unique service id:
1144
1150
.. code-block :: php
1145
1151
1146
1152
// app/config/services.php
1153
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1154
+
1147
1155
use AppBundle\Service\MessageGenerator;
1148
1156
use AppBundle\Updates\SiteUpdateManager;
1149
- use Symfony\Component\DependencyInjection\Reference;
1150
-
1151
- $container->register('site_update_manager.superadmin', SiteUpdateManager::class)
1152
- ->setAutowired(false)
1153
- ->setArguments([
1154
- new Reference(MessageGenerator::class),
1155
- new Reference('mailer'),
1156
- 'superadmin@example.com'
1157
- ]);
1158
-
1159
- $container->register('site_update_manager.normal_users', SiteUpdateManager::class)
1160
- ->setAutowired(false)
1161
- ->setArguments([
1162
- new Reference(MessageGenerator::class),
1163
- new Reference('mailer'),
1164
- 'contact@example.com'
1165
- ]);
1166
-
1167
- $container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1157
+
1158
+ return function(ContainerConfigurator $configurator) {
1159
+ // ...
1160
+
1161
+ $container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1162
+ ->autowire(false)
1163
+ ->args([
1164
+ ref(MessageGenerator::class),
1165
+ ref('mailer'),
1166
+ 'superadmin@example.com'
1167
+ ]);
1168
+ $container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1169
+ ->autowire(false)
1170
+ ->args([
1171
+ ref(MessageGenerator::class),
1172
+ ref('mailer'),
1173
+ 'contact@example.com'
1174
+ ]);
1175
+ $container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1176
+ };
1168
1177
1169
1178
In this case, *two * services are registered: ``site_update_manager.superadmin ``
1170
1179
and ``site_update_manager.normal_users ``. Thanks to the alias, if you type-hint
0 commit comments