Skip to content

Enabled Strict types and improved type hinting #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/Data/LinkHeaders.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JustBetter\Http3EarlyHints\Data;

use Fig\Link\GenericLinkProvider;
Expand Down Expand Up @@ -30,7 +32,7 @@ public function setLinkProvider(EvolvableLinkProviderInterface $linkProvider): s
return $this;
}

public function addLink(EvolvableLinkInterface|string|array $uri, string|array|null $rel = null, ?array $attributes = []): static
public function addLink(EvolvableLinkInterface|string|array $uri, string|array|null $rel = null, array $attributes = []): static
{
if (is_array($uri)) {
foreach ($uri as $data) {
Expand Down Expand Up @@ -71,9 +73,9 @@ public function addLink(EvolvableLinkInterface|string|array $uri, string|array|n

public function addFromString(string $link): static
{
$links = explode(',', trim($link));
foreach ($links as $link) {
$parts = explode('; ', trim($link));
$explodedLinks = explode(',', trim($link));
foreach ($explodedLinks as $explodedLink) {
$parts = explode('; ', trim($explodedLink));
$uri = trim(array_shift($parts), '<>');
$rel = null;
$attributes = [];
Expand All @@ -96,14 +98,14 @@ public function addFromString(string $link): static
return $this;
}

public function makeUnique()
public function makeUnique(): static
{
$handledHashes = [];

foreach ($this->getLinkProvider()->getLinks() as $link) {
/** @var Link $link */
$hash = md5($link->getHref(), serialize($link->getRels()));
if (! in_array($hash, $handledHashes)) {
$hash = md5($link->getHref().serialize($link->getRels()));
if (! in_array($hash, $handledHashes, true)) {
$handledHashes[] = $hash;

continue;
Expand All @@ -115,18 +117,18 @@ public function makeUnique()
return $this;
}

public function __toString()
public function __toString(): string
{
return trim(collect($this->getLinkProvider()->getLinks())
->map([static::class, 'linkToString'])
->filter()
->implode(','));
}

public static function linkToString(LinkInterface $link)
public static function linkToString(LinkInterface $link): ?string
{
if ($link->isTemplated()) {
return;
return null;
}

$attributes = ['', sprintf('rel="%s"', implode(' ', $link->getRels()))];
Expand Down
2 changes: 2 additions & 0 deletions src/Events/GenerateEarlyHints.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JustBetter\Http3EarlyHints\Events;

use Illuminate\Foundation\Events\Dispatchable;
Expand Down
6 changes: 4 additions & 2 deletions src/Listeners/AddDefaultHeaders.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

declare(strict_types=1);

namespace JustBetter\Http3EarlyHints\Listeners;

use Illuminate\Support\Facades\Event;
use JustBetter\Http3EarlyHints\Events\GenerateEarlyHints;

class AddDefaultHeaders
{
public function handle(GenerateEarlyHints $event)
public function handle(GenerateEarlyHints $event): void
{
foreach (config('http3earlyhints.default_headers', []) as $header) {
$event->linkHeaders->addFromString($header);
}
}

public static function register()
public static function register(): void
{
Event::listen(GenerateEarlyHints::class, static::class);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Listeners/AddFromBody.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JustBetter\Http3EarlyHints\Listeners;

use Fig\Link\Link;
Expand Down
24 changes: 14 additions & 10 deletions src/Middleware/AddHttp3EarlyHints.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JustBetter\Http3EarlyHints\Middleware;

use Closure;
Expand All @@ -26,7 +28,10 @@ public function handle(Request $request, Closure $next, ?int $sizeLimit = null):
$lastPath = Str::afterLast($request->path(), '/');
if (
$request->format() !== 'html'
|| (str_contains($lastPath, '.') && ! in_array(Str::afterLast($lastPath, '.'), config('http3earlyhints.extensions', ['', 'php', 'html'])))
|| (
str_contains($lastPath, '.')
&& ! in_array(Str::afterLast($lastPath, '.'), config('http3earlyhints.extensions', ['', 'php', 'html']), true)
)
) {
$this->skipCurrentRequest = true;

Expand Down Expand Up @@ -84,15 +89,15 @@ public function terminate(Request $request, SymfonyResponse $response): void
$this->handleGeneratingLinkHeaders($request, $response);
}

public function handleGeneratingLinkHeaders(Request $request, SymfonyResponse $response)
public function handleGeneratingLinkHeaders(Request $request, SymfonyResponse $response): ?LinkHeaders
{
if (
! $response instanceof Response
$this->skipCurrentRequest
|| ! $response instanceof Response
|| $response->isRedirection()
|| ! $response->isSuccessful()
|| $this->skipCurrentRequest
) {
return;
return null;
}
$linkHeaders = $this->generateLinkHeaders($request, $response, $this->sizeLimit);

Expand All @@ -112,7 +117,7 @@ protected function generateLinkHeaders(Request $request, SymfonyResponse $respon

$this->linkHeaders->makeUnique();

$sizeLimit = $sizeLimit ?? max(1, intval(config('http3earlyhints.size_limit', 32 * 1024)));
$sizeLimit = $sizeLimit ?? max(1, (int) config('http3earlyhints.size_limit', 32 * 1024));
$headersText = $this->linkHeaders->__toString();

while (strlen($headersText) > $sizeLimit) {
Expand All @@ -126,18 +131,17 @@ protected function generateLinkHeaders(Request $request, SymfonyResponse $respon
/**
* Add Link Header
*/
private function addLinkHeaders(SymfonyResponse $response, LinkHeaders $linkHeaders): SymfonyResponse
private function addLinkHeaders(SymfonyResponse $response, LinkHeaders $linkHeaders): void
{
$link = $linkHeaders->__toString();
if (! $link || ! $response instanceof Response) {
return $response;
return;
}

if ($response->headers->get('Link')) {
$link = $response->headers->get('Link').','.$link;
}

$response->header('Link', $link);

return $response;
}
}
6 changes: 3 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JustBetter\Http3EarlyHints;

use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
Expand All @@ -10,10 +12,8 @@ class ServiceProvider extends LaravelServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->mergeConfigFrom(__DIR__.'/config.php', 'http3earlyhints');

Expand Down
2 changes: 2 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return [
/**
* This works by building a cache of links after the first request, how long should we keep them for in seconds?
Expand Down
Loading
Loading