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 | 6 | * @param StreamFactory $streamFactory |
|
46 | * @param array $config |
||
47 | 6 | */ |
|
48 | 6 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
58 | |||
59 | /** |
||
60 | 4 | * {@inheritdoc} |
|
61 | 1 | */ |
|
62 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
||
94 | 1 | ||
95 | /** |
||
96 | 2 | * Verify that we can cache this response. |
|
97 | * |
||
98 | * @param ResponseInterface $response |
||
99 | 2 | * |
|
100 | * @return bool |
||
101 | */ |
||
102 | protected function isCacheable(ResponseInterface $response) |
||
116 | 2 | ||
117 | 2 | /** |
|
118 | 1 | * Get the value of a parameter in the cache control header. |
|
119 | * |
||
120 | * @param ResponseInterface $response |
||
121 | 1 | * @param string $name The field of Cache-Control to fetch |
|
122 | 1 | * |
|
123 | * @return bool|string The value of the directive, true if directive without value, false if directive not present. |
||
124 | */ |
||
125 | private function getCacheControlDirective(ResponseInterface $response, $name) |
||
142 | |||
143 | /** |
||
144 | * @param RequestInterface $request |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 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 | 2 | * @param ResponseInterface $response |
|
157 | 2 | * |
|
158 | 1 | * @return int|null |
|
159 | 1 | */ |
|
160 | 1 | private function getMaxAge(ResponseInterface $response) |
|
185 | |||
186 | /** |
||
187 | * Configure an options resolver |
||
188 | * |
||
189 | * @param OptionsResolver $resolver |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | private function configureOptions(OptionsResolver $resolver) |
||
203 | |||
204 | /** |
||
205 | * Set config to the plugin. This will overwrite any previously set config values. |
||
206 | * |
||
207 | * @param array $config |
||
208 | */ |
||
209 | public function setConfig(array $config) |
||
213 | } |
||
214 |