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

Commitae734d0

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

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+
http://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]);
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::
574614

575-
The collected services can be prioritized using the ``priority`` attribute:
615+
// src/App/Handler/One.php
616+
namespace App/Handler;
576617

577-
..configuration-block::
618+
class One
619+
{
620+
public static function getDefaultPriority(): int
621+
{
622+
return 3;
623+
}
624+
}
578625

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

581-
# config/services.yaml
582-
services:
583-
App\Handler\One:
584-
tags:
585-
-{ name: 'app.handler', priority: 20 }
628+
..configuration-block::
586629

587-
..code-block::xml
630+
..code-block::yaml
588631
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">
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 }
595638
596-
<services>
597-
<serviceid="App\Handler\One">
598-
<tagname="app.handler"priority="20"/>
599-
</service>
600-
</services>
601-
</container>
639+
..code-block::xml
602640
603-
..code-block::php
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+
http://symfony.com/schema/dic/services/services-1.0.xsd">
604647
605-
// config/services.php
606-
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
648+
<services>
649+
<serviceid="App\HandlerCollection">
650+
<argumenttype="tagged"tag="app.handler"default_priority_method="getPriority"/>
651+
</service>
652+
</services>
653+
</container>
607654
608-
return function(ContainerConfigurator $configurator) {
609-
$services = $configurator->services();
655+
..code-block::php
610656
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;
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