From 90fb1b8b7db8ecebae3612a0ada6e36e8e26ff8e Mon Sep 17 00:00:00 2001 From: ay-git Date: Sat, 3 May 2025 14:30:28 +0300 Subject: [PATCH 1/2] [AutowireLocator] Using #[AutowireLocator] with tagged class Add an example of using #[AutowireLocator] with a tagged class and tagging with #[AutoconfigureTag]. --- .../service_subscribers_locators.rst | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index 2c057067927..15a5b5bb9ae 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -397,6 +397,75 @@ attribute:: } } +Another way to use +:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` +attribute is location class tagged with a specific :doc:`tag ` + +Tagging allows you to add classes without having to explicitly list classes in +:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` +attribute + +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 +:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` +attribute:: + + // src/CommandBus.php + namespace App; + + use Psr\Container\ContainerInterface; + use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; + + class CommandBus + { + public function __construct( + #[AutowireLocator("command_handler")] + private ContainerInterface $handlers, + ) { + } + + public function handle(Object $command, string $handlerClassName): mixed + { + if ($this->handlers->has($handlerClassName)) { + $handler = $this->handlers->get($handlerClassName); + + return $handler->handle($command); + } + } + } + + // src/CommandHandler/CommandHandlerInterface.php + namespace App\CommandHandler; + + use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; + + #[AutoconfigureTag('command_handler')] + interface CommandHandlerInterface + { + public function handle(object $command); + } + + // src/CommandHandler/FooHandler.php + namespace App\CommandHandler; + + class FooHandler implements CommandHandlerInterface + { + public function handle(object $command) + { + dump("Foo", $command); + } + } + + // src/CommandHandler/BarHandler.php + namespace App\CommandHandler; + + class BarHandler implements CommandHandlerInterface + { + public function handle(object $command) + { + dump("Bar", $command); + } + } + .. _service-locator_autowire-iterator: The AutowireIterator Attribute From f97d7d466ffd77bed4ea9ef1ef6a07b4deb02a1e Mon Sep 17 00:00:00 2001 From: ay-git Date: Sat, 3 May 2025 14:55:38 +0300 Subject: [PATCH 2/2] Update service_subscribers_locators.rst --- service_container/service_subscribers_locators.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/service_container/service_subscribers_locators.rst b/service_container/service_subscribers_locators.rst index 15a5b5bb9ae..86e51683516 100644 --- a/service_container/service_subscribers_locators.rst +++ b/service_container/service_subscribers_locators.rst @@ -397,15 +397,17 @@ attribute:: } } -Another way to use +Using AutowireLocator with tagged class + +Another way to use :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` attribute is location class tagged with a specific :doc:`tag ` -Tagging allows you to add classes without having to explicitly list classes in +Tagging allows you to add classes without having to explicitly list classes in :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` attribute -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 +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 :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator` attribute:: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy