@@ -507,12 +507,15 @@ will share identical locators among all the services referencing them::
507
507
Indexing the Collection of Services
508
508
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
509
509
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.
512
513
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:
516
519
517
520
.. configuration-block ::
518
521
@@ -592,12 +595,13 @@ of the ``key`` tag attribute (as defined in the ``index_by`` locator option):
592
595
593
596
$services->set(App\Handler\HandlerCollection::class)
594
597
// 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')])
596
599
;
597
600
};
598
601
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::
601
605
602
606
// src/Handler/HandlerCollection.php
603
607
namespace App\Handler;
@@ -608,31 +612,25 @@ Inside this locator you can retrieve services by index using the value of the
608
612
{
609
613
public function __construct(ServiceLocator $locator)
610
614
{
615
+ // this value is defined in the `key` option of the service
611
616
$handlerTwo = $locator->get('handler_two');
612
617
}
613
618
614
619
// ...
615
620
}
616
621
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:
623
624
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.
630
628
631
- // ...
632
- }
629
+ The `` default_index_method `` Option
630
+ ...................................
633
631
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 :
636
634
637
635
.. configuration-block ::
638
636
@@ -647,7 +645,7 @@ attribute to the locator service defining the name of this custom method:
647
645
class CommandBus
648
646
{
649
647
public function __construct(
650
- #[TaggedLocator('app.handler', 'key', defaultIndexMethod: 'myOwnMethodName ')]
648
+ #[TaggedLocator('app.handler', defaultIndexMethod: 'getLocatorKey ')]
651
649
ServiceLocator $locator
652
650
) {
653
651
}
@@ -659,8 +657,9 @@ attribute to the locator service defining the name of this custom method:
659
657
services :
660
658
# ...
661
659
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' }]
664
663
665
664
.. code-block :: xml
666
665
@@ -672,11 +671,11 @@ attribute to the locator service defining the name of this custom method:
672
671
https://symfony.com/schema/dic/services/services-1.0.xsd" >
673
672
674
673
<services >
675
-
676
674
<!-- ... -->
677
675
678
676
<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" />
680
679
</service >
681
680
</services >
682
681
</container >
@@ -687,17 +686,27 @@ attribute to the locator service defining the name of this custom method:
687
686
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
688
687
689
688
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')])
693
695
;
694
696
};
695
697
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:
697
706
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 .
701
710
702
711
.. _service-subscribers-service-subscriber-trait :
703
712
0 commit comments