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 | * Available options are |
||
36 | * - respect_cache_headers: Whether to look at the cache directives or ignore them. |
||
37 | * - default_ttl: If we do not respect cache headers or can't calculate a good ttl, use this value. |
||
38 | * |
||
39 | * @param CacheItemPoolInterface $pool |
||
40 | * @param StreamFactory $streamFactory |
||
41 | * @param array $config |
||
42 | */ |
||
43 | 6 | public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) |
|
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | 4 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
96 | |||
97 | /** |
||
98 | * Verify that we can cache this response. |
||
99 | * |
||
100 | * @param ResponseInterface $response |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | 3 | protected function isCacheable(ResponseInterface $response) |
|
118 | |||
119 | /** |
||
120 | * Get the value of a parameter in the cache control header. |
||
121 | * |
||
122 | * @param ResponseInterface $response |
||
123 | * @param string $name The field of Cache-Control to fetch |
||
124 | * |
||
125 | * @return bool|string The value of the directive, true if directive without value, false if directive not present. |
||
126 | */ |
||
127 | 2 | private function getCacheControlDirective(ResponseInterface $response, $name) |
|
144 | |||
145 | /** |
||
146 | * @param RequestInterface $request |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 3 | private function createCacheKey(RequestInterface $request) |
|
154 | |||
155 | /** |
||
156 | * Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
||
157 | * |
||
158 | * @param ResponseInterface $response |
||
159 | * |
||
160 | * @return int|null |
||
161 | */ |
||
162 | 2 | private function getMaxAge(ResponseInterface $response) |
|
187 | |||
188 | /** |
||
189 | * Configure an options resolver. |
||
190 | * |
||
191 | * @param OptionsResolver $resolver |
||
192 | */ |
||
193 | 6 | private function configureOptions(OptionsResolver $resolver) |
|
203 | } |
||
204 |