@@ -570,47 +570,92 @@ application handlers::
570
570
}
571
571
}
572
572
573
- .. tip ::
573
+ Prioritizing tagged services
574
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
575
+
576
+ The tagged services can be prioritized using the ``priority `` attribute:
577
+
578
+ .. configuration-block ::
579
+
580
+ .. code-block :: yaml
581
+
582
+ # config/services.yaml
583
+ services :
584
+ App\Handler\One :
585
+ tags :
586
+ - { name: 'app.handler', priority: 20 }
587
+
588
+ .. code-block :: xml
589
+
590
+ <!-- config/services.xml -->
591
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
592
+ <container xmlns =" http://symfony.com/schema/dic/services"
593
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
594
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
595
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
596
+
597
+ <services >
598
+ <service id =" App\Handler\One" >
599
+ <tag name =" app.handler" priority =" 20" />
600
+ </service >
601
+ </services >
602
+ </container >
603
+
604
+ .. code-block :: php
605
+
606
+ // config/services.php
607
+ $container->register(App\Handler\One::class)
608
+ ->addTag('app.handler', ['priority' => 20]);
609
+
610
+ Note that any other custom attributes will be ignored by this feature.
611
+
612
+
613
+ Another option, which is particularly useful when using autoconfiguring tags, is to implement the ``getDefaultPriority `` static method on a collected class::
574
614
575
- The collected services can be prioritized using the ``priority `` attribute:
615
+ // src/App/Handler/One.php
616
+ namespace App/Handler;
576
617
577
- .. configuration-block ::
618
+ class One
619
+ {
620
+ public static function getDefaultPriority(): int
621
+ {
622
+ return 3;
623
+ }
624
+ }
578
625
579
- .. code-block :: yaml
626
+ If you want to have another method defining the priority, can define it in the configuration of the collecting service:
580
627
581
- # config/services.yaml
582
- services :
583
- App\Handler\One :
584
- tags :
585
- - { name: 'app.handler', priority: 20 }
628
+ .. configuration-block ::
586
629
587
- .. code-block :: xml
630
+ .. code-block :: yaml
588
631
589
- <!-- config/services.xml -->
590
- <? xml version = " 1.0 " encoding = " UTF-8 " ?>
591
- < container xmlns = " http://symfony.com/schema/dic/services "
592
- xmlns : xsi = " http://www.w3.org/2001/XMLSchema-instance "
593
- xsi : schemaLocation = " http://symfony.com/schema/dic/services
594
- https://symfony.com/schema/dic/services/services-1.0.xsd " >
632
+ # config/services.yaml
633
+ services :
634
+ App\HandlerCollection :
635
+ # inject all services tagged with app.handler as first argument
636
+ arguments :
637
+ - !tagged_iterator { tag: app.handler, default_priority_method: getPriority }
595
638
596
- <services >
597
- <service id =" App\Handler\One" >
598
- <tag name =" app.handler" priority =" 20" />
599
- </service >
600
- </services >
601
- </container >
639
+ .. code-block :: xml
602
640
603
- .. code-block :: php
641
+ <!-- config/services.xml -->
642
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
643
+ <container xmlns =" http://symfony.com/schema/dic/services"
644
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
645
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
646
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
604
647
605
- // config/services.php
606
- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
648
+ <services >
649
+ <service id =" App\HandlerCollection" >
650
+ <argument type =" tagged" tag =" app.handler" default_priority_method =" getPriority" />
651
+ </service >
652
+ </services >
653
+ </container >
607
654
608
- return function(ContainerConfigurator $configurator) {
609
- $services = $configurator->services();
655
+ .. code-block :: php
610
656
611
- $services->set(App\Handler\One::class)
612
- ->tag('app.handler', ['priority' => 20])
613
- ;
614
- };
657
+ // config/services.php
658
+ use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
615
659
616
- Note that any other custom attributes will be ignored by this feature.
660
+ $container->register(App\HandlerCollection::class)
661
+ ->addArgument(new TaggedIteratorArgument('app.handler', null, null, false, 'getPriority'));
0 commit comments