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