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] Autowire arguments using the #[TaggedLocator] attribute#17662

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
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
22 changes: 21 additions & 1 deletionservice_container/service_subscribers_locators.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -305,6 +305,26 @@ As shown in the previous sections, the constructor of the ``CommandBus`` class
must type-hint its argument with ``ContainerInterface``. Then, you can get any of
the service locator services via their ID (e.g. ``$this->locator->get('App\FooCommand')``).

The same behavior can be achieved using the ``#[TaggedLocator]`` attribute. This
attribute must be directly used on a ``ServiceLocator`` argument::

// src/HandlerCollection.php
namespace App;

use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\ServiceLocator;

class HandlerCollection
{
public function __construct(#[TaggedLocator('app.handler')] ServiceLocator $locator)
{
}
}

.. versionadded:: 5.3

The ``#[TaggedLocator]`` attribute was introduced in Symfony 5.3 and requires PHP 8.

Reusing a Service Locator in Multiple Services
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand DownExpand Up@@ -459,7 +479,7 @@ will share identical locators among all the services referencing them::
// ...
'logger' => new Reference('logger'),
];

$myService = $container->findDefinition(MyService::class);

$myService->addArgument(ServiceLocatorTagPass::register($container, $locateableServices));
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp