Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[DependencyInjection] Add docs for default priority method for tagged services#12697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -585,47 +585,119 @@ application handlers:: | ||
| } | ||
| } | ||
| Tagged Services with Priority | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| ..versionadded:: 4.4 | ||
| The ability to prioritize tagged services was introduced in Symfony 4.4. | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We need a ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Added it. Thanks :) Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Missing a dot :) ContributorAuthor
| ||
| The tagged services can be prioritized using the ``priority`` attribute, thus providing | ||
| a way to inject a sorted collection. | ||
| ..configuration-block:: | ||
| .. code-block:: yaml | ||
| # config/services.yaml | ||
| services: | ||
| App\Handler\One: | ||
| tags: | ||
| - { name: 'app.handler', priority: 20 } | ||
| .. code-block::xml | ||
| <!-- config/services.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
| https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <service id="App\Handler\One"> | ||
| <tag name="app.handler" priority="20"/> | ||
| </service> | ||
| </services> | ||
| </container> | ||
| .. code-block:: php | ||
| // config/services.php | ||
| namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
| return function(ContainerConfigurator $configurator) { | ||
| $services = $configurator->services(); | ||
| $services->set(App\Handler\One::class) | ||
| ->tag('app.handler', ['priority' => 20]) | ||
| ; | ||
| }; | ||
| .. note:: | ||
| Note that any other custom attribute will be ignored by this feature. | ||
| Another option, which is particularly useful when using autoconfiguring tags, is to implement the | ||
| static ``getDefaultPriority`` method on the service itself:: | ||
| // src/App/Handler/One.php | ||
| namespace App/Handler; | ||
| class One | ||
| { | ||
| public static function getDefaultPriority(): int | ||
| { | ||
| return 3; | ||
| } | ||
| } | ||
| If you want to have another method defining the priority, you can define it in the configuration of the collecting service: | ||
| .. configuration-block:: | ||
| .. code-block:: yaml | ||
| # config/services.yaml | ||
| services: | ||
| App\HandlerCollection: | ||
| # inject all services tagged with app.handler as first argument | ||
| arguments: | ||
| - !tagged_iterator { tag: app.handler, default_priority_method: getPriority } | ||
| .. code-block:: xml | ||
| <!-- config/services.xml --> | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
| https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <service id="App\HandlerCollection"> | ||
| <argument type="tagged" tag="app.handler" default-priority-method="getPriority"/> | ||
| </service> | ||
| </services> | ||
| </container> | ||
| .. code-block:: php | ||
| // config/services.php | ||
| namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
| use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; | ||
| return function (ContainerConfigurator $configurator) { | ||
| $services = $configurator->services(); | ||
| // ... | ||
| $services->set(App\HandlerCollection::class) | ||
| ->args([ | ||
| tagged_iterator('app.handler', null, null, 'getPriority'), | ||
| ] | ||
| ) | ||
| ; | ||
| }; | ||