1 | <?php |
||
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 | * @var OptionsResolver |
||
36 | */ |
||
37 | private $optionsResolver; |
||
38 | |||
39 | /** |
||
40 | * Available options are |
||
41 | * - respect_cache_headers: Whether to look at the cache directives or ignore them. |
||
42 | * - default_ttl: If we do not respect cache headers or can't calculate a good ttl, use this value. |
||
43 | * |
||
44 | * @param CacheItemPoolInterface $pool |
||
45 | * @param StreamFactory $streamFactory |
||
46 | * @param array $config |
||
47 | */ |
||
48 | 6 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
49 | { |
||
50 | 6 | $this->pool = $pool; |
|
51 | 6 | $this->streamFactory = $streamFactory; |
|
52 | |||
53 | 6 | $this->optionsResolver = new OptionsResolver(); |
|
54 | 6 | $this->configureOptions($this->optionsResolver); |
|
55 | |||
56 | 6 | $this->setConfig($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 not is cachable, move to $next |
||
67 | 4 | if ($method !== 'GET' && $method !== 'HEAD') { |
|
68 | 1 | return $next($request); |
|
69 | } |
||
70 | |||
71 | // If we can cache the request |
||
72 | 3 | $key = $this->createCacheKey($request); |
|
73 | 3 | $cacheItem = $this->pool->getItem($key); |
|
74 | |||
75 | 3 | if ($cacheItem->isHit()) { |
|
76 | // return cached response |
||
77 | $data = $cacheItem->get(); |
||
78 | $response = $data['response']; |
||
79 | $response = $response->withBody($this->streamFactory->createStream($data['body'])); |
||
80 | |||
81 | return new FulfilledPromise($response); |
||
82 | } |
||
83 | |||
84 | 3 | return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { |
|
85 | 3 | if ($this->isCacheable($response)) { |
|
86 | 2 | $cacheItem->set(['response' => $response, 'body' => $response->getBody()->__toString()]) |
|
87 | 2 | ->expiresAfter($this->getMaxAge($response)); |
|
88 | 2 | $this->pool->save($cacheItem); |
|
89 | 2 | } |
|
90 | |||
91 | 3 | return $response; |
|
92 | 3 | }); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Verify that we can cache this response. |
||
97 | * |
||
98 | * @param ResponseInterface $response |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | 3 | protected function isCacheable(ResponseInterface $response) |
|
103 | { |
||
104 | 3 | if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) { |
|
105 | 1 | return false; |
|
106 | } |
||
107 | 2 | if (!$this->config['respect_cache_headers']) { |
|
108 | return true; |
||
109 | } |
||
110 | 2 | if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { |
|
111 | return false; |
||
112 | } |
||
113 | |||
114 | 2 | return true; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get the value of a parameter in the cache control header. |
||
119 | * |
||
120 | * @param ResponseInterface $response |
||
121 | * @param string $name The field of Cache-Control to fetch |
||
122 | * |
||
123 | * @return bool|string The value of the directive, true if directive without value, false if directive not present. |
||
124 | */ |
||
125 | 2 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
126 | { |
||
127 | 2 | $headers = $response->getHeader('Cache-Control'); |
|
128 | 2 | foreach ($headers as $header) { |
|
129 | 1 | if (preg_match(sprintf('|%s=?([0-9]+)?|i', $name), $header, $matches)) { |
|
130 | |||
131 | // return the value for $name if it exists |
||
132 | 1 | if (isset($matches[1])) { |
|
133 | 1 | return $matches[1]; |
|
134 | } |
||
135 | |||
136 | return true; |
||
137 | } |
||
138 | 2 | } |
|
139 | |||
140 | 2 | return false; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param RequestInterface $request |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 3 | private function createCacheKey(RequestInterface $request) |
|
152 | |||
153 | /** |
||
154 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
155 | * |
||
156 | * @param ResponseInterface $response |
||
157 | * |
||
158 | * @return int|null |
||
159 | */ |
||
160 | 2 | private function getMaxAge(ResponseInterface $response) |
|
185 | |||
186 | /** |
||
187 | * Configure an options resolver. |
||
188 | * |
||
189 | * @param OptionsResolver $resolver |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | 6 | private function configureOptions(OptionsResolver $resolver) |
|
211 | |||
212 | /** |
||
213 | * Set config to the plugin. This will overwrite any previously set config values. |
||
214 | * |
||
215 | * @param array $config |
||
216 | */ |
||
217 | 6 | public function setConfig(array $config) |
|
221 | } |
||
222 |
This check looks for function calls that miss required arguments.