1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface; |
8
|
|
|
|
9
|
|
|
use function assert; |
10
|
|
|
use function is_string; |
11
|
|
|
use function parse_str; |
12
|
|
|
use function sprintf; |
13
|
|
|
use function str_replace; |
14
|
|
|
use function strtoupper; |
15
|
|
|
|
16
|
|
|
use const PHP_EOL; |
17
|
|
|
|
18
|
|
|
final class CliHttpCache implements HttpCacheInterface |
19
|
|
|
{ |
20
|
|
|
/** @var ResourceStorageInterface */ |
21
|
|
|
private $storage; |
22
|
|
|
|
23
|
|
|
public function __construct(ResourceStorageInterface $storage) |
24
|
|
|
{ |
25
|
|
|
$this->storage = $storage; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function isNotModified(array $server): bool |
32
|
|
|
{ |
33
|
|
|
$etag = $this->getEtag($server); |
34
|
|
|
if ($etag === null) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->storage->hasEtag($etag); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function transfer() |
47
|
|
|
{ |
48
|
|
|
echo '304 Not Modified' . PHP_EOL . PHP_EOL; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array<string, string> |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
private function getServer(string $query): array |
55
|
|
|
{ |
56
|
|
|
parse_str($query, $headers); |
57
|
|
|
$server = []; |
58
|
|
|
foreach ($headers as $key => $header) { |
|
|
|
|
59
|
|
|
assert(is_string($header)); |
60
|
|
|
assert(is_string($key)); |
61
|
|
|
$server[$this->getServerKey($key)] = $header; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $server; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getServerKey(string $key): string |
68
|
|
|
{ |
69
|
|
|
return sprintf('HTTP_%s', strtoupper(str_replace('-', '_', $key))); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array<string, mixed> $server |
74
|
|
|
*/ |
75
|
|
|
private function getEtag(array $server): ?string |
76
|
|
|
{ |
77
|
|
|
$hasRequestHeaderInCli = isset($server['argc']) && $server['argc'] === 4 && isset($server['argv'][3]); |
78
|
|
|
if ($hasRequestHeaderInCli) { |
79
|
|
|
$server = $this->getServer((string) $server['argv'][3]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$hasValidEtag = isset($server['HTTP_IF_NONE_MATCH']) && is_string($server['HTTP_IF_NONE_MATCH']); |
83
|
|
|
|
84
|
|
|
return $hasValidEtag ? $server['HTTP_IF_NONE_MATCH'] : null; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.