Skip to content

Commit f2c930f

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: [DependencyInjection] Update syntax for `#[AutowireLocator]` and add `#[AutowireIterator]`
2 parents d7ec6b2 + 4918230 commit f2c930f

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

reference/attributes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Dependency Injection
3333
* :ref:`Autowire <autowire-attribute>`
3434
* :ref:`AutowireCallable <autowiring_closures>`
3535
* :doc:`AutowireDecorated </service_container/service_decoration>`
36+
* :doc:`AutowireIterator <service-locator_autowire-iterator>`
3637
* :ref:`AutowireLocator <service-locator_autowire-locator>`
3738
* :ref:`AutowireServiceClosure <autowiring_closures>`
3839
* :ref:`Exclude <service-psr4-loader>`

service_container/service_subscribers_locators.rst

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,10 @@ This is done by having ``getSubscribedServices()`` return an array of
298298
The above example requires using ``3.2`` version or newer of ``symfony/service-contracts``.
299299

300300
.. _service-locator_autowire-locator:
301+
.. _service-locator_autowire-iterator:
301302

302-
The AutowireLocator attribute
303-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303+
The AutowireLocator and AutowireIterator Attributes
304+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304305

305306
Another way to define a service locator is to use the
306307
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
@@ -317,26 +318,31 @@ attribute::
317318
class CommandBus
318319
{
319320
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,
322326
) {
323327
}
324328

325329
public function handle(Command $command): mixed
326330
{
327331
$commandClass = get_class($command);
328332

329-
if ($this->locator->has($commandClass)) {
330-
$handler = $this->locator->get($commandClass);
333+
if ($this->handlers->has($commandClass)) {
334+
$handler = $this->handlers->get($commandClass);
331335

332336
return $handler->handle($command);
333337
}
334338
}
335339
}
336340

337341
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::
340346

341347
// src/CommandBus.php
342348
namespace App;
@@ -345,23 +351,25 @@ services::
345351
use App\CommandHandler\BazHandler;
346352
use App\CommandHandler\FooHandler;
347353
use Psr\Container\ContainerInterface;
354+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
348355
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
356+
use Symfony\Contracts\Service\Attribute\SubscribedService;
349357

350358
class CommandBus
351359
{
352360
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,
359367
) {
360368
}
361369

362370
public function handle(Command $command): mixed
363371
{
364-
$fooHandler = $this->locator->get('fooHandlerAlias');
372+
$fooHandler = $this->handlers->get('foo');
365373

366374
// ...
367375
}
@@ -373,6 +381,20 @@ services::
373381
:class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator`
374382
attribute was introduced in Symfony 6.4.
375383

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+
376398
.. _service-subscribers-locators_defining-service-locator:
377399

378400
Defining a Service Locator

0 commit comments

Comments
 (0)