8000 minor #17662 [DependencyInjection] Autowire arguments using the #[Tag… · symfony/symfony-docs@92b0775 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b0775

Browse files
committed
minor #17662 [DependencyInjection] Autowire arguments using the #[TaggedLocator] attribute (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Autowire arguments using the #[TaggedLocator] attribute Partially solves #15238, follow-up of #17652. Commits ------- d072fbd [DependencyInjection] Autowire arguments using the #[TaggedLocator] attribute
2 parents 63da246 + d072fbd commit 92b0775

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,26 @@ As shown in the previous sections, the constructor of the ``CommandBus`` class
305305
must type-hint its argument with ``ContainerInterface``. Then, you can get any of
306306
the service locator services via their ID (e.g. ``$this->locator->get('App\FooCommand')``).
307307

308+
The same behavior can be achieved using the ``#[TaggedLocator]`` attribute. This
309+
attribute must be directly used on a ``ServiceLocator`` argument::
310+
311+
// src/HandlerCollection.php
312+
namespace App;
313+
314+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
315+
use Symfony\Component\DependencyInjection\ServiceLocator;
316+
317+
class HandlerCollection
318+
{
319+
public function __construct(#[TaggedLocator('app.handler')] ServiceLocator $locator)
320+
{
321+
}
322+
}
323+
324+
.. versionadded:: 5.3
325+
326+
The ``#[TaggedLocator]`` attribute was introduced in Symfony 5.3 and requires PHP 8.
327+
308328
Reusing a Service Locator in Multiple Services
309329
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
310330

@@ -459,7 +479,7 @@ will share identical locators among all the services referencing them::
459479
// ...
460480
'logger' => new Reference('logger'),
461481
];
462-
482+
463483
$myService = $container->findDefinition(MyService::class);
464484

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

0 commit comments

Comments
 (0)
0