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

[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

Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 104 additions & 32 deletionsservice_container/tags.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -585,47 +585,119 @@ application handlers::
}
}

.. tip::

The collected services can be prioritized using the ``priority`` attribute:
Tagged Services with Priority
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

..configuration-block::
..versionadded:: 4.4

.. code-block:: yaml
The ability to prioritize tagged services was introduced in Symfony 4.4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Added it. Thanks :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Missing a dot :)

Copy link
ContributorAuthor

@alexandrunastasealexandrunastaseJun 8, 2020
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Added it :)

# config/services.yaml
services:
App\Handler\One:
tags:
- { name: 'app.handler', priority: 20 }
The tagged services can be prioritized using the ``priority`` attribute, thus providing
a way to inject a sorted collection.

..code-block:: xml
..configuration-block::

<!-- 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">
.. code-block:: yaml

<services>
<service id="App\Handler\One">
<tag name="app.handler" priority="20"/>
</service>
</services>
</container>
# config/services.yaml
services:
App\Handler\One:
tags:
- { name: 'app.handler', priority: 20 }

.. code-block::php
.. code-block::xml

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
<!-- 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">

return function(ContainerConfigurator $configurator) {
$services = $configurator->services();
<services>
<service id="App\Handler\One">
<tag name="app.handler" priority="20"/>
</service>
</services>
</container>

$services->set(App\Handler\One::class)
->tag('app.handler', ['priority' => 20])
;
};
.. 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::

Note that any other custom attributes will be ignored by this feature.
// 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'),
]
)
;
};

[8]ページ先頭

©2009-2025 Movatter.jp