@@ -587,47 +587,113 @@ application handlers::
587587 }
588588 }
589589
590- ..tip ::
590+ Tagged Services with Priority
591+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
591592
592- The collected services can be prioritized using the ``priority `` attribute:
593+ The tagged services can be prioritized using the ``priority `` attribute,
594+ thus providing a way to inject a sorted collection of services:
593595
594- ..configuration-block ::
596+ ..configuration-block ::
595597
596- ..code-block ::yaml
598+ ..code-block ::yaml
597599
598- # config/services.yaml
599- services :
600- App\Handler\One :
601- tags :
602- -{ name: 'app.handler', priority: 20 }
600+ # config/services.yaml
601+ services :
602+ App\Handler\One :
603+ tags :
604+ -{ name: 'app.handler', priority: 20 }
603605
604- ..code-block ::xml
606+ ..code-block ::xml
605607
606- <!-- config/services.xml-->
607- <?xml version =" 1.0" encoding =" UTF-8" ?>
608- <container xmlns =" http://symfony.com/schema/dic/services"
609- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
610- xsi : schemaLocation =" http://symfony.com/schema/dic/services
611- https://symfony.com/schema/dic/services/services-1.0.xsd" >
608+ <!-- config/services.xml-->
609+ <?xml version =" 1.0" encoding =" UTF-8" ?>
610+ <container xmlns =" http://symfony.com/schema/dic/services"
611+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
612+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
613+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
612614
613- <services >
614- <service id =" App\Handler\One" >
615- <tag name =" app.handler" priority =" 20" />
616- </service >
617- </services >
618- </container >
615+ <services >
616+ <service id =" App\Handler\One" >
617+ <tag name =" app.handler" priority =" 20" />
618+ </service >
619+ </services >
620+ </container >
619621
620- ..code-block ::php
622+ ..code-block ::php
621623
622- // config/services.php
623- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
624+ // config/services.php
625+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
624626
625- return function(ContainerConfigurator $configurator) {
626- $services = $configurator->services();
627+ use App\Handler\One;
627628
628- $services->set(App\Handler\One::class)
629- ->tag('app.handler', ['priority' => 20])
630- ;
631- };
629+ return function(ContainerConfigurator $configurator) {
630+ $services = $configurator->services();
631+
632+ $services->set(One::class)
633+ ->tag('app.handler', ['priority' => 20])
634+ ;
635+ };
632636
633- Note that any other custom attributes will be ignored by this feature.
637+ Another option, which is particularly useful when using autoconfiguring
638+ tags, is to implement the static ``getDefaultPriority() `` method on the
639+ service itself::
640+
641+ // src/Handler/One.php
642+ namespace App/Handler;
643+
644+ class One
645+ {
646+ public static function getDefaultPriority(): int
647+ {
648+ return 3;
649+ }
650+ }
651+
652+ If you want to have another method defining the priority, you can define it
653+ in the configuration of the collecting service:
654+
655+ ..configuration-block ::
656+
657+ ..code-block ::yaml
658+
659+ # config/services.yaml
660+ services :
661+ App\HandlerCollection :
662+ # inject all services tagged with app.handler as first argument
663+ arguments :
664+ -!tagged_iterator { tag: app.handler, default_priority_method: getPriority }
665+
666+ ..code-block ::xml
667+
668+ <!-- config/services.xml-->
669+ <?xml version =" 1.0" encoding =" UTF-8" ?>
670+ <container xmlns =" http://symfony.com/schema/dic/services"
671+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
672+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
673+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
674+ <services >
675+ <service id =" App\HandlerCollection" >
676+ <argument type =" tagged" tag =" app.handler" default-priority-method =" getPriority" />
677+ </service >
678+ </services >
679+ </container >
680+
681+ ..code-block ::php
682+
683+ // config/services.php
684+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
685+
686+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
687+
688+ return function (ContainerConfigurator $configurator) {
689+ $services = $configurator->services();
690+
691+ // ...
692+
693+ $services->set(App\HandlerCollection::class)
694+ ->args([
695+ tagged_iterator('app.handler', null, null, 'getPriority'),
696+ ]
697+ )
698+ ;
699+ };