@@ -284,25 +284,42 @@ provides :ref:`named lock <reference-lock-resources-name>`:
284
284
;
285
285
};
286
286
287
- An autowiring alias is created for each named lock with a name using the camel
288
- case version of its name suffixed by `` LockFactory ``.
287
+ After having configured one or more named locks, you have two ways of injecting
288
+ them in any service or controller:
289
289
290
- For instance, the ``invoice `` lock can be injected by naming the argument
291
- ``$invoiceLockFactory `` and type-hinting it with
292
- :class: `Symfony\\ Component\\ Lock\\ LockFactory `::
290
+ **(1) Use a specific argument name **
293
291
294
- // src/Controller/PdfController.php
295
- namespace App\Controller;
292
+ Type-hint your construtor/method argument with ``LockFactory `` and name the
293
+ argument using this pattern: "lock name in camelCase" + ``LockFactory `` suffix.
294
+ For example, to inject the ``invoice `` package defined earlier::
296
295
297
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
298
- use Symfony\Component\HttpFoundation\Response;
299
296
use Symfony\Component\Lock\LockFactory;
300
297
301
- class PdfController extends AbstractController
298
+ class SomeService
302
299
{
303
- #[Route('/download/terms-of-use.pdf')]
304
- public function downloadPdf(LockFactory $invoiceLockFactory, MyPdfGeneratorService $pdf): Response
305
- {
300
+ public function __construct(
301
+ private LockFactory $invoiceLockFactory
302
+ ): void {
303
+ // ...
304
+ }
305
+ }
306
+
307
+ **(2) Use the ``#[Target]`` attribute **
308
+
309
+ When :ref: `dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type >`
310
+ the ``#[Target] `` attribute helps you select which one to inject. Symfony creates
311
+ a target called "asset package name" + ``.lock.factory `` suffix.
312
+
313
+ For example, to select the ``invoice `` lock defined earlier::
314
+
315
+ // ...
316
+ use Symfony\Component\DependencyInjection\Attribute\Target;
317
+
318
+ class SomeService
319
+ {
320
+ public function __construct(
321
+ #[Target('invoice.lock.factory')] private LockFactory $invoiceLockFactory
322
+ ): void {
306
323
// ...
307
324
}
308
325
}
0 commit comments