Skip to content

Commit 50acd66

Browse files
committed
Merge branch '6.4' into 7.3
* 6.4: [Asset][Lock][RateLimiter] Use the `#[Target]` attribute in some examples
2 parents 30acc00 + c251130 commit 50acd66

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
@@ -291,25 +291,42 @@ provides :ref:`named lock <reference-lock-resources-name>`:
291291
;
292292
};
293293
294-
An autowiring alias is created for each named lock with a name using the camel
295-
case version of its name suffixed by ``LockFactory``.
294+
After having configured one or more named locks, you have two ways of injecting
295+
them in any service or controller:
296296

297-
For instance, the ``invoice`` lock can be injected by naming the argument
298-
``$invoiceLockFactory`` and type-hinting it with
299-
:class:`Symfony\\Component\\Lock\\LockFactory`::
297+
**(1) Use a specific argument name**
300298

301-
// src/Controller/PdfController.php
302-
namespace App\Controller;
299+
Type-hint your construtor/method argument with ``LockFactory`` and name the
300+
argument using this pattern: "lock name in camelCase" + ``LockFactory`` suffix.
301+
For example, to inject the ``invoice`` package defined earlier::
303302

304-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
305-
use Symfony\Component\HttpFoundation\Response;
306303
use Symfony\Component\Lock\LockFactory;
307304

308-
class PdfController extends AbstractController
305+
class SomeService
309306
{
310-
#[Route('/download/terms-of-use.pdf')]
311-
public function downloadPdf(LockFactory $invoiceLockFactory, MyPdfGeneratorService $pdf): Response
312-
{
307+
public function __construct(
308+
private LockFactory $invoiceLockFactory
309+
): void {
310+
// ...
311+
}
312+
}
313+
314+
**(2) Use the ``#[Target]`` attribute**
315+
316+
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
317+
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
318+
a target called "asset package name" + ``.lock.factory`` suffix.
319+
320+
For example, to select the ``invoice`` lock defined earlier::
321+
322+
// ...
323+
use Symfony\Component\DependencyInjection\Attribute\Target;
324+
325+
class SomeService
326+
{
327+
public function __construct(
328+
#[Target('invoice.lock.factory')] private LockFactory $lockFactory
329+
): void {
313330
// ...
314331
}
315332
}

rate_limiter.rst

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,69 @@ prevents that number from being higher than 5,000).
230230
Rate Limiting in Action
231231
-----------------------
232232

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

235285
:class:`Symfony\\Component\\RateLimiter\\RateLimiterFactoryInterface` was
236286
added and should now be used for autowiring instead of
237287
:class:`Symfony\\Component\\RateLimiter\\RateLimiterFactory`.
238288

239-
After having installed and configured the rate limiter, inject it in any service
240-
or controller and call the ``consume()`` method to try to consume a given number
241-
of tokens. For example, this controller uses the previous rate limiter to control
242-
the number of requests to the API::
289+
Using the Rate Limiter Service
290+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291+
292+
After having injected the rate limiter in any service or controller, call the
293+
``consume()`` method to try to consume a given number of tokens. For example,
294+
this controller uses the previous rate limiter to control the number of requests
295+
to the API::
243296

244297
// src/Controller/ApiController.php
245298
namespace App\Controller;
@@ -252,8 +305,8 @@ the number of requests to the API::
252305

253306
class ApiController extends AbstractController
254307
{
255-
// if you're using service autowiring, the variable name must be:
256-
// "rate limiter name" (in camelCase) + "Limiter" suffix
308+
// the argument name here is important; read the previous section about
309+
// how to inject a specific rate limiter service
257310
public function index(Request $request, RateLimiterFactoryInterface $anonymousApiLimiter): Response
258311
{
259312
// 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
@@ -277,6 +277,46 @@ package:
277277

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

280+
After having configured one or more asset packages, you have two ways of injecting
281+
them in any service or controller:
282+
283+
**(1) Use a specific argument name**
284+
285+
Type-hint your construtor/method argument with ``PackageInterface`` and name
286+
the argument using this pattern: "asset package name in camelCase". For example,
287+
to inject the ``foo_package`` package defined earlier::
288+
289+
use Symfony\Component\Asset\PackageInterface;
290+
291+
class SomeService
292+
{
293+
public function __construct(
294+
private PackageInterface $fooPackage
295+
): void {
296+
// ...
297+
}
298+
}
299+
300+
**(2) Use the ``#[Target]`` attribute**
301+
302+
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
303+
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
304+
a target called "asset package name" + ``.package`` suffix.
305+
306+
For example, to select the ``foo_package`` package defined earlier::
307+
308+
// ...
309+
use Symfony\Component\DependencyInjection\Attribute\Target;
310+
311+
class SomeService
312+
{
313+
public function __construct(
314+
#[Target('foo_package.package')] private PackageInterface $package
315+
): void {
316+
// ...
317+
}
318+
}
319+
280320
.. _reference-framework-assets-packages:
281321

282322
packages

0 commit comments

Comments
 (0)