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] 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 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>`
* :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 deletionsservice_container/service_subscribers_locators.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp