Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53d4fc0

Browse files
committed
review
1 parent c8caf02 commit 53d4fc0

File tree

6 files changed

+26
-28
lines changed

6 files changed

+26
-28
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ abstract class AbstractController implements ServiceSubscriberInterface
6060
*/
6161
protected $container;
6262

63-
private ?HttpHeaderSerializer $httpHeaderSerializer = null;
64-
6563
#[Required]
6664
public function setContainer(ContainerInterface $container): ?ContainerInterface
6765
{
@@ -96,6 +94,7 @@ public static function getSubscribedServices(): array
9694
'security.token_storage' => '?'.TokenStorageInterface::class,
9795
'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,
9896
'parameter_bag' => '?'.ContainerBagInterface::class,
97+
'web_link.http_header_serializer' => '?'.HttpHeaderSerializer::class,
9998
];
10099
}
101100

@@ -412,32 +411,24 @@ protected function addLink(Request $request, LinkInterface $link): void
412411
*/
413412
protected function sendEarlyHints(iterable $links, Response $response = null): Response
414413
{
415-
if (!class_exists(HttpHeaderSerializer::class)) {
414+
if (!$this->container->has('web_link.http_header_serializer')) {
416415
throw new \LogicException('You cannot use the "sendEarlyHints" method if the WebLink component is not available. Try running "composer require symfony/web-link".');
417416
}
418417

419-
if (null === $response) {
420-
$response = new Response();
421-
}
422-
423-
if (null === $this->httpHeaderSerializer) {
424-
$this->httpHeaderSerializer = new HttpHeaderSerializer();
425-
}
426-
427-
$response->headers->set('Link', $this->httpHeaderSerializer->serialize($this->populateEarlyHints($links)), false);
428-
$response->sendHeaders(103);
429-
430-
return $response;
431-
}
418+
$response ??= new Response();
432419

433-
private function populateEarlyHints(iterable $links): \Generator
434-
{
420+
$populatedLinks = [];
435421
foreach ($links as $link) {
436422
if ($link instanceof EvolvableLinkInterface && !$link->getRels()) {
437423
$link = $link->withRel('preload');
438424
}
439425

440-
yield $link;
426+
$populatedLinks[] = $link;
441427
}
428+
429+
$response->headers->set('Link', $this->container->get('web_link.http_header_serializer')->serialize($populatedLinks), false);
430+
$response->sendHeaders(103);
431+
432+
return $response;
442433
}
443434
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/web_link.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
15+
use Symfony\Component\WebLink\HttpHeaderSerializer;
1516

1617
return static function (ContainerConfigurator $container) {
1718
$container->services()
19+
20+
->set('web_link.http_header_serializer', HttpHeaderSerializer::class)
21+
->alias(HttpHeaderSerializer::class, 'web_link.http_header_serializer')
22+
1823
->set('web_link.add_link_header_listener', AddLinkHeaderListener::class)
24+
->args([
25+
service('web_link.http_header_serializer'),
26+
])
1927
->tag('kernel.event_subscriber')
2028
;
2129
};

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ CHANGELOG
113113
make sure to run `ALTER TABLE sessions MODIFY sess_lifetime INTEGER UNSIGNED NOT NULL` to
114114
update your database.
115115
* `PdoSessionHandler` now precalculates the expiry timestamp in the lifetime column,
116-
make sure to run `CREATE INDEX EXPIRY ON sessions (sess_lifetime)` to update your database
116+
make sure to run `CREATE INDEX expiry ON sessions (sess_lifetime)` to update your database
117117
to speed up garbage collection of expired sessions.
118118
* added `SessionHandlerFactory` to create session handlers with a DSN
119119
* added `IpUtils::anonymize()` to help with GDPR compliance.

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ public function prepare(Request $request): static
331331
/**
332332
* Sends HTTP headers.
333333
*
334-
* @param null|positive-int $statusCode The status code to use. Override the statusCode property if set and not null.
334+
* @param null|positive-int $statusCode The status code to use, override the statusCode property if set and not null
335335
*
336336
* @return $this
337337
*/
338-
public function sendHeaders(/* ?int $statusCode = null */): static
338+
public function sendHeaders(/* int $statusCode = null */): static
339339
{
340340
// headers have already been sent by the developer
341341
if (headers_sent()) {

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public function setCallback(callable $callback): static
5959
/**
6060
* This method only sends the headers once.
6161
*
62-
* @param null|positive-int $statusCode The status code to use. Override the statusCode property if set and not null.
62+
* @param null|positive-int $statusCode The status code to use, override the statusCode property if set and not null
6363
*
6464
* @return $this
6565
*/
66-
public function sendHeaders(/* ?int $statusCode = null */): static
66+
public function sendHeaders(/* int $statusCode = null */): static
6767
{
6868
if ($this->headersSent) {
6969
return $this;

src/Symfony/Component/WebLink/EventListener/AddLinkHeaderListener.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ class_exists(HttpHeaderSerializer::class);
2929
*/
3030
class AddLinkHeaderListener implements EventSubscriberInterface
3131
{
32-
private HttpHeaderSerializer $serializer;
33-
34-
public function __construct()
32+
public function __construct(
33+
private readonly HttpHeaderSerializer $serializer = new HttpHeaderSerializer(),
34+
)
3535
{
36-
$this->serializer = new HttpHeaderSerializer();
3736
}
3837

3938
public function onKernelResponse(ResponseEvent $event): void

0 commit comments

Comments
 (0)