-
-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathCacheableDocumentationProcessor.php
More file actions
93 lines (75 loc) · 2.66 KB
/
CacheableDocumentationProcessor.php
File metadata and controls
93 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\State\Processor;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\StopwatchAwareInterface;
use ApiPlatform\State\StopwatchAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Adds an ETag and revalidating Cache-Control headers on the API documentation
* and entrypoint responses so clients can avoid re-downloading the (often large)
* payload when nothing changed.
*
* @template T1
* @template T2
*
* @implements ProcessorInterface<T1, T2>
*/
final class CacheableDocumentationProcessor implements ProcessorInterface, StopwatchAwareInterface
{
use StopwatchAwareTrait;
/**
* @param ProcessorInterface<T1, T2> $decorated
*/
public function __construct(
private readonly ProcessorInterface $decorated,
private readonly int $maxAge = 0,
private readonly ?int $sharedMaxAge = null,
private readonly bool $public = true,
private readonly bool $mustRevalidate = true,
private readonly bool $etag = true,
) {
}
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
$response = $this->decorated->process($data, $operation, $uriVariables, $context);
if (!$response instanceof Response || 200 !== $response->getStatusCode()) {
return $response;
}
$content = $response->getContent();
if (false === $content || '' === $content) {
return $response;
}
$this->stopwatch?->start('api_platform.processor.cacheable_documentation');
if ($this->etag) {
$response->setEtag(md5($content));
}
if ($this->public) {
$response->setPublic();
} else {
$response->setPrivate();
}
$response->setMaxAge($this->maxAge);
if (null !== $this->sharedMaxAge) {
$response->setSharedMaxAge($this->sharedMaxAge);
}
if ($this->mustRevalidate) {
$response->headers->addCacheControlDirective('must-revalidate');
}
if ($this->etag && ($request = $context['request'] ?? null) instanceof Request) {
$response->isNotModified($request);
}
$this->stopwatch?->stop('api_platform.processor.cacheable_documentation');
return $response;
}
}