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 | * @var string $hash_algo The hashing algorithm to use when generating cache keys. |
||
43 | * } |
||
44 | */ |
||
45 | 6 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 4 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
59 | { |
||
60 | 4 | $method = strtoupper($request->getMethod()); |
|
61 | |||
62 | // if the request not is cachable, move to $next |
||
63 | 4 | if ($method !== 'GET' && $method !== 'HEAD') { |
|
64 | 1 | return $next($request); |
|
65 | } |
||
66 | |||
67 | // If we can cache the request |
||
68 | 3 | $key = $this->createCacheKey($request); |
|
69 | 3 | $cacheItem = $this->pool->getItem($key); |
|
70 | |||
71 | 3 | if ($cacheItem->isHit()) { |
|
72 | // return cached response |
||
73 | $data = $cacheItem->get(); |
||
74 | $response = $data['response']; |
||
75 | $response = $response->withBody($this->streamFactory->createStream($data['body'])); |
||
76 | |||
77 | return new FulfilledPromise($response); |
||
78 | } |
||
79 | |||
80 | 3 | return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { |
|
81 | 3 | if ($this->isCacheable($response)) { |
|
82 | 2 | $bodyStream = $response->getBody(); |
|
83 | 2 | $body = $bodyStream->__toString(); |
|
84 | 2 | if ($bodyStream->isSeekable()) { |
|
85 | 2 | $bodyStream->rewind(); |
|
86 | 2 | } else { |
|
87 | $response = $response->withBody($this->streamFactory->createStream($body)); |
||
88 | } |
||
89 | |||
90 | 2 | $cacheItem->set(['response' => $response, 'body' => $body]) |
|
91 | 2 | ->expiresAfter($this->getMaxAge($response)); |
|
92 | 2 | $this->pool->save($cacheItem); |
|
93 | 2 | } |
|
94 | |||
95 | 3 | return $response; |
|
96 | 3 | }); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Verify that we can cache this response. |
||
101 | * |
||
102 | * @param ResponseInterface $response |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | 3 | protected function isCacheable(ResponseInterface $response) |
|
120 | |||
121 | /** |
||
122 | * Get the value of a parameter in the cache control header. |
||
123 | * |
||
124 | * @param ResponseInterface $response |
||
125 | * @param string $name The field of Cache-Control to fetch |
||
126 | * |
||
127 | * @return bool|string The value of the directive, true if directive without value, false if directive not present |
||
128 | */ |
||
129 | 2 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
146 | |||
147 | /** |
||
148 | * @param RequestInterface $request |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | 3 | private function createCacheKey(RequestInterface $request) |
|
156 | |||
157 | /** |
||
158 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
159 | * |
||
160 | * @param ResponseInterface $response |
||
161 | * |
||
162 | * @return int|null |
||
163 | */ |
||
164 | 2 | private function getMaxAge(ResponseInterface $response) |
|
189 | |||
190 | /** |
||
191 | * Configure an options resolver. |
||
192 | * |
||
193 | * @param OptionsResolver $resolver |
||
194 | */ |
||
195 | 6 | private function configureOptions(OptionsResolver $resolver) |
|
207 | } |
||
208 |