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