From f6c140c2f99f8d062c047735784f08fe66d66aea Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Sat, 14 Jan 2023 20:30:06 +0100 Subject: [PATCH] [DependencyInjection] Complete examples with TaggedIterator and TaggedLocator attributes --- .../service_subscribers_locators.rst | 34 +++++++++++++ service_container/tags.rst | 48 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index c3cf4c6a744..2630db0977d 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -516,6 +516,23 @@ of the ``key`` tag attribute (as defined in the ``index_by`` locator option): .. configuration-block:: + .. code-block:: php-attributes + + // src/CommandBus.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; + use Symfony\Component\DependencyInjection\ServiceLocator; + + class CommandBus + { + public function __construct( + #[TaggedLocator('app.handler', indexAttribute: 'key')] + ServiceLocator $locator + ) { + } + } + .. code-block:: yaml # config/services.yaml @@ -619,6 +636,23 @@ attribute to the locator service defining the name of this custom method: .. configuration-block:: + .. code-block:: php-attributes + + // src/CommandBus.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; + use Symfony\Component\DependencyInjection\ServiceLocator; + + class CommandBus + { + public function __construct( + #[TaggedLocator('app.handler', 'key', defaultIndexMethod: 'myOwnMethodName')] + ServiceLocator $locator + ) { + } + } + .. code-block:: yaml # config/services.yaml diff --git a/service_container/tags.rst b/service_container/tags.rst index d166c7b88a8..e34a01a7739 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -707,6 +707,22 @@ you can define it in the configuration of the collecting service: .. configuration-block:: + .. code-block:: php-attributes + + // src/HandlerCollection.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; + + class HandlerCollection + { + public function __construct( + #[TaggedIterator('app.handler', defaultPriorityMethod: 'getPriority')] + iterable $handlers + ) { + } + } + .. code-block:: yaml # config/services.yaml @@ -762,6 +778,22 @@ indexed by the ``key`` attribute: .. configuration-block:: + .. code-block:: php-attributes + + // src/HandlerCollection.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; + + class HandlerCollection + { + public function __construct( + #[TaggedIterator('app.handler', indexAttribute: 'key')] + iterable $handlers + ) { + } + } + .. code-block:: yaml # config/services.yaml @@ -868,6 +900,22 @@ array element. For example, to retrieve the ``handler_two`` handler:: .. configuration-block:: + .. code-block:: php-attributes + + // src/HandlerCollection.php + namespace App; + + use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; + + class HandlerCollection + { + public function __construct( + #[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')] + iterable $handlers + ) { + } + } + .. code-block:: yaml # config/services.yaml