Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Mar 11, 2023
1 parent c8caf02 commit 53d4fc0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 28 deletions.
Expand Up @@ -60,8 +60,6 @@ abstract class AbstractController implements ServiceSubscriberInterface
*/
protected $container;

private ?HttpHeaderSerializer $httpHeaderSerializer = null;

#[Required]
public function setContainer(ContainerInterface $container): ?ContainerInterface
{
Expand Down Expand Up @@ -96,6 +94,7 @@ public static function getSubscribedServices(): array
'security.token_storage' => '?'.TokenStorageInterface::class,
'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,
'parameter_bag' => '?'.ContainerBagInterface::class,
'web_link.http_header_serializer' => '?'.HttpHeaderSerializer::class,
];
}

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

if (null === $response) {
$response = new Response();
}

if (null === $this->httpHeaderSerializer) {
$this->httpHeaderSerializer = new HttpHeaderSerializer();
}

$response->headers->set('Link', $this->httpHeaderSerializer->serialize($this->populateEarlyHints($links)), false);
$response->sendHeaders(103);

return $response;
}
$response ??= new Response();

private function populateEarlyHints(iterable $links): \Generator
{
$populatedLinks = [];
foreach ($links as $link) {
if ($link instanceof EvolvableLinkInterface && !$link->getRels()) {
$link = $link->withRel('preload');
}

yield $link;
$populatedLinks[] = $link;
}

$response->headers->set('Link', $this->container->get('web_link.http_header_serializer')->serialize($populatedLinks), false);
$response->sendHeaders(103);

return $response;
}
}
Expand Up @@ -12,10 +12,18 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
use Symfony\Component\WebLink\HttpHeaderSerializer;

return static function (ContainerConfigurator $container) {
$container->services()

->set('web_link.http_header_serializer', HttpHeaderSerializer::class)
->alias(HttpHeaderSerializer::class, 'web_link.http_header_serializer')

->set('web_link.add_link_header_listener', AddLinkHeaderListener::class)
->args([
service('web_link.http_header_serializer'),
])
->tag('kernel.event_subscriber')
;
};
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/CHANGELOG.md
Expand Up @@ -113,7 +113,7 @@ CHANGELOG
make sure to run `ALTER TABLE sessions MODIFY sess_lifetime INTEGER UNSIGNED NOT NULL` to
update your database.
* `PdoSessionHandler` now precalculates the expiry timestamp in the lifetime column,
make sure to run `CREATE INDEX EXPIRY ON sessions (sess_lifetime)` to update your database
make sure to run `CREATE INDEX expiry ON sessions (sess_lifetime)` to update your database
to speed up garbage collection of expired sessions.
* added `SessionHandlerFactory` to create session handlers with a DSN
* added `IpUtils::anonymize()` to help with GDPR compliance.
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -331,11 +331,11 @@ public function prepare(Request $request): static
/**
* Sends HTTP headers.
*
* @param null|positive-int $statusCode The status code to use. Override the statusCode property if set and not null.
* @param null|positive-int $statusCode The status code to use, override the statusCode property if set and not null
*
* @return $this
*/
public function sendHeaders(/* ?int $statusCode = null */): static
public function sendHeaders(/* int $statusCode = null */): static
{
// headers have already been sent by the developer
if (headers_sent()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/StreamedResponse.php
Expand Up @@ -59,11 +59,11 @@ public function setCallback(callable $callback): static
/**
* This method only sends the headers once.
*
* @param null|positive-int $statusCode The status code to use. Override the statusCode property if set and not null.
* @param null|positive-int $statusCode The status code to use, override the statusCode property if set and not null
*
* @return $this
*/
public function sendHeaders(/* ?int $statusCode = null */): static
public function sendHeaders(/* int $statusCode = null */): static
{
if ($this->headersSent) {
return $this;
Expand Down
Expand Up @@ -29,11 +29,10 @@ class_exists(HttpHeaderSerializer::class);
*/
class AddLinkHeaderListener implements EventSubscriberInterface
{
private HttpHeaderSerializer $serializer;

public function __construct()
public function __construct(
private readonly HttpHeaderSerializer $serializer = new HttpHeaderSerializer(),
)
{
$this->serializer = new HttpHeaderSerializer();
}

public function onKernelResponse(ResponseEvent $event): void
Expand Down

0 comments on commit 53d4fc0

Please sign in to comment.