Skip to content

Commit 3a40992

Browse files
committed
fix(laravel): http cache compatibility
1 parent 949c3c9 commit 3a40992

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/HttpCache/State/AddHeadersProcessor.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ final class AddHeadersProcessor implements ProcessorInterface
2929
/**
3030
* @param ProcessorInterface<T1, T2> $decorated
3131
*/
32-
public function __construct(private readonly ProcessorInterface $decorated, private readonly bool $etag = false, private readonly ?int $maxAge = null, private readonly ?int $sharedMaxAge = null, private readonly ?array $vary = null, private readonly ?bool $public = null, private readonly ?int $staleWhileRevalidate = null, private readonly ?int $staleIfError = null)
32+
public function __construct(
33+
private readonly ProcessorInterface $decorated,
34+
private readonly bool $etag = false,
35+
private readonly ?int $maxAge = null,
36+
private readonly ?int $sharedMaxAge = null,
37+
private readonly ?array $vary = null,
38+
private readonly ?bool $public = null,
39+
private readonly ?int $staleWhileRevalidate = null,
40+
private readonly ?int $staleIfError = null,
41+
)
3342
{
3443
}
3544

src/Laravel/ApiPlatformProvider.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use ApiPlatform\Hal\Serializer\EntrypointNormalizer as HalEntrypointNormalizer;
5050
use ApiPlatform\Hal\Serializer\ItemNormalizer as HalItemNormalizer;
5151
use ApiPlatform\Hal\Serializer\ObjectNormalizer as HalObjectNormalizer;
52+
use ApiPlatform\HttpCache\State\AddHeadersProcessor;
5253
use ApiPlatform\Hydra\JsonSchema\SchemaFactory as HydraSchemaFactory;
5354
use ApiPlatform\Hydra\Serializer\CollectionFiltersNormalizer as HydraCollectionFiltersNormalizer;
5455
use ApiPlatform\Hydra\Serializer\CollectionNormalizer as HydraCollectionNormalizer;
@@ -403,8 +404,25 @@ public function register(): void
403404

404405
$this->app->bind(ProviderInterface::class, ContentNegotiationProvider::class);
405406

406-
$this->app->singleton(RespondProcessor::class, function () {
407-
return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer());
407+
$this->app->singleton(RespondProcessor::class, function (Application $app) {
408+
$decorated = new RespondProcessor();
409+
if (class_exists(AddHeadersProcessor::class)) {
410+
/** @var ConfigRepository */
411+
$config = $app['config']['http_cache'] ?? [];
412+
413+
$decorated = new AddHeadersProcessor(
414+
$decorated,
415+
etag: $config['etag'] ?? false,
416+
maxAge: $config['max_age'] ?? null,
417+
sharedMaxAge: $config['shared_max_age'] ?? null,
418+
vary: $config['vary'] ?? null,
419+
public: $config['public'] ?? null,
420+
staleWhileRevalidate: $config['stale_while_revalidate'] ?? null,
421+
staleIfError: $config['stale_if_error']
422+
);
423+
}
424+
425+
return new AddLinkHeaderProcessor($decorated, new HttpHeaderSerializer());
408426
});
409427

410428
$this->app->singleton(SerializeProcessor::class, function (Application $app) {

src/Laravel/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
},
7575
"suggest": {
7676
"api-platform/graphql": "Enable GraphQl support.",
77+
"api-platform/http-cache": "Enable HTTP Cache support.",
7778
"phpstan/phpdoc-parser": "To support extracting metadata from PHPDoc."
7879
},
7980
"extra": {

src/Laravel/config/api-platform.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,25 @@
144144

145145
// we recommend using "file" or "acpu"
146146
'cache' => 'file',
147+
148+
// 'http_cache' => [
149+
// 'etag' => false,
150+
// 'max_age' => null,
151+
// 'shared_max_age' => null,
152+
// 'vary' => null,
153+
// 'public' => null,
154+
// 'stale_while_revalidate' => null,
155+
// 'stale_if_error' => null,
156+
// 'invalidation' => [
157+
// 'varnish_urls' => [],
158+
// 'urls' => [],
159+
// 'scoped_clients' => [],
160+
// 'max_header_length' => 7500,
161+
// 'request_options' => [],
162+
// 'purger' => 'api_platform.http_cache.purger.varnish',
163+
// 'xkey' => [
164+
// 'glue' => ' ',
165+
// ],
166+
// ],
167+
// ]
147168
];

0 commit comments

Comments
 (0)