Skip to content

Commit de3ce18

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: Merged the new example into the existing configuration block [DependencyInjection] Autowire arguments using the #[TaggedLocator] attribute [DependencyInjection] Autowire arguments using the #[TaggedIterator] attribute
2 parents ac95c90 + 9a2b261 commit de3ce18

File tree

2 files changed

+83
-17
lines changed

2 files changed

+83
-17
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,45 @@ Defining a Service Locator
247247
--------------------------
248248

249249
To manually define a service locator and inject it to another service, create an
250-
argument of type ``service_locator``:
250+
argument of type ``service_locator``.
251+
252+
Consider the following ``CommandBus`` class where you want to inject
253+
some services into it via a service locator::
254+
255+
// src/HandlerCollection.php
256+
namespace App;
257+
258+
use Symfony\Component\DependencyInjection\ServiceLocator;
259+
260+
class CommandBus
261+
{
262+
public function __construct(ServiceLocator $locator)
263+
{
264+
}
265+
}
266+
267+
Symfony allows you to inject the service locator using YAML/XML/PHP configuration
268+
or directly via PHP attributes:
251269

252270
.. configuration-block::
253271

272+
.. conde-block:: php-attributes
273+
274+
// src/CommandBus.php
275+
namespace App;
276+
277+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
278+
use Symfony\Component\DependencyInjection\ServiceLocator;
279+
280+
class CommandBus
281+
{
282+
public function __construct(
283+
// creates a service locator with all the services tagged with 'app.handler'
284+
#[TaggedLocator('app.handler')] ServiceLocator $locator
285+
) {
286+
}
287+
}
288+
254289
.. code-block:: yaml
255290
256291
# config/services.yaml
@@ -457,7 +492,7 @@ will share identical locators among all the services referencing them::
457492
// ...
458493
'logger' => new Reference('logger'),
459494
];
460-
495+
461496
$myService = $container->findDefinition(MyService::class);
462497

463498
$myService->addArgument(ServiceLocatorTagPass::register($container, $locateableServices));

service_container/tags.rst

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,40 @@ Symfony provides a shortcut to inject all services tagged with a specific tag,
511511
which is a common need in some applications, so you don't have to write a
512512
compiler pass just for that.
513513

514-
In the following example, all services tagged with ``app.handler`` are passed as
515-
first constructor argument to the ``App\HandlerCollection`` service:
514+
Consider the following ``HandlerCollection`` class where you want to inject
515+
all services tagged with ``app.handler`` into its constructor argument::
516+
517+
// src/HandlerCollection.php
518+
namespace App;
519+
520+
class HandlerCollection
521+
{
522+
public function __construct(iterable $handlers)
523+
{
524+
}
525+
}
526+
527+
Symfony allows you to inject the services using YAML/XML/PHP configuration or
528+
directly via PHP attributes:
516529

517530
.. configuration-block::
518531

532+
.. code-block:: php-attributes
533+
534+
// src/HandlerCollection.php
535+
namespace App;
536+
537+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
538+
539+
class HandlerCollection
540+
{
541+
public function __construct(
542+
// the attribute must be applied directly to the argument to autowire
543+
#[TaggedIterator('app.handler')] iterable $handlers
544+
) {
545+
}
546+
}
547+
519548
.. code-block:: yaml
520549
521550
# config/services.yaml
@@ -578,24 +607,26 @@ first constructor argument to the ``App\HandlerCollection`` service:
578607
;
579608
};
580609
581-
After compilation the ``HandlerCollection`` service is able to iterate over your
582-
application handlers::
583-
584-
// src/HandlerCollection.php
585-
namespace App;
586-
587-
class HandlerCollection
588-
{
589-
public function __construct(iterable $handlers)
590-
{
591-
}
592-
}
593-
594610
If for some reason you need to exclude one or more services when using a tagged
595611
iterator, add the ``exclude`` option:
596612

597613
.. configuration-block::
598614

615+
.. code-block:: php-attributes
616+
617+
// src/HandlerCollection.php
618+
namespace App;
619+
620+
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
621+
622+
class HandlerCollection
623+
{
624+
public function __construct(
625+
#[TaggedIterator('app.handler', exclude: ['App\Handler\Three'])] iterable $handlers
626+
) {
627+
}
628+
}
629+
599630
.. code-block:: yaml
600631
601632
# config/services.yaml

0 commit comments

Comments
 (0)