@@ -397,6 +397,75 @@ attribute::
397
397
}
398
398
}
399
399
400
+ Another way to use
401
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireLocator `
402
+ attribute is location class tagged with a specific :doc: `tag </service_container/tags >`
403
+
404
+ Tagging allows you to add classes without having to explicitly list classes in
405
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireLocator `
406
+ attribute
407
+
408
+ It is also possible to use the #[AutoconfigureTag] attribute directly on the base class or interface and all classes implementing this interface will be automatically tagged and included in
409
+ :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ AutowireLocator `
410
+ attribute::
411
+
412
+ // src/CommandBus.php
413
+ namespace App;
414
+
415
+ use Psr\Container\ContainerInterface;
416
+ use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
417
+
418
+ class CommandBus
419
+ {
420
+ public function __construct(
421
+ #[AutowireLocator("command_handler")]
422
+ private ContainerInterface $handlers,
423
+ ) {
424
+ }
425
+
426
+ public function handle(Object $command, string $handlerClassName): mixed
427
+ {
428
+ if ($this->handlers->has($handlerClassName)) {
429
+ $handler = $this->handlers->get($handlerClassName);
430
+
431
+ return $handler->handle($command);
432
+ }
433
+ }
434
+ }
435
+
436
+ // src/CommandHandler/CommandHandlerInterface.php
437
+ namespace App\CommandHandler;
438
+
439
+ use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
440
+
441
+ #[AutoconfigureTag('command_handler')]
442
+ interface CommandHandlerInterface
443
+ {
444
+ public function handle(object $command);
445
+ }
446
+
447
+ // src/CommandHandler/FooHandler.php
448
+ namespace App\CommandHandler;
449
+
450
+ class FooHandler implements CommandHandlerInterface
451
+ {
452
+ public function handle(object $command)
453
+ {
454
+ dump("Foo", $command);
455
+ }
456
+ }
457
+
458
+ // src/CommandHandler/BarHandler.php
459
+ namespace App\CommandHandler;
460
+
461
+ class BarHandler implements CommandHandlerInterface
462
+ {
463
+ public function handle(object $command)
464
+ {
465
+ dump("Bar", $command);
466
+ }
467
+ }
468
+
400
469
.. _service-locator_autowire-iterator :
401
470
402
471
The AutowireIterator Attribute
0 commit comments