Skip to content

Commit aa27552

Browse files
committed
Add more Target examples
1 parent 74d43d7 commit aa27552

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

lock.rst

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,42 @@ provides :ref:`named lock <reference-lock-resources-name>`:
284284
;
285285
};
286286
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:
289289

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**
293291

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::
296295

297-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
298-
use Symfony\Component\HttpFoundation\Response;
299296
use Symfony\Component\Lock\LockFactory;
300297

301-
class PdfController extends AbstractController
298+
class SomeService
302299
{
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 {
306323
// ...
307324
}
308325
}

reference/configuration/framework.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,46 @@ package:
25462546

25472547
If a URL is set, the JSON manifest is downloaded on each request using the `http_client`_.
25482548

2549+
After having configured one or more asset packages, you have two ways of injecting
2550+
them in any service or controller:
2551+
2552+
**(1) Use a specific argument name**
2553+
2554+
Type-hint your construtor/method argument with ``PackageInterface`` and name
2555+
the argument using this pattern: "asset package name in camelCase". For example,
2556+
to inject the ``foo_package`` package defined earlier::
2557+
2558+
use Symfony\Component\Asset\PackageInterface;
2559+
2560+
class SomeService
2561+
{
2562+
public function __construct(
2563+
private PackageInterface $fooPackage
2564+
): void {
2565+
// ...
2566+
}
2567+
}
2568+
2569+
**(2) Use the ``#[Target]`` attribute**
2570+
2571+
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
2572+
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
2573+
a target called "asset package name" + ``.package`` suffix.
2574+
2575+
For example, to select the ``foo_package`` package defined earlier::
2576+
2577+
// ...
2578+
use Symfony\Component\DependencyInjection\Attribute\Target;
2579+
2580+
class SomeService
2581+
{
2582+
public function __construct(
2583+
#[Target('foo_package.package')] private PackageInterface $fooPackage
2584+
): void {
2585+
// ...
2586+
}
2587+
}
2588+
25492589
.. _reference-assets-strict-mode:
25502590

25512591
strict_mode

0 commit comments

Comments
 (0)