Skip to content

Commit 7fda9d4

Browse files
committed
Feedback code fixes
1 parent 879d153 commit 7fda9d4

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

src/CachePlugin.php

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,52 @@ final class CachePlugin implements Plugin
3434
*/
3535
private $config;
3636

37+
/**
38+
* Cache directives indicating if a response can be cached
39+
*
40+
* @var array
41+
*/
42+
private $noCacheFlags = ['no-cache', 'private'];
43+
3744
/**
3845
* @param CacheItemPoolInterface $pool
3946
* @param StreamFactory $streamFactory
4047
* @param array $config {
4148
*
42-
* @var bool $respect_cache_headers Whether to look at the cache directives or ignore them
49+
* @var bool $respect_cache_headers Whether to look at the cache directives or ignore them. This option is deprecated, use `respect_response_cache_directives` instead
4350
* @var int $default_ttl (seconds) If we do not respect cache headers or can't calculate a good ttl, use this
4451
* value
4552
* @var string $hash_algo The hashing algorithm to use when generating cache keys
4653
* @var int $cache_lifetime (seconds) To support serving a previous stale response when the server answers 304
4754
* we have to store the cache for a longer time than the server originally says it is valid for.
4855
* We store a cache item for $cache_lifetime + max age of the response.
4956
* @var array $methods list of request methods which can be cached.
57+
* @var array $respect_response_cache_directives list of cache directives this plugin will respect while caching responses.
5058
* }
5159
*/
5260
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
5361
{
5462
$this->pool = $pool;
5563
$this->streamFactory = $streamFactory;
5664

65+
if (isset($config['respect_cache_headers']) && $config['respect_response_cache_directives']) {
66+
throw new \InvalidArgumentException('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives". Use "respect_response_cache_directives" instead.');
67+
}
68+
5769
$optionsResolver = new OptionsResolver();
5870
$this->configureOptions($optionsResolver);
5971
$this->config = $optionsResolver->resolve($config);
6072
}
6173

74+
/**
75+
* This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will cache responses with `private` cache directive
76+
*
77+
* @param CacheItemPoolInterface $pool
78+
* @param StreamFactory $streamFactory
79+
* @param array $config
80+
*
81+
* @return CachePlugin
82+
*/
6283
public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
6384
{
6485
// Allow caching of private requests
@@ -76,6 +97,15 @@ public static function clientCache(CacheItemPoolInterface $pool, StreamFactory $
7697
return new self($pool, $streamFactory, $config);
7798
}
7899

100+
/**
101+
* This method will setup the cachePlugin in server cache mode. This is the default caching behavior (refuses to cache responses with the `private`or `no-cache` directives
102+
*
103+
* @param CacheItemPoolInterface $pool
104+
* @param StreamFactory $streamFactory
105+
* @param array $config
106+
*
107+
* @return CachePlugin
108+
*/
79109
public static function serverCache(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = [])
80110
{
81111
return new self($pool, $streamFactory, $config);
@@ -206,12 +236,10 @@ protected function isCacheable(ResponseInterface $response)
206236
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) {
207237
return false;
208238
}
209-
if (!$this->config['respect_cache_headers']) {
210-
return true;
211-
}
212239

213-
foreach ($this->config['respect_response_cache_directives'] as $cacheDirective) {
214-
if ($this->getCacheControlDirective($response, $cacheDirective)) {
240+
$cacheableDirectives = $this->extractDirectives($this->config['respect_response_cache_directives'], $this->noCacheFlags);
241+
foreach ($cacheableDirectives as $cacheableDirective) {
242+
if ($this->getCacheControlDirective($response, $cacheableDirective)) {
215243
return false;
216244
}
217245
}
@@ -268,7 +296,7 @@ private function createCacheKey(RequestInterface $request)
268296
*/
269297
private function getMaxAge(ResponseInterface $response)
270298
{
271-
if (!$this->config['respect_cache_headers'] || !in_array('max-age', $this->config['respect_response_cache_directives'], true)) {
299+
if (!in_array('max-age', $this->config['respect_response_cache_directives'], true)) {
272300
return $this->config['default_ttl'];
273301
}
274302

@@ -306,7 +334,7 @@ private function configureOptions(OptionsResolver $resolver)
306334
'respect_cache_headers' => true,
307335
'hash_algo' => 'sha1',
308336
'methods' => ['GET', 'HEAD'],
309-
'respect_response_cache_directives' => ['no-cache', 'private', 'max-age'],
337+
'respect_response_cache_directives' => ['no-cache', 'private', 'max-age', 'no-store'],
310338
]);
311339

312340
$resolver->setAllowedTypes('cache_lifetime', ['int', 'null']);
@@ -328,6 +356,14 @@ private function configureOptions(OptionsResolver $resolver)
328356

329357
return $value;
330358
});
359+
360+
$resolver->setNormalizer('respect_response_cache_directives', function (Options $options, $value) {
361+
if (false === $options['respect_cache_headers']) {
362+
return [];
363+
}
364+
365+
return $value;
366+
});
331367
}
332368

333369
/**
@@ -392,4 +428,9 @@ private function getETag(CacheItemInterface $cacheItem)
392428
}
393429
}
394430
}
431+
432+
private function extractDirectives($directives, $extractDirectivesList)
433+
{
434+
return array_intersect($directives, $extractDirectivesList);
435+
}
395436
}

0 commit comments

Comments
 (0)