|
20 | 20 | *
|
21 | 21 | * @author Kévin Dunglas <[email protected]>
|
22 | 22 | */
|
23 |
| -final class VarnishXKeyPurger implements PurgerInterface |
| 23 | +final class VarnishXKeyPurger extends SurrogateKeysPurger |
24 | 24 | {
|
25 | 25 | private const VARNISH_MAX_HEADER_LENGTH = 8000;
|
| 26 | + private const VARNISH_SEPARATOR = ' '; |
26 | 27 |
|
27 | 28 | /**
|
28 | 29 | * @param HttpClientInterface[] $clients
|
29 | 30 | */
|
30 |
| - public function __construct(private readonly array $clients, private readonly int $maxHeaderLength = self::VARNISH_MAX_HEADER_LENGTH, private readonly string $xkeyGlue = ' ') |
| 31 | + public function __construct(array $clients, int $maxHeaderLength = self::VARNISH_MAX_HEADER_LENGTH, string $xkeyGlue = self::VARNISH_SEPARATOR) |
31 | 32 | {
|
32 |
| - } |
33 |
| - |
34 |
| - /** |
35 |
| - * {@inheritdoc} |
36 |
| - */ |
37 |
| - public function purge(array $iris): void |
38 |
| - { |
39 |
| - if (!$iris) { |
40 |
| - return; |
41 |
| - } |
42 |
| - |
43 |
| - $irisChunks = array_chunk($iris, \count($iris)); |
44 |
| - |
45 |
| - foreach ($irisChunks as $irisChunk) { |
46 |
| - $this->purgeIris($irisChunk); |
47 |
| - } |
48 |
| - } |
49 |
| - |
50 |
| - /** |
51 |
| - * {@inheritdoc} |
52 |
| - */ |
53 |
| - public function getResponseHeaders(array $iris): array |
54 |
| - { |
55 |
| - return ['xkey' => implode($this->xkeyGlue, $iris)]; |
56 |
| - } |
57 |
| - |
58 |
| - private function purgeIris(array $iris): void |
59 |
| - { |
60 |
| - foreach ($this->chunkKeys($iris) as $keys) { |
61 |
| - $this->purgeKeys($keys); |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - private function purgeKeys(string $keys): void |
66 |
| - { |
67 |
| - foreach ($this->clients as $client) { |
68 |
| - $client->request('PURGE', '', ['headers' => ['xkey' => $keys]]); |
69 |
| - } |
70 |
| - } |
71 |
| - |
72 |
| - private function chunkKeys(array $keys): iterable |
73 |
| - { |
74 |
| - $concatenatedKeys = implode($this->xkeyGlue, $keys); |
75 |
| - |
76 |
| - // If all keys fit in the header, we can return them |
77 |
| - if (\strlen($concatenatedKeys) <= $this->maxHeaderLength) { |
78 |
| - yield $concatenatedKeys; |
79 |
| - |
80 |
| - return; |
81 |
| - } |
82 |
| - |
83 |
| - $currentHeader = ''; |
84 |
| - |
85 |
| - foreach ($keys as $position => $key) { |
86 |
| - if (\strlen((string) $key) > $this->maxHeaderLength) { |
87 |
| - throw new \Exception(sprintf('IRI "%s" is too long to fit current max header length (currently set to "%s"). You can increase it using the "api_platform.http_cache.invalidation.max_header_length" parameter.', $key, $this->maxHeaderLength)); |
88 |
| - } |
89 |
| - |
90 |
| - $headerCandidate = sprintf('%s%s%s', $currentHeader, $position > 0 ? $this->xkeyGlue : '', $key); |
91 |
| - |
92 |
| - if (\strlen($headerCandidate) > $this->maxHeaderLength) { |
93 |
| - $nextKeys = \array_slice($keys, $position, \count($keys) - $position); |
94 |
| - |
95 |
| - yield $currentHeader; |
96 |
| - yield from $this->chunkKeys($nextKeys); |
97 |
| - |
98 |
| - break; |
99 |
| - } |
100 |
| - |
101 |
| - // Key can be added to header |
102 |
| - $currentHeader .= sprintf('%s%s', $position > 0 ? $this->xkeyGlue : '', $key); |
103 |
| - } |
| 33 | + parent::__construct($clients, $maxHeaderLength, 'xkey', $xkeyGlue); |
104 | 34 | }
|
105 | 35 | }
|
0 commit comments