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
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $staticUrls = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $prefixUrls = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CacheInterface |
28
|
|
|
*/ |
29
|
|
|
private $cache; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $urls |
33
|
|
|
* @param CacheInterface $cache |
34
|
|
|
*/ |
35
|
1 |
|
public function __construct(array $urls, array $headers = [], CacheInterface $cache = null) |
36
|
|
|
{ |
37
|
1 |
|
$this->staticUrls = array_filter( |
38
|
1 |
|
$urls, |
39
|
1 |
|
function ($url) { |
40
|
1 |
|
return !(strlen($url) >= 3 && substr($url, -3) === '***'); |
41
|
1 |
|
} |
42
|
|
|
); |
43
|
1 |
|
$this->prefixUrls = array_map( |
44
|
1 |
|
function ($url) { |
45
|
1 |
|
return substr($url, 0, -3); |
46
|
1 |
|
}, |
47
|
1 |
|
array_filter( |
48
|
1 |
|
$urls, |
49
|
1 |
|
function ($url) { |
50
|
1 |
|
return strlen($url) >= 3 && substr($url, -3) === '***'; |
51
|
1 |
|
} |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
|
55
|
1 |
|
$this->headers = $headers; |
|
|
|
|
56
|
1 |
|
$this->cache = $cache instanceof CacheInterface ? $cache : new ArrayCache(); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
60
|
|
|
{ |
61
|
1 |
|
if ($request->getMethod() !== 'GET') { |
62
|
|
|
return resolve($next($request)); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$uri = $request->getUri()->getPath(); |
66
|
1 |
|
if (!in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) { |
67
|
|
|
return resolve($next($request)); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$key = $uri; |
71
|
|
|
|
72
|
1 |
|
return $this->cache->get($key)->then(function ($json) { |
73
|
1 |
|
$cachedResponse = json_decode($json); |
74
|
|
|
|
75
|
1 |
|
return new Response($cachedResponse->code, (array)$cachedResponse->headers, stream_for($cachedResponse->body)); |
76
|
|
|
}, function () use ($next, $request, $key) { |
77
|
1 |
|
return resolve($next($request))->then(function (ResponseInterface $response) use ($key) { |
78
|
1 |
|
if ($response->getBody() instanceof HttpBodyStream) { |
79
|
1 |
|
return $response; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$body = (string)$response->getBody(); |
83
|
1 |
|
$headers = []; |
84
|
1 |
|
foreach ($this->headers as $header) { |
85
|
1 |
|
if (!$response->hasHeader($header)) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$headers[$header] = $response->getHeaderLine($header); |
90
|
|
|
} |
91
|
1 |
|
$cachedResponse = json_encode([ |
92
|
1 |
|
'body' => $body, |
93
|
1 |
|
'headers' => $headers, |
94
|
1 |
|
'code' => $response->getStatusCode(), |
95
|
|
|
]); |
96
|
1 |
|
$this->cache->set($key, $cachedResponse); |
97
|
|
|
|
98
|
1 |
|
return $response->withBody(stream_for($body)); |
99
|
1 |
|
}); |
100
|
1 |
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
private function matchesPrefixUrl(string $uri): bool |
104
|
|
|
{ |
105
|
1 |
|
foreach ($this->prefixUrls as $url) { |
106
|
1 |
|
$urlLength = strlen($url); |
107
|
1 |
|
if (substr($uri, 0, $urlLength) === $url) { |
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|