Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsreference/attributes.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsservice_container/service_subscribers_locators.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 AutowireLocatorattribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The AutowireLocatorand 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

[8]ページ先頭

©2009-2025 Movatter.jp