@@ -298,9 +298,10 @@ This is done by having ``getSubscribedServices()`` return an array of
298
298
The above example requires using ``3.2 `` version or newer of ``symfony/service-contracts ``.
299
299
300
300
.. _service-locator_autowire-locator :
301
+ .. _service-locator_autowire-iterator :
301
302
302
- The AutowireLocator attribute
303
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303
+ The AutowireLocator and AutowireIterator Attributes
304
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304
305
305
306
Another way to define a service locator is to use the
306
307
:class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireLocator `
@@ -317,26 +318,31 @@ attribute::
317
318
class CommandBus
318
319
{
319
320
public function __construct(
320
- #[AutowireLocator(FooHandler::class, BarHandler::class)]
321
- private ContainerInterface $locator,
321
+ #[AutowireLocator([
322
+ FooHandler::class,
323
+ BarHandler::class,
324
+ ])]
325
+ private ContainerInterface $handlers,
322
326
) {
323
327
}
324
328
325
329
public function handle(Command $command): mixed
326
330
{
327
331
$commandClass = get_class($command);
328
332
329
- if ($this->locator ->has($commandClass)) {
330
- $handler = $this->locator ->get($commandClass);
333
+ if ($this->handlers ->has($commandClass)) {
334
+ $handler = $this->handlers ->get($commandClass);
331
335
332
336
return $handler->handle($command);
333
337
}
334
338
}
335
339
}
336
340
337
341
Just like with the ``getSubscribedServices() `` method, it is possible
338
- to define aliased services thanks to named arguments, as well as optional
339
- services::
342
+ to define aliased services thanks to the array keys, as well as optional
343
+ services, plus you can nest it with
344
+ :class: `Symfony\\ Contracts\\ Service\\ Attribute\\ SubscribedService `
345
+ attribute::
340
346
341
347
// src/CommandBus.php
342
348
namespace App;
@@ -345,23 +351,25 @@ services::
345
351
use App\CommandHandler\BazHandler;
346
352
use App\CommandHandler\FooHandler;
347
353
use Psr\Container\ContainerInterface;
354
+ use Symfony\Component\DependencyInjection\Attribute\Autowire;
348
355
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
356
+ use Symfony\Contracts\Service\Attribute\SubscribedService;
349
357
350
358
class CommandBus
351
359
{
352
360
public function __construct(
353
- #[AutowireLocator(
354
- fooHandlerAlias: FooHandler::class,
355
- barHandlerAlias: BarHandler::class ,
356
- optionalBazHandlerAlias: '?'.BazHandler::class
357
- )]
358
- private ContainerInterface $locator ,
361
+ #[AutowireLocator([
362
+ 'foo' => FooHandler::class,
363
+ 'bar' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')) ,
364
+ 'optionalBaz' => '?'.BazHandler::class,
365
+ ] )]
366
+ private ContainerInterface $handlers ,
359
367
) {
360
368
}
361
369
362
370
public function handle(Command $command): mixed
363
371
{
364
- $fooHandler = $this->locator ->get('fooHandlerAlias ');
372
+ $fooHandler = $this->handlers ->get('foo ');
365
373
366
374
// ...
367
375
}
@@ -373,6 +381,20 @@ services::
373
381
:class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireLocator `
374
382
attribute was introduced in Symfony 6.4.
375
383
384
+ .. note ::
385
+
386
+ To receive an iterable instead of a service locator, you can switch the
387
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireLocator `
388
+ attribute to
389
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireIterator `
390
+ attribute.
391
+
392
+ .. versionadded :: 6.4
393
+
394
+ The
395
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireIterator `
396
+ attribute was introduced in Symfony 6.4.
397
+
376
398
.. _service-subscribers-locators_defining-service-locator :
377
399
378
400
Defining a Service Locator
0 commit comments