@@ -392,18 +392,15 @@ will share identical locators amongst all the services referencing them::
392392 $myService->addArgument(ServiceLocatorTagPass::register($container, $locateableServices));
393393 }
394394
395- .. _`Command pattern` : https://en.wikipedia.org/wiki/Command_pattern
396-
397- Tagged Services Locator Collection with Index
398- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
395+ Indexing the Collection of Services
396+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399397
400- If you want to retrieve a specific service within the injected service collector
401- you can use the ``index_by `` and ``default_index_method `` options of the argument
402- in combination with ``!tagged_locator `` to define an index.
398+ Services passed to the service locator can define their own index using an
399+ arbitrary attribute whose name is defined as ``index_by `` in the service locator.
403400
404- In the following example, all services tagged with `` app.handler `` are passed as
405- first constructor argument to ``App\Handler\HandlerCollection ``,
406- but we can now access a specific injected service :
401+ In the following example, the `` App\Handler\HandlerCollection `` locator receives
402+ all services tagged with ``app.handler `` and they are indexed using the value
403+ of the `` key `` tag attribute (as defined in the `` index_by `` locator option) :
407404
408405.. configuration-block ::
409406
@@ -464,9 +461,8 @@ but we can now access a specific injected service:
464461 // inject all services tagged with app.handler as first argument
465462 ->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('app.handler', 'key')));
466463
467- After compilation the ``HandlerCollection `` to retrieve a specific service by it's ``key `` attribute
468- from the service locator injected, we just have to do ``$serviceLocator->get('handler_two'); `` to
469- retrieve the ``handler_two `` handler::
464+ Inside this locator you can retrieve services by index using the value of the
465+ ``key `` attribute. For example, to get the ``App\Handler\Two `` service::
470466
471467 // src/Handler/HandlerCollection.php
472468 namespace App\Handler;
@@ -479,126 +475,67 @@ retrieve the ``handler_two`` handler::
479475 {
480476 $handlerTwo = $locator->get('handler_two'):
481477 }
482- }
483-
484- .. tip ::
485-
486- You can omit the ``index_attribute_name `` attribute, by implementing a static
487- method ``getDefaultIndexAttributeName `` to the handler.
488-
489- Based on the previous example ``App\Handler\One `` should look like this::
490-
491- // src/Handler/One.php
492- namespace App\Handler;
493-
494- class One
495- {
496- public static function getDefaultIndexName(): string
497- {
498- return 'handler_one';
499- }
500- }
501-
502- And the configuration:
503-
504- .. configuration-block ::
505478
506- .. code-block :: yaml
507-
508- # config/services.yaml
509- services :
510- App\Handler\One :
511- tags :
512- - { name: 'app.handler', priority: 20 }
513-
514- # ...
515-
516- .. code-block :: xml
517-
518- <!-- config/services.xml -->
519- <?xml version =" 1.0" encoding =" UTF-8" ?>
520- <container xmlns =" http://symfony.com/schema/dic/services"
521- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
522- xsi : schemaLocation =" http://symfony.com/schema/dic/services
523- http://symfony.com/schema/dic/services/services-1.0.xsd" >
524-
525- <services >
526- <service id =" App\Handler\One" >
527- <tag name =" app.handler" priority =" 20" />
528- </service >
529-
530- <!-- ... -->
531- </services >
532- </container >
533-
534- .. code-block :: php
535-
536- // config/services.php
537- $container->register(App\Handler\One::class)
538- ->addTag('app.handler', ['priority' => 20]);
539-
540- // ...
541-
542- You also can define the name of the static method to implement on each service
543- with the ``default_index_method `` attribute on the argument.
479+ // ...
480+ }
544481
545- Based on the previous example ``App\Handler\One `` should look like::
482+ Instead of defining the index in the service definition, you can return its
483+ value in a method called ``getDefaultIndexName() `` inside the class associated
484+ to the service::
546485
547- // src/Handler/One.php
548- namespace App\Handler;
486+ // src/Handler/One.php
487+ namespace App\Handler;
549488
550- class One
489+ class One
490+ {
491+ public static function getDefaultIndexName(): string
551492 {
552- public static function someFunctionName(): string
553- {
554- return 'handler_one';
555- }
493+ return 'handler_one';
556494 }
557495
558- And the configuration:
496+ // ...
497+ }
559498
560- .. configuration-block ::
499+ If you prefer to use another method name, add a ``default_index_method ``
500+ attribute to the locator service defining the name of this custom method:
561501
562- .. code -block :: yaml
502+ .. configuration -block ::
563503
564- # config/services.yaml
565- services :
566- # ...
504+ .. code-block :: yaml
567505
568- App\HandlerCollection :
569- # inject all services tagged with app.handler as first argument
570- arguments : [!tagged_locator { tag: 'app.handler', index_by: 'key', default_index_method: 'someFunctionName' }]
506+ # config/services.yaml
507+ services :
508+ # ...
571509
572- .. code-block :: xml
510+ App\HandlerCollection :
511+ arguments : [!tagged_locator { tag: 'app.handler', default_index_method: 'myOwnMethodName' }]
573512
574- <!-- config/services.xml -->
575- <?xml version =" 1.0" encoding =" UTF-8" ?>
576- <container xmlns =" http://symfony.com/schema/dic/services"
577- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
578- xsi : schemaLocation =" http://symfony.com/schema/dic/services
579- http://symfony.com/schema/dic/services/services-1.0.xsd" >
513+ .. code-block :: xml
580514
581- <services >
515+ <!-- config/services.xml -->
516+ <?xml version =" 1.0" encoding =" UTF-8" ?>
517+ <container xmlns =" http://symfony.com/schema/dic/services"
518+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
519+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
520+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
582521
583- <!-- ... --! >
522+ < services >
584523
585- <service id="App\HandlerCollection">
586- <!-- inject all services tagged with app.handler as first argument -->
587- <argument type =" tagged_locator" tag =" app.handler" index-by =" key" default-index-method =" someFunctionName" />
588- </service >
589- </services >
590- </container >
524+ <!-- ... -->
591525
592- .. code-block :: php
526+ <service id =" App\HandlerCollection" >
527+ <argument type =" tagged_locator" tag =" app.handler" default-index-method =" myOwnMethodName" />
528+ </service >
529+ </services >
530+ </container >
593531
594- // config/services.php
595- // ...
532+ .. code-block :: php
596533
597- $container->register(App\HandlerCollection::class)
598- // inject all services tagged with app.handler as first argument
599- ->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('app.handler', 'key', 'someFunctionName')));
534+ // config/services.php
535+ // ...
600536
601- See also :doc: `tagged services </service_container/tags >`
537+ $container->register(App\HandlerCollection::class)
538+ ->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('app.handler', null, 'myOwnMethodName')));
602539
603540 Service Subscriber Trait
604541------------------------
@@ -690,3 +627,5 @@ and compose your services with them::
690627 When creating these helper traits, the service id cannot be ``__METHOD__ ``
691628 as this will include the trait name, not the class name. Instead, use
692629 ``__CLASS__.'::'.__FUNCTION__ `` as the service id.
630+
631+ .. _`Command pattern` : https://en.wikipedia.org/wiki/Command_pattern
0 commit comments