@@ -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
+ https://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]);
574
609
575
- The collected services can be prioritized using the `` priority `` attribute:
610
+ Note that any other custom attributes will be ignored by this feature.
576
611
577
- .. configuration-block ::
578
612
579
- .. code-block :: yaml
613
+ Another option, which is particularly useful when using autoconfiguring tags, is to implement the `` getDefaultPriority `` static method on a collected class::
580
614
581
- # config/services.yaml
582
- services :
583
- App\Handler\One :
584
- tags :
585
- - { name: 'app.handler', priority: 20 }
615
+ // src/App/Handler/One.php
616
+ namespace App/Handler;
586
617
587
- .. code-block :: xml
618
+ class One
619
+ {
620
+ public static function getDefaultPriority(): int
621
+ {
622
+ return 3;
623
+ }
624
+ }
588
625
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" >
626
+ If you want to have another method defining the priority, can define it in the configuration of the collecting service:
595
627
596
- <services >
597
- <service id =" App\Handler\One" >
598
- <tag name =" app.handler" priority =" 20" />
599
- </service >
600
- </services >
601
- </container >
628
+ .. configuration-block ::
602
629
603
- .. code-block :: php
630
+ .. code-block :: yaml
604
631
605
- // config/services.php
606
- namespace Symfony\Component\DependencyInjection\Loader\Configurator;
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 }
607
638
608
- return function(ContainerConfigurator $configurator) {
609
- $services = $configurator->services();
639
+ .. code-block :: xml
610
640
611
- $services->set(App\Handler\One::class)
612
- ->tag('app.handler', ['priority' => 20])
613
- ;
614
- };
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
+ https://symfony.com/schema/dic/services/services-1.0.xsd" >
647
+
648
+ <services >
649
+ <service id =" App\HandlerCollection" >
650
+ <argument type =" tagged" tag =" app.handler" default_priority_method =" getPriority" />
651
+ </service >
652
+ </services >
653
+ </container >
654
+
655
+ .. code-block :: php
656
+
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