Skip to content

Commit bbcbffb

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: 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 8ee9da6 + a5b8e76 commit bbcbffb

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 41 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
@@ -304,6 +339,10 @@ As shown in the previous sections, the constructor of the ``CommandBus`` class
304339
must type-hint its argument with ``ContainerInterface``. Then, you can get any of
305340
the service locator services via their ID (e.g. ``$this->locator->get('App\FooCommand')``).
306341

342+
.. versionadded:: 5.3
343+
344+
The ``#[TaggedLocator]`` attribute was introduced in Symfony 5.3 and requires PHP 8.
345+
307346
Reusing a Service Locator in Multiple Services
308347
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
309348

@@ -457,7 +496,7 @@ will share identical locators among all the services referencing them::
457496
// ...
458497
'logger' => new Reference('logger'),
459498
];
460-
499+
461500
$myService = $container->findDefinition(MyService::class);
462501

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

service_container/tags.rst

Lines changed: 33 additions & 13 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,18 +607,9 @@ 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;
610+
.. versionadded:: 5.3
586611

587-
class HandlerCollection
588-
{
589-
public function __construct(iterable $handlers)
590-
{
591-
}
592-
}
612+
The ``#[TaggedIterator]`` attribute was introduced in Symfony 5.3 and requires PHP 8.
593613

594614
.. seealso::
595615

0 commit comments

Comments
 (0)