Skip to content

Commit 37bdbf3

Browse files
[DependencyInjection] Complete examples with TaggedIterator and TaggedLocator attributes
1 parent 362f027 commit 37bdbf3

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ of the ``key`` tag attribute (as defined in the ``index_by`` locator option):
516516

517517
.. configuration-block::
518518

519+
.. code-block:: php-attributes
520+
521+
// src/CommandBus.php
522+
namespace App;
523+
524+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
525+
use Symfony\Component\DependencyInjection\ServiceLocator;
526+
527+
class CommandBus
528+
{
529+
public function __construct(
530+
#[TaggedLocator('app.handler', indexAttribute: 'key')] ServiceLocator $locator
531+
) {
532+
}
533+
}
534+
519535
.. code-block:: yaml
520536
521537
# config/services.yaml
@@ -619,6 +635,22 @@ attribute to the locator service defining the name of this custom method:
619635

620636
.. configuration-block::
621637

638+
.. code-block:: php-attributes
639+
640+
// src/CommandBus.php
641+
namespace App;
642+
643+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
644+
use Symfony\Component\DependencyInjection\ServiceLocator;
645+
646+
class CommandBus
647+
{
648+
public function __construct(
649+
#[TaggedLocator('app.handler', 'key', defaultIndexMethod: 'myOwnMethodName')] ServiceLocator $locator
650+
) {
651+
}
652+
}
653+
622654
.. code-block:: yaml
623655
624656
# config/services.yaml

service_container/tags.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,21 @@ you can define it in the configuration of the collecting service:
707707

708708
.. configuration-block::
709709

710+
.. code-block:: php-attributes
711+
712+
// src/HandlerCollection.php
713+
namespace App;
714+
715+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
716+
717+
class HandlerCollection
718+
{
719+
public function __construct(
720+
#[TaggedIterator('app.handler', defaultPriorityMethod: 'getPriority')] iterable $handlers
721+
) {
722+
}
723+
}
724+
710725
.. code-block:: yaml
711726
712727
# config/services.yaml
@@ -762,6 +777,21 @@ indexed by the ``key`` attribute:
762777

763778
.. configuration-block::
764779

780+
.. code-block:: php-attributes
781+
782+
// src/HandlerCollection.php
783+
namespace App;
784+
785+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
786+
787+
class HandlerCollection
788+
{
789+
public function __construct(
790+
#[TaggedIterator('app.handler', indexAttribute: 'key')] iterable $handlers
791+
) {
792+
}
793+
}
794+
765795
.. code-block:: yaml
766796
767797
# config/services.yaml
@@ -921,6 +951,21 @@ array element. For example, to retrieve the ``handler_two`` handler::
921951
;
922952
};
923953
954+
.. code-block:: php-attributes
955+
956+
// src/HandlerCollection.php
957+
namespace App;
958+
959+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
960+
961+
class HandlerCollection
962+
{
963+
public function __construct(
964+
#[TaggedIterator('app.handler', defaultIndexMethod: 'getIndex')] iterable $handlers
965+
) {
966+
}
967+
}
968+
924969
The ``#[AsTaggedItem]`` attribute
925970
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
926971

0 commit comments

Comments
 (0)