Skip to content

Commit 2b22d91

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: 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 5640ecb + 95d5763 commit 2b22d91

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
@@ -300,10 +300,45 @@ Defining a Service Locator
300300
--------------------------
301301

302302
To manually define a service locator and inject it to another service, create an
303-
argument of type ``service_locator``:
303+
argument of type ``service_locator``.
304+
305+
Consider the following ``CommandBus`` class where you want to inject
306+
some services into it via a service locator::
307+
308+
// src/HandlerCollection.php
309+
namespace App;
310+
311+
use Symfony\Component\DependencyInjection\ServiceLocator;
312+
313+
class CommandBus
314+
{
315+
public function __construct(ServiceLocator $locator)
316+
{
317+
}
318+
}
319+
320+
Symfony allows you to inject the service locator using YAML/XML/PHP configuration
321+
or directly via PHP attributes:
304322

305323
.. configuration-block::
306324

325+
.. conde-block:: php-attributes
326+
327+
// src/CommandBus.php
328+
namespace App;
329+
330+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
331+
use Symfony\Component\DependencyInjection\ServiceLocator;
332+
333+
class CommandBus
334+
{
335+
public function __construct(
336+
// creates a service locator with all the services tagged with 'app.handler'
337+
#[TaggedLocator('app.handler')] ServiceLocator $locator
338+
) {
339+
}
340+
}
341+
307342
.. code-block:: yaml
308343
309344
# config/services.yaml
@@ -510,7 +545,7 @@ will share identical locators among all the services referencing them::
510545
// ...
511546
'logger' => new Reference('logger'),
512547
];
513-
548+
514549
$myService = $container->findDefinition(MyService::class);
515550

516551
$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)