|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
|
4
|
|
|
|
|
5
|
1 |
|
@trigger_error('The '.__NAMESPACE__.'\CachePlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\CachePlugin instead.', E_USER_DEPRECATED); |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Http\Message\StreamFactory; |
|
8
|
|
|
use Http\Promise\FulfilledPromise; |
|
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Allow for caching a response with a PSR-6 compatible caching engine. |
|
16
|
|
|
* |
|
17
|
|
|
* It can follow the RFC-7234 caching specification or use a fixed cache lifetime. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\CachePlugin} instead. |
|
22
|
|
|
*/ |
|
23
|
|
|
class CachePlugin implements Plugin |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var CacheItemPoolInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $pool; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var StreamFactory |
|
32
|
|
|
*/ |
|
33
|
|
|
private $streamFactory; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $config; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param CacheItemPoolInterface $pool |
|
42
|
|
|
* @param StreamFactory $streamFactory |
|
43
|
|
|
* @param array $config { |
|
44
|
|
|
* |
|
45
|
|
|
* @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
|
46
|
|
|
* @var int $default_ttl If we do not respect cache headers or the headers specify cache control, use this value |
|
47
|
|
|
* } |
|
48
|
|
|
*/ |
|
49
|
6 |
|
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
50
|
|
|
{ |
|
51
|
6 |
|
$this->pool = $pool; |
|
52
|
6 |
|
$this->streamFactory = $streamFactory; |
|
53
|
|
|
|
|
54
|
6 |
|
$optionsResolver = new OptionsResolver(); |
|
55
|
6 |
|
$this->configureOptions($optionsResolver); |
|
56
|
6 |
|
$this->config = $optionsResolver->resolve($config); |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
63
|
|
|
{ |
|
64
|
4 |
|
$method = strtoupper($request->getMethod()); |
|
65
|
|
|
|
|
66
|
|
|
// if the request is not cacheable, move to $next |
|
67
|
4 |
|
if ('GET' !== $method && 'HEAD' !== $method) { |
|
68
|
1 |
|
return $next($request); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
$key = $this->createCacheKey($request); |
|
72
|
3 |
|
$cacheItem = $this->pool->getItem($key); |
|
73
|
|
|
|
|
74
|
3 |
|
if ($cacheItem->isHit()) { |
|
75
|
|
|
$data = $cacheItem->get(); |
|
76
|
|
|
/** @var ResponseInterface $response */ |
|
77
|
|
|
$response = $data['response']; |
|
78
|
|
|
$response = $response->withBody($this->streamFactory->createStream($data['body'])); |
|
79
|
|
|
|
|
80
|
|
|
return new FulfilledPromise($response); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { |
|
84
|
3 |
|
if ($this->isCacheable($response)) { |
|
85
|
2 |
|
$bodyStream = $response->getBody(); |
|
86
|
2 |
|
$body = (string) $bodyStream; |
|
87
|
2 |
|
if ($bodyStream->isSeekable()) { |
|
88
|
2 |
|
$bodyStream->rewind(); |
|
89
|
2 |
|
} else { |
|
90
|
|
|
$response = $response->withBody($this->streamFactory->createStream($body)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
$cacheItem->set(['response' => $response, 'body' => $body]) |
|
94
|
2 |
|
->expiresAfter($this->getMaxAge($response)); |
|
95
|
2 |
|
$this->pool->save($cacheItem); |
|
96
|
2 |
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
return $response; |
|
99
|
3 |
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Verify that we can cache this response. |
|
104
|
|
|
* |
|
105
|
|
|
* @param ResponseInterface $response |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
3 |
|
protected function isCacheable(ResponseInterface $response) |
|
110
|
|
|
{ |
|
111
|
3 |
|
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) { |
|
112
|
1 |
|
return false; |
|
113
|
|
|
} |
|
114
|
2 |
|
if (!$this->config['respect_cache_headers']) { |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
2 |
|
if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the value of a parameter in the cache control header. |
|
126
|
|
|
* |
|
127
|
|
|
* @param ResponseInterface $response |
|
128
|
|
|
* @param string $name The field of Cache-Control to fetch |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool|string the value of the directive, true if directive without value, false if directive not present |
|
131
|
|
|
*/ |
|
132
|
2 |
|
private function getCacheControlDirective(ResponseInterface $response, $name) |
|
133
|
|
|
{ |
|
134
|
2 |
|
$headers = $response->getHeader('Cache-Control'); |
|
135
|
2 |
|
foreach ($headers as $header) { |
|
136
|
1 |
|
if (preg_match(sprintf('|%s=?([0-9]+)?|i', $name), $header, $matches)) { |
|
137
|
|
|
// return the value for $name if it exists |
|
138
|
1 |
|
if (isset($matches[1])) { |
|
139
|
1 |
|
return $matches[1]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
2 |
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param RequestInterface $request |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
3 |
|
private function createCacheKey(RequestInterface $request) |
|
155
|
|
|
{ |
|
156
|
3 |
|
return md5($request->getMethod().' '.$request->getUri()); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
|
161
|
|
|
* |
|
162
|
|
|
* @param ResponseInterface $response |
|
163
|
|
|
* |
|
164
|
|
|
* @return int|null |
|
165
|
|
|
*/ |
|
166
|
2 |
|
private function getMaxAge(ResponseInterface $response) |
|
167
|
|
|
{ |
|
168
|
2 |
|
if (!$this->config['respect_cache_headers']) { |
|
169
|
|
|
return $this->config['default_ttl']; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// check for max age in the Cache-Control header |
|
173
|
2 |
|
$maxAge = $this->getCacheControlDirective($response, 'max-age'); |
|
174
|
2 |
|
if (!is_bool($maxAge)) { |
|
175
|
1 |
|
$ageHeaders = $response->getHeader('Age'); |
|
176
|
1 |
|
foreach ($ageHeaders as $age) { |
|
177
|
1 |
|
return $maxAge - ((int) $age); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $maxAge; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// check for ttl in the Expires header |
|
184
|
1 |
|
$headers = $response->getHeader('Expires'); |
|
185
|
1 |
|
foreach ($headers as $header) { |
|
186
|
|
|
return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp(); |
|
187
|
1 |
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
return $this->config['default_ttl']; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Configure an options resolver. |
|
194
|
|
|
* |
|
195
|
|
|
* @param OptionsResolver $resolver |
|
196
|
|
|
*/ |
|
197
|
6 |
|
private function configureOptions(OptionsResolver $resolver) |
|
198
|
|
|
{ |
|
199
|
6 |
|
$resolver->setDefaults([ |
|
200
|
6 |
|
'default_ttl' => null, |
|
201
|
6 |
|
'respect_cache_headers' => true, |
|
202
|
6 |
|
]); |
|
203
|
|
|
|
|
204
|
6 |
|
$resolver->setAllowedTypes('default_ttl', ['int', 'null']); |
|
205
|
6 |
|
$resolver->setAllowedTypes('respect_cache_headers', 'bool'); |
|
206
|
6 |
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: