1 | <?php |
||
19 | final class CachePlugin implements Plugin |
||
20 | { |
||
21 | /** |
||
22 | * @var CacheItemPoolInterface |
||
23 | */ |
||
24 | private $pool; |
||
25 | |||
26 | /** |
||
27 | * @var StreamFactory |
||
28 | */ |
||
29 | private $streamFactory; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $config; |
||
35 | |||
36 | /** |
||
37 | * @param CacheItemPoolInterface $pool |
||
38 | * @param StreamFactory $streamFactory |
||
39 | * @param array $config { |
||
40 | * |
||
41 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
42 | * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value. |
||
43 | * } |
||
44 | */ |
||
45 | 10 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 8 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
59 | { |
||
60 | 8 | $method = strtoupper($request->getMethod()); |
|
61 | // if the request not is cachable, move to $next |
||
62 | 8 | if ($method !== 'GET' && $method !== 'HEAD') { |
|
63 | 1 | return $next($request); |
|
64 | } |
||
65 | |||
66 | // If we can cache the request |
||
67 | 7 | $key = $this->createCacheKey($request); |
|
68 | 7 | $cacheItem = $this->pool->getItem($key); |
|
69 | |||
70 | 7 | if ($cacheItem->isHit()) { |
|
71 | 3 | $data = $cacheItem->get(); |
|
72 | 3 | if (isset($data['expiresAt']) && time() < $data['expiresAt']) { |
|
73 | // This item is still valid according to previous cache headers |
||
74 | 1 | return new FulfilledPromise($this->createResponseFromCacheItem($cacheItem)); |
|
75 | } |
||
76 | |||
77 | // Add headers to ask the server if this cache is still valid |
||
78 | 2 | if ($mod = $this->getModifiedAt($cacheItem)) { |
|
79 | 2 | $mod = new \DateTime('@'.$mod); |
|
80 | 2 | $mod->setTimezone(new \DateTimeZone('GMT')); |
|
81 | 2 | $request = $request->withHeader('If-Modified-Since', sprintf('%s GMT', $mod->format('l, d-M-y H:i:s'))); |
|
82 | 2 | } |
|
83 | |||
84 | 2 | if ($etag = $this->getETag($cacheItem)) { |
|
85 | 2 | $request = $request->withHeader('If-None-Match', $etag); |
|
86 | 2 | } |
|
87 | 2 | } |
|
88 | |||
89 | 6 | return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { |
|
90 | 6 | if (304 === $response->getStatusCode()) { |
|
91 | 2 | if (!$cacheItem->isHit()) { |
|
92 | // We do not have the item in cache. We can return the cached response. |
||
93 | 1 | return $response; |
|
94 | } |
||
95 | |||
96 | // The cached response we have is still valid |
||
97 | 1 | $data = $cacheItem->get(); |
|
98 | 1 | $maxAge = $this->getMaxAge($response); |
|
99 | 1 | $data['expiresAt'] = time() + $maxAge; |
|
100 | 1 | $cacheItem->set($data)->expiresAfter($this->config['cache_lifetime'] + $maxAge); |
|
101 | 1 | $this->pool->save($cacheItem); |
|
102 | |||
103 | 1 | return $this->createResponseFromCacheItem($cacheItem); |
|
104 | } |
||
105 | |||
106 | 4 | if ($this->isCacheable($response)) { |
|
107 | 3 | $bodyStream = $response->getBody(); |
|
108 | 3 | $body = $bodyStream->__toString(); |
|
109 | 3 | if ($bodyStream->isSeekable()) { |
|
110 | 3 | $bodyStream->rewind(); |
|
111 | 3 | } else { |
|
112 | $response = $response->withBody($this->streamFactory->createStream($body)); |
||
113 | } |
||
114 | |||
115 | 3 | $maxAge = $this->getMaxAge($response); |
|
116 | $cacheItem |
||
117 | 3 | ->expiresAfter($this->config['cache_lifetime'] + $maxAge) |
|
118 | 3 | ->set([ |
|
119 | 3 | 'response' => $response, |
|
120 | 3 | 'body' => $body, |
|
121 | 3 | 'expiresAt' => time() + $maxAge, |
|
122 | 3 | 'createdAt' => time(), |
|
123 | 3 | 'etag' => $response->getHeader('ETag'), |
|
124 | 3 | ]); |
|
125 | 3 | $this->pool->save($cacheItem); |
|
126 | 3 | } |
|
127 | |||
128 | 4 | return $response; |
|
129 | 6 | }); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Verify that we can cache this response. |
||
134 | * |
||
135 | * @param ResponseInterface $response |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | 4 | protected function isCacheable(ResponseInterface $response) |
|
153 | |||
154 | /** |
||
155 | * Get the value of a parameter in the cache control header. |
||
156 | * |
||
157 | * @param ResponseInterface $response |
||
158 | * @param string $name The field of Cache-Control to fetch |
||
159 | * |
||
160 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
161 | */ |
||
162 | 4 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
179 | |||
180 | /** |
||
181 | * @param RequestInterface $request |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | 7 | private function createCacheKey(RequestInterface $request) |
|
189 | |||
190 | /** |
||
191 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
192 | * |
||
193 | * @param ResponseInterface $response |
||
194 | * |
||
195 | * @return int|null |
||
196 | */ |
||
197 | 4 | private function getMaxAge(ResponseInterface $response) |
|
222 | |||
223 | /** |
||
224 | * Configure an options resolver. |
||
225 | * |
||
226 | * @param OptionsResolver $resolver |
||
227 | */ |
||
228 | 10 | private function configureOptions(OptionsResolver $resolver) |
|
240 | |||
241 | /** |
||
242 | * @param CacheItemInterface $cacheItem |
||
243 | * |
||
244 | * @return ResponseInterface |
||
245 | */ |
||
246 | 2 | private function createResponseFromCacheItem(CacheItemInterface $cacheItem) |
|
256 | |||
257 | /** |
||
258 | * Get the timestamp when the cached response was stored. |
||
259 | * |
||
260 | * @param CacheItemInterface $cacheItem |
||
261 | * |
||
262 | * @return int|null |
||
263 | */ |
||
264 | 2 | private function getModifiedAt(CacheItemInterface $cacheItem) |
|
273 | |||
274 | /** |
||
275 | * Get the ETag from the cached response. |
||
276 | * |
||
277 | * @param CacheItemInterface $cacheItem |
||
278 | * |
||
279 | * @return string|null |
||
280 | */ |
||
281 | 2 | private function getETag(CacheItemInterface $cacheItem) |
|
300 | } |
||
301 |