Skip to content

Commit 5d24590

Browse files
committed
[Asset][Lock][RateLimiter] Use the #[Target] attribute in some examples
1 parent 002e105 commit 5d24590

File tree

3 files changed

+129
-19
lines changed

3 files changed

+129
-19
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 $lockFactory
322+
): void {
306323
// ...
307324
}
308325
}

rate_limiter.rst

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,63 @@ prevents that number from being higher than 5,000).
219219
Rate Limiting in Action
220220
-----------------------
221221

222-
After having installed and configured the rate limiter, inject it in any service
223-
or controller and call the ``consume()`` method to try to consume a given number
224-
of tokens. For example, this controller uses the previous rate limiter to control
225-
the number of requests to the API::
222+
Injecting the Rate Limiter Service
223+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224+
225+
After having configured one or more rate limiters, you have two ways of injecting
226+
them in any service or controller:
227+
228+
**(1) Use a specific argument name**
229+
230+
Type-hint your construtor/method argument with ``RateLimiterFactory`` and name
231+
the argument using this pattern: "rate limiter name in camelCase" + ``Limiter`` suffix.
232+
For example, to inject the ``anonymous_api`` limiter defined earlier, use an
233+
argument named ``$anonymousApiLimiter``::
234+
235+
// src/Controller/ApiController.php
236+
namespace App\Controller;
237+
238+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
239+
use Symfony\Component\HttpFoundation\Response;
240+
use Symfony\Component\RateLimiter\RateLimiterFactory;
241+
242+
class ApiController extends AbstractController
243+
{
244+
public function index(RateLimiterFactory $anonymousApiLimiter): Response
245+
{
246+
// ...
247+
}
248+
}
249+
250+
**(2) Use the ``#[Target]`` attribute**
251+
252+
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
253+
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
254+
a target called "rate limiter name" + ``.limiter`` suffix.
255+
256+
For example, to select the ``anonymous_api`` limiter defined earlier, use
257+
``anonymous_api.limiter`` as the target::
258+
259+
// ...
260+
use Symfony\Component\DependencyInjection\Attribute\Target;
261+
262+
class ApiController extends AbstractController
263+
{
264+
public function index(
265+
#[Target('anonymous_api.limiter')] RateLimiterFactory $rateLimiter
266+
): Response
267+
{
268+
// ...
269+
}
270+
}
271+
272+
Using the Rate Limiter Service
273+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
274+
275+
After having injected the rate limiter in any service or controller, call the
276+
``consume()`` method to try to consume a given number of tokens. For example,
277+
this controller uses the previous rate limiter to control the number of requests
278+
to the API::
226279

227280
// src/Controller/ApiController.php
228281
namespace App\Controller;
@@ -235,8 +288,8 @@ the number of requests to the API::
235288

236289
class ApiController extends AbstractController
237290
{
238-
// if you're using service autowiring, the variable name must be:
239-
// "rate limiter name" (in camelCase) + "Limiter" suffix
291+
// the argument name here is important; read the previous section about
292+
// how to inject a specific rate limiter service
240293
public function index(Request $request, RateLimiterFactory $anonymousApiLimiter): Response
241294
{
242295
// create a limiter based on a unique identifier of the client

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 $package
2584+
): void {
2585+
// ...
2586+
}
2587+
}
2588+
25492589
.. _reference-assets-strict-mode:
25502590

25512591
strict_mode

0 commit comments

Comments
 (0)