Skip to content

Commit 62dc9e5

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: [Asset][Lock][RateLimiter] Use the `#[Target]` attribute in some examples [HtmlSanitizer] Use the native HTML5 parser when using PHP 8.4+
2 parents 33c2d1a + 6872b7d commit 62dc9e5

File tree

4 files changed

+136
-19
lines changed

4 files changed

+136
-19
lines changed

html_sanitizer.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ You can install the HTML Sanitizer component with:
2929
3030
$ composer require symfony/html-sanitizer
3131
32+
.. versionadded:: 7.4
33+
34+
Starting in Symfony 7.4, applications running on PHP 8.4 or higher will use
35+
the native HTML5 parser provided by PHP. All other applications will continue
36+
to use the third-party ``masterminds/html5`` parser, which is installed
37+
automatically when installing the HTML Sanitizer package.
38+
3239
Basic Usage
3340
-----------
3441

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
@@ -225,10 +225,63 @@ prevents that number from being higher than 5,000).
225225
Rate Limiting in Action
226226
-----------------------
227227

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

233286
// src/Controller/ApiController.php
234287
namespace App\Controller;
@@ -241,8 +294,8 @@ the number of requests to the API::
241294

242295
class ApiController extends AbstractController
243296
{
244-
// if you're using service autowiring, the variable name must be:
245-
// "rate limiter name" (in camelCase) + "Limiter" suffix
297+
// the argument name here is important; read the previous section about
298+
// how to inject a specific rate limiter service
246299
public function index(Request $request, RateLimiterFactoryInterface $anonymousApiLimiter): Response
247300
{
248301
// 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
@@ -239,6 +239,46 @@ package:
239239

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

242+
After having configured one or more asset packages, you have two ways of injecting
243+
them in any service or controller:
244+
245+
**(1) Use a specific argument name**
246+
247+
Type-hint your construtor/method argument with ``PackageInterface`` and name
248+
the argument using this pattern: "asset package name in camelCase". For example,
249+
to inject the ``foo_package`` package defined earlier::
250+
251+
use Symfony\Component\Asset\PackageInterface;
252+
253+
class SomeService
254+
{
255+
public function __construct(
256+
private PackageInterface $fooPackage
257+
): void {
258+
// ...
259+
}
260+
}
261+
262+
**(2) Use the ``#[Target]`` attribute**
263+
264+
When :ref:`dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type>`
265+
the ``#[Target]`` attribute helps you select which one to inject. Symfony creates
266+
a target called "asset package name" + ``.package`` suffix.
267+
268+
For example, to select the ``foo_package`` package defined earlier::
269+
270+
// ...
271+
use Symfony\Component\DependencyInjection\Attribute\Target;
272+
273+
class SomeService
274+
{
275+
public function __construct(
276+
#[Target('foo_package.package')] private PackageInterface $package
277+
): void {
278+
// ...
279+
}
280+
}
281+
242282
.. _reference-framework-assets-packages:
243283

244284
packages

0 commit comments

Comments
 (0)