Skip to content

Commit 7fd8957

Browse files
committed
Renamed option + fixes
1 parent cec149b commit 7fd8957

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/CachePlugin.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Cache\CacheItemPoolInterface;
1010
use Psr\Http\Message\RequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
12+
use Symfony\Component\OptionsResolver\Options;
1213
use Symfony\Component\OptionsResolver\OptionsResolver;
1314

1415
/**
@@ -61,7 +62,13 @@ public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamF
6162
public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
6263
{
6364
// Allow caching of private requests
64-
$config['excluded_directives'] = ['no-cache'];
65+
if (isset($config['respect_response_cache_directives'])) {
66+
if (!in_array('no-cache', $config['respect_response_cache_directives'], true)) {
67+
$config['respect_response_cache_directives'][] = ['no-cache'];
68+
}
69+
} else {
70+
$config['respect_response_cache_directives'] = ['no-cache'];
71+
}
6572

6673
self::__construct($pool, $streamFactory, $config);
6774
}
@@ -200,7 +207,7 @@ protected function isCacheable(ResponseInterface $response)
200207
return true;
201208
}
202209

203-
foreach ($this->config['excluded_directives'] as $cacheDirective) {
210+
foreach ($this->config['respect_response_cache_directives'] as $cacheDirective) {
204211
if ($this->getCacheControlDirective($response, $cacheDirective)) {
205212
return false;
206213
}
@@ -258,7 +265,7 @@ private function createCacheKey(RequestInterface $request)
258265
*/
259266
private function getMaxAge(ResponseInterface $response)
260267
{
261-
if (!$this->config['respect_cache_headers']) {
268+
if (!$this->config['respect_cache_headers'] || !in_array('max-age', $this->config['respect_response_cache_directives'], true)) {
262269
return $this->config['default_ttl'];
263270
}
264271

@@ -292,10 +299,11 @@ private function configureOptions(OptionsResolver $resolver)
292299
$resolver->setDefaults([
293300
'cache_lifetime' => 86400 * 30, // 30 days
294301
'default_ttl' => 0,
302+
//Deprecated as of v1.3, to be removed in v2.0. Use respect_response_cache_directives instead
295303
'respect_cache_headers' => true,
296304
'hash_algo' => 'sha1',
297305
'methods' => ['GET', 'HEAD'],
298-
'excluded_directives' => ['no-cache', 'private'],
306+
'respect_response_cache_directives' => ['no-cache', 'private', 'max-age'],
299307
]);
300308

301309
$resolver->setAllowedTypes('cache_lifetime', ['int', 'null']);
@@ -309,6 +317,14 @@ private function configureOptions(OptionsResolver $resolver)
309317

310318
return empty($matches);
311319
});
320+
321+
$resolver->setNormalizer('respect_cache_headers', function (Options $options, $value) {
322+
if (null !== $value) {
323+
@trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED);
324+
}
325+
326+
return $value;
327+
});
312328
}
313329

314330
/**

0 commit comments

Comments
 (0)