10000 [DependencyInjection] Add docs for default priority method for tagged services by alexandrunastase · Pull Request #12697 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add docs for default priority method for tagged services #12697

< 8000 summary id="button-4d0c372e0f7f2b62" class="btn btn-sm btn-primary m-0 ml-0 ml-md-2" > 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all 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
[DependencyInjection] Add docs for default priority method for tagged…
… services
  • Loading branch information
alexandrunastase authored and wouterj committed Oct 3, 2020
commit 294ac51bf0d6bea5360cb9cec4b98ff21053515a
136 changes: 104 additions & 32 deletions service_container/tags.rst
3C7C
Original file line number Diff line number Diff 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
Contributor Author

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
Contributor Author
@alexandrunastase alexandrunastase Jun 8, 2020

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'),
]
)
;
};
0