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