Skip to content

[DependencyInjection] Update syntax for #[AutowireLocator] and add #[AutowireIterator] #18993

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
Oct 10, 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
[DependencyInjection] Update syntax for #[AutowireLocator] and add …
…`#[AutowireIterator]`
  • Loading branch information
OskarStark committed Oct 10, 2023
commit 528cbda9f2934ed5d9624d27c6d62a10c712a8e6
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>`
* :doc:`AutowireIterator <service-locator_autowire-iterator>`
* :ref:`AutowireLocator <service-locator_autowire-locator>`
* :ref:`AutowireServiceClosure <autowiring_closures>`
* :ref:`Exclude <service-psr4-loader>`
Expand Down
52 changes: 37 additions & 15 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ 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:
.. _service-locator_autowire-iterator:

The AutowireLocator attribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The AutowireLocator and AutowireIterator Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another way to define a service locator is to use the
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
Expand All @@ -317,26 +318,31 @@ attribute::
class CommandBus
{
public function __construct(
#[AutowireLocator(FooHandler::class, BarHandler::class)]
private ContainerInterface $locator,
#[AutowireLocator([
FooHandler::class,
BarHandler::class,
])]
private ContainerInterface $handlers,
) {
}

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

if ($this->locator->has($commandClass)) {
$handler = $this->locator->get($commandClass);
if ($this->handlers->has($commandClass)) {
$handler = $this->handlers->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::
to define aliased services thanks to the array keys, as well as optional
services, plus you can nest it with
:class:`Symfony\\Contracts\\Service\\Attribute\\SubscribedService`
attribute::

// src/CommandBus.php
namespace App;
Expand All @@ -345,23 +351,25 @@ services::
use App\CommandHandler\BazHandler;
use App\CommandHandler\FooHandler;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Contracts\Service\Attribute\SubscribedService;

class CommandBus
{
public function __construct(
#[AutowireLocator(
fooHandlerAlias: FooHandler::class,
barHandlerAlias: BarHandler::class,
optionalBazHandlerAlias: '?'.BazHandler::class
)]
private ContainerInterface $locator,
#[AutowireLocator([
'foo' => FooHandler::class,
'bar' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')),
'optionalBaz' => '?'.BazHandler::class,
])]
private ContainerInterface $handlers,
) {
}

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

// ...
}
Expand All @@ -373,6 +381,20 @@ services::
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
attribute was introduced in Symfony 6.4.

.. note::

To receive an iterable instead of a service locator, you can switch the
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
attribute to
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireIterator`
attribute.

.. versionadded:: 6.4

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

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

Defining a Service Locator
Expand Down