Skip to content

Commit 3b328b8

Browse files
authored
adding a custom method to define the priority of services
This documents symfony/symfony#31943 about adding a custom method to define the priority of services
1 parent 17170ed commit 3b328b8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

service_container/tags.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,55 @@ application handlers::
562562
->addTag('app.handler', ['priority' => 20]);
563563
564564
Note that any other custom attributes will be ignored by this feature.
565+
566+
You can also prioritized services implementing a static method for it.
567+
The name of the method must be defined on the handler collection service:
568+
569+
.. configuration-block::
570+
571+
.. code-block:: yaml
572+
573+
# config/services.yaml
574+
services:
575+
App\HandlerCollection:
576+
# inject all services tagged with app.handler and ordered by the result of the getDefaultPriority
577+
arguments: [!tagged { key: 'app.handler', default_priority_method: 'getDefaultPriority']
578+
579+
.. code-block:: xml
580+
581+
<!-- config/services.xml -->
582+
<?xml version="1.0" encoding="UTF-8" ?>
583+
<container xmlns="http://symfony.com/schema/dic/services"
584+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
585+
xsi:schemaLocation="http://symfony.com/schema/dic/services
586+
https://symfony.com/schema/dic/services/services-1.0.xsd">
587+
588+
<services>
589+
<service id="App\HandlerCollection">
590+
<!-- inject all services tagged with app.handler and ordered by the result of the getDefaultPriority-->
591+
<argument type="tagged" tag="app.handler" default-priority-method="getDefaultPriority"/>
592+
</service>
593+
</services>
594+
</container>
595+
596+
.. code-block:: php
597+
598+
// config/services.php
599+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
600+
601+
$container->register(App\HandlerCollection::class)
602+
// inject all services tagged with app.handler as first argument
603+
->addArgument(new TaggedIteratorArgument('app.handler', null, null, false, 'getDefaultPriority'));
604+
605+
Now you can define the priority on the service class::
606+
607+
// src/Handler/One.php
608+
namespace App\Handler;
609+
610+
class One
611+
{
612+
public static function getDefaultPriorityMethod(): int
613+
{
614+
return 10;
615+
}
616+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy