@@ -507,12 +507,15 @@ will share identical locators among all the services referencing them::
507507Indexing the Collection of Services
508508~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
509509
510- Services passed to the service locator can define their own index using an
511- arbitrary attribute whose name is defined as ``index_by `` in the service locator.
510+ By default, services passed to the service locator are indexed using their service
511+ IDs. You can change this behavior with two options of the tagged locator (``index_by ``
512+ and ``default_index_method ``) which can be used independently or combined.
512513
513- In the following example, the ``App\Handler\HandlerCollection `` locator receives
514- all services tagged with ``app.handler `` and they are indexed using the value
515- of the ``key `` tag attribute (as defined in the ``index_by `` locator option):
514+ The ``index_by `` / ``indexAttribute `` Option
515+ ............................................
516+
517+ This option defines the name of the option/attribute that stores the value used
518+ to index the services:
516519
517520.. configuration-block ::
518521
@@ -592,12 +595,13 @@ of the ``key`` tag attribute (as defined in the ``index_by`` locator option):
592595
593596 $services->set(App\Handler\HandlerCollection::class)
594597 // inject all services tagged with app.handler as first argument
595- ->args([tagged_locator('app.handler', 'key')])
598+ ->args([tagged_locator('app.handler', indexAttribute: 'key')])
596599 ;
597600 };
598601
599- Inside this locator you can retrieve services by index using the value of the
600- ``key `` attribute. For example, to get the ``App\Handler\Two `` service::
602+ In this example, the ``index_by `` option is ``key ``. All services define that
603+ option/attribute, so that will be the value used to index the services. For example,
604+ to get the ``App\Handler\Two `` service::
601605
602606 // src/Handler/HandlerCollection.php
603607 namespace App\Handler;
@@ -608,31 +612,25 @@ Inside this locator you can retrieve services by index using the value of the
608612 {
609613 public function __construct(ServiceLocator $locator)
610614 {
615+ // this value is defined in the `key` option of the service
611616 $handlerTwo = $locator->get('handler_two');
612617 }
613618
614619 // ...
615620 }
616621
617- Instead of defining the index in the service definition, you can return its
618- value in a method called ``getDefaultIndexName() `` inside the class associated
619- to the service::
620-
621- // src/Handler/One.php
622- namespace App\Handler;
622+ If some service doesn't define the option/attribute configured in ``index_by ``,
623+ Symfony applies this fallback process:
623624
624- class One
625- {
626- public static function getDefaultIndexName(): string
627- {
628- return 'handler_one';
629- }
625+ #. If the service class defines a static method called ``getDefault<CamelCase index_by value>Name ``
626+ (in this example, ``getDefaultKeyName() ``), call it and use the returned value;
627+ #. Otherwise, fall back to the default behavior and use the service ID.
630628
631- // ...
632- }
629+ The `` default_index_method `` Option
630+ ...................................
633631
634- If you prefer to use another method name, add a `` default_index_method ``
635- attribute to the locator service defining the name of this custom method :
632+ This option defines the name of the service class method that will be called to
633+ get the value used to index the services :
636634
637635.. configuration-block ::
638636
@@ -647,7 +645,7 @@ attribute to the locator service defining the name of this custom method:
647645 class CommandBus
648646 {
649647 public function __construct(
650- #[TaggedLocator('app.handler', 'key', defaultIndexMethod: 'myOwnMethodName ')]
648+ #[TaggedLocator('app.handler', defaultIndexMethod: 'getLocatorKey ')]
651649 ServiceLocator $locator
652650 ) {
653651 }
@@ -659,8 +657,9 @@ attribute to the locator service defining the name of this custom method:
659657 services :
660658 # ...
661659
662- App\HandlerCollection :
663- arguments : [!tagged_locator { tag: 'app.handler', index_by: 'key', default_index_method: 'myOwnMethodName' }]
660+ App\Handler\HandlerCollection :
661+ # inject all services tagged with app.handler as first argument
662+ arguments : [!tagged_locator { tag: 'app.handler', default_index_method: 'getLocatorKey' }]
664663
665664 .. code-block :: xml
666665
@@ -672,11 +671,11 @@ attribute to the locator service defining the name of this custom method:
672671 https://symfony.com/schema/dic/services/services-1.0.xsd" >
673672
674673 <services >
675-
676674 <!-- ... -->
677675
678676 <service id =" App\HandlerCollection" >
679- <argument type =" tagged_locator" tag =" app.handler" index-by =" key" default-index-method =" myOwnMethodName" />
677+ <!-- inject all services tagged with app.handler as first argument -->
678+ <argument type =" tagged_locator" tag =" app.handler" default-index-method =" getLocatorKey" />
680679 </service >
681680 </services >
682681 </container >
@@ -687,17 +686,27 @@ attribute to the locator service defining the name of this custom method:
687686 namespace Symfony\Component\DependencyInjection\Loader\Configurator;
688687
689688 return function(ContainerConfigurator $container) {
690- $container->services()
691- ->set(App\HandlerCollection::class)
692- ->args([tagged_locator('app.handler', 'key', 'myOwnMethodName')])
689+ $services = $container->services();
690+ // ...
691+
692+ $services->set(App\Handler\HandlerCollection::class)
693+ // inject all services tagged with app.handler as first argument
694+ ->args([tagged_locator('app.handler', defaultIndexMethod: 'getLocatorKey')])
693695 ;
694696 };
695697
696- .. note ::
698+ If some service class doesn't define the method configured in ``default_index_method ``,
699+ Symfony will fall back to using the service ID as its index inside the locator.
700+
701+ Combining the ``index_by `` and ``default_index_method `` Options
702+ ...............................................................
703+
704+ You can combine both options in the same locator. Symfony will process them in
705+ the following order:
697706
698- Since code should not be responsible for defining how the locators are
699- going to be used, a configuration key (`` key `` in the example above) must
700- be set so the custom method may be called as a fallback .
707+ #. If the service defines the option/attribute configured in `` index_by ``, use it;
708+ #. If the service class defines the method configured in `` default_index_method ``, use it;
709+ #. Otherwise, fall back to using the service ID as its index inside the locator .
701710
702711.. _service-subscribers-service-subscriber-trait :
703712
0 commit comments