1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use React\Cache\ArrayCache; |
8
|
|
|
use React\Cache\CacheInterface; |
9
|
|
|
use React\Http\Io\HttpBodyStream; |
10
|
|
|
use React\Http\Response; |
11
|
|
|
use function React\Promise\resolve; |
12
|
|
|
use function RingCentral\Psr7\stream_for; |
13
|
|
|
|
14
|
|
|
final class ResponseCacheMiddleware |
15
|
|
|
{ |
16
|
|
|
const PREFIX_WITHOUT_QUERY = '***'; |
17
|
|
|
const PREFIX_WITH_QUERY = '???'; |
18
|
|
|
const PREFIXES = [ |
19
|
|
|
self::PREFIX_WITH_QUERY, |
20
|
|
|
self::PREFIX_WITHOUT_QUERY, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $staticUrls = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $prefixUrlsWithoutQuery = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $prefixUrlsWithQuery = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var CacheInterface |
40
|
|
|
*/ |
41
|
|
|
private $cache; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $urls |
45
|
|
|
* @param CacheInterface $cache |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(array $urls, array $headers = [], CacheInterface $cache = null) |
48
|
|
|
{ |
49
|
1 |
|
$this->sortUrls($urls); |
50
|
1 |
|
$this->headers = $headers; |
|
|
|
|
51
|
1 |
|
$this->cache = $cache instanceof CacheInterface ? $cache : new ArrayCache(); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
55
|
|
|
{ |
56
|
1 |
|
if ($request->getMethod() !== 'GET') { |
57
|
|
|
return resolve($next($request)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$uri = $request->getUri()->getPath(); |
61
|
1 |
|
if (!in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) { |
62
|
|
|
return resolve($next($request)); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$key = $uri; |
66
|
1 |
|
$query = $request->getUri()->getQuery(); |
67
|
1 |
|
if (strlen($query) > 0 && $this->queryInKey($uri)) { |
68
|
1 |
|
$key .= '?' . $query; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $this->cache->get($key)->then(function ($json) { |
72
|
1 |
|
$cachedResponse = json_decode($json); |
73
|
|
|
|
74
|
1 |
|
return new Response($cachedResponse->code, (array)$cachedResponse->headers, stream_for($cachedResponse->body)); |
75
|
|
|
}, function () use ($next, $request, $key) { |
76
|
1 |
|
return resolve($next($request))->then(function (ResponseInterface $response) use ($key) { |
77
|
1 |
|
if ($response->getBody() instanceof HttpBodyStream) { |
78
|
1 |
|
return $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$body = (string)$response->getBody(); |
82
|
1 |
|
$headers = []; |
83
|
1 |
|
foreach ($this->headers as $header) { |
84
|
1 |
|
if (!$response->hasHeader($header)) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$headers[$header] = $response->getHeaderLine($header); |
89
|
|
|
} |
90
|
1 |
|
$cachedResponse = json_encode([ |
91
|
1 |
|
'body' => $body, |
92
|
1 |
|
'headers' => $headers, |
93
|
1 |
|
'code' => $response->getStatusCode(), |
94
|
|
|
]); |
95
|
1 |
|
$this->cache->set($key, $cachedResponse); |
96
|
|
|
|
97
|
1 |
|
return $response->withBody(stream_for($body)); |
98
|
1 |
|
}); |
99
|
1 |
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
private function sortUrls(array $urls) |
103
|
|
|
{ |
104
|
1 |
|
foreach ($urls as $url) { |
105
|
1 |
|
if (!(strlen($url) >= 3 && in_array(substr($url, -3), self::PREFIXES, true))) { |
106
|
1 |
|
$this->staticUrls[] = $url; |
107
|
|
|
|
108
|
1 |
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
if (strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITHOUT_QUERY) { |
112
|
1 |
|
$this->prefixUrlsWithoutQuery[] = substr($url, 0, -3); |
113
|
|
|
|
114
|
1 |
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
if (strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITH_QUERY) { |
118
|
1 |
|
$this->prefixUrlsWithQuery[] = substr($url, 0, -3); |
119
|
|
|
|
120
|
1 |
|
continue; |
121
|
|
|
} |
122
|
|
|
} |
123
|
1 |
|
} |
124
|
|
|
|
125
|
1 |
|
private function matchesPrefixUrl(string $uri): bool |
126
|
|
|
{ |
127
|
1 |
|
if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) { |
128
|
1 |
|
return true; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
private function queryInKey(string $uri): bool |
135
|
|
|
{ |
136
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
private function urlMatchesPrefixes(array $urls, string $uri): bool |
140
|
|
|
{ |
141
|
1 |
|
foreach ($urls as $url) { |
142
|
1 |
|
$urlLength = strlen($url); |
143
|
1 |
|
if (substr($uri, 0, $urlLength) === $url) { |
144
|
1 |
|
return true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
return false; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|