1 | <?php |
||
18 | final class CachePlugin implements Plugin |
||
19 | { |
||
20 | /** |
||
21 | * @var CacheItemPoolInterface |
||
22 | */ |
||
23 | private $pool; |
||
24 | |||
25 | /** |
||
26 | * @var StreamFactory |
||
27 | */ |
||
28 | private $streamFactory; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $config; |
||
34 | |||
35 | /** |
||
36 | * @param CacheItemPoolInterface $pool |
||
37 | * @param StreamFactory $streamFactory |
||
38 | * @param array $config { |
||
39 | * |
||
40 | * @var bool $respect_cache_headers Whether to look at the cache directives or ignore them |
||
41 | * @var int $default_ttl If we do not respect cache headers or can't calculate a good ttl, use this value. |
||
42 | * } |
||
43 | */ |
||
44 | 6 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 4 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
58 | { |
||
59 | 4 | $method = strtoupper($request->getMethod()); |
|
60 | |||
61 | // if the request not is cachable, move to $next |
||
62 | 4 | if ($method !== 'GET' && $method !== 'HEAD') { |
|
63 | 1 | return $next($request); |
|
64 | } |
||
65 | |||
66 | // If we can cache the request |
||
67 | 3 | $key = $this->createCacheKey($request); |
|
68 | 3 | $cacheItem = $this->pool->getItem($key); |
|
69 | |||
70 | 3 | if ($cacheItem->isHit()) { |
|
71 | // return cached response |
||
72 | $data = $cacheItem->get(); |
||
73 | $response = $data['response']; |
||
74 | $response = $response->withBody($this->streamFactory->createStream($data['body'])); |
||
75 | |||
76 | return new FulfilledPromise($response); |
||
77 | } |
||
78 | |||
79 | 3 | return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { |
|
80 | 3 | if ($this->isCacheable($response) && ($maxAge = $this->getMaxAge($response)) > 0) { |
|
81 | 2 | $bodyStream = $response->getBody(); |
|
82 | 2 | $body = $bodyStream->__toString(); |
|
83 | 2 | if ($bodyStream->isSeekable()) { |
|
84 | 2 | $bodyStream->rewind(); |
|
85 | 2 | } else { |
|
86 | $response = $response->withBody($this->streamFactory->createStream($body)); |
||
87 | } |
||
88 | |||
89 | 2 | $cacheItem->set(['response' => $response, 'body' => $body]) |
|
90 | 2 | ->expiresAfter($maxAge); |
|
91 | 2 | $this->pool->save($cacheItem); |
|
92 | 2 | } |
|
93 | |||
94 | 3 | return $response; |
|
95 | 3 | }); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Verify that we can cache this response. |
||
100 | * |
||
101 | * @param ResponseInterface $response |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | 3 | protected function isCacheable(ResponseInterface $response) |
|
119 | |||
120 | /** |
||
121 | * Get the value of a parameter in the cache control header. |
||
122 | * |
||
123 | * @param ResponseInterface $response |
||
124 | * @param string $name The field of Cache-Control to fetch |
||
125 | * |
||
126 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
127 | */ |
||
128 | 2 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
145 | |||
146 | /** |
||
147 | * @param RequestInterface $request |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 3 | private function createCacheKey(RequestInterface $request) |
|
155 | |||
156 | /** |
||
157 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
158 | * |
||
159 | * @param ResponseInterface $response |
||
160 | * |
||
161 | * @return int|null |
||
162 | */ |
||
163 | 2 | private function getMaxAge(ResponseInterface $response) |
|
188 | |||
189 | /** |
||
190 | * Configure an options resolver. |
||
191 | * |
||
192 | * @param OptionsResolver $resolver |
||
193 | */ |
||
194 | 6 | private function configureOptions(OptionsResolver $resolver) |
|
204 | } |
||
205 |