Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit00fda26

Browse files
[DependencyInjection] Add method priority for tagged services
1 parentc681bd7 commit00fda26

File tree

1 file changed

+77
-32
lines changed

1 file changed

+77
-32
lines changed

‎service_container/tags.rst‎

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -570,47 +570,92 @@ application handlers::
570570
}
571571
}
572572

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+
<containerxmlns="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+
<serviceid="App\Handler\One">
599+
<tagname="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]);
574609
575-
The collected services canbeprioritized using the ``priority`` attribute:
610+
Note that any other custom attributes willbeignored by this feature.
576611

577-
..configuration-block::
578612

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::
580614

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;
586617

587-
..code-block::xml
618+
class One
619+
{
620+
public static function getDefaultPriority(): int
621+
{
622+
return 3;
623+
}
624+
}
588625

589-
<!-- config/services.xml-->
590-
<?xml version="1.0" encoding="UTF-8" ?>
591-
<containerxmlns="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:
595627

596-
<services>
597-
<serviceid="App\Handler\One">
598-
<tagname="app.handler"priority="20"/>
599-
</service>
600-
</services>
601-
</container>
628+
..configuration-block::
602629

603-
..code-block::php
630+
..code-block::yaml
604631
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 }
607638
608-
return function(ContainerConfigurator $configurator) {
609-
$services = $configurator->services();
639+
..code-block::xml
610640
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+
<containerxmlns="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+
<serviceid="App\HandlerCollection">
650+
<argumenttype="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;
615659
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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp