-
Notifications
You must be signed in to change notification settings - Fork 9
/
CliHttpCache.php
70 lines (57 loc) · 1.75 KB
/
CliHttpCache.php
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
<?php
declare(strict_types=1);
namespace BEAR\QueryRepository;
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface;
use function is_string;
use function parse_str;
use function sprintf;
use function str_replace;
use function strtoupper;
use const PHP_EOL;
final class CliHttpCache implements HttpCacheInterface
{
/** @var ResourceStorageInterface */
private $storage;
public function __construct(ResourceStorageInterface $storage)
{
$this->storage = $storage;
}
/**
* {@inheritdoc}
*/
public function isNotModified(array $server): bool
{
/** @var array{HTTP_IF_NONE_MATCH: string}|array{argc: int, argv: array} $server */
$hasRequestHeaderInCli = isset($server['argc']) && $server['argc'] === 4 && isset($server['argv']);
if ($hasRequestHeaderInCli) {
$server = $this->setRequestHeaders($server, $server['argv'][3]); // @phpstan-ignore-line
}
return isset($server['HTTP_IF_NONE_MATCH']) && is_string($server['HTTP_IF_NONE_MATCH']) && $this->storage->hasEtag($server['HTTP_IF_NONE_MATCH']);
}
/**
* {@inheritdoc}
*
* @return void
*/
public function transfer()
{
echo '304 Not Modified' . PHP_EOL . PHP_EOL;
}
/**
* @param array<string, mixed> $server
*
* @return array<string, string>
*/
private function setRequestHeaders(array $server, string $query): array
{
parse_str($query, $headers);
foreach ($headers as $key => $header) {
$server[$this->getServerKey($key)] = (string) $header;
}
return $server;
}
private function getServerKey(string $key): string
{
return sprintf('HTTP_%s', strtoupper(str_replace('-', '_', $key)));
}
}