Skip to content

[DependencyInjection] Add #[AutowireLocator] attribute #18775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Dependency Injection
* :ref:`Autowire <autowire-attribute>`
* :ref:`AutowireCallable <autowiring_closures>`
* :doc:`AutowireDecorated </service_container/service_decoration>`
* :ref:`AutowireLocator <service-locator_autowire-locator>`
* :ref:`AutowireServiceClosure <autowiring_closures>`
* :ref:`Exclude <service-psr4-loader>`
* :ref:`TaggedIterator <tags_reference-tagged-services>`
Expand Down
76 changes: 76 additions & 0 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,82 @@ This is done by having ``getSubscribedServices()`` return an array of

The above example requires using ``3.2`` version or newer of ``symfony/service-contracts``.

.. _service-locator_autowire-locator:

The AutowireLocator attribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another way to define a service locator is to use the
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
attribute::

// src/CommandBus.php
namespace App;

use App\CommandHandler\BarHandler;
use App\CommandHandler\FooHandler;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;

class CommandBus
{
public function __construct(
#[AutowireLocator(FooHandler::class, BarHandler::class)]
private ContainerInterface $locator,
) {
}

public function handle(Command $command): mixed
{
$commandClass = get_class($command);

if ($this->locator->has($commandClass)) {
$handler = $this->locator->get($commandClass);

return $handler->handle($command);
}
}
}

Just like with the ``getSubscribedServices()`` method, it is possible
to define aliased services thanks to named arguments, as well as optional
services::

// src/CommandBus.php
namespace App;

use App\CommandHandler\BarHandler;
use App\CommandHandler\BazHandler;
use App\CommandHandler\FooHandler;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;

class CommandBus
{
public function __construct(
#[AutowireLocator(
fooHandlerAlias: FooHandler::class,
barHandlerAlias: BarHandler::class,
optionalBazHandlerAlias: '?'.BazHandler::class
)]
private ContainerInterface $locator,
) {
}

public function handle(Command $command): mixed
{
$fooHandler = $this->locator->get('fooHandlerAlias');

// ...
}
}

.. versionadded:: 6.4

The
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
attribute was introduced in Symfony 6.4.

.. _service-subscribers-locators_defining-service-locator:

Defining a Service Locator
Expand Down