Skip to content

[DependencyInjection] Complete examples with #[TaggedIterator] and #[TaggedLocator] attributes #17747

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
Merged
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions service_container/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down