Skip to content

Commit 0e49971

Browse files
committed
Merge pull request #22 from php-http/dbu-patch-1
improve isCacheable readability
2 parents 01ea431 + 65ff8d8 commit 0e49971

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/CachePlugin.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ class CachePlugin implements Plugin
2828
private $defaultTtl;
2929

3030
/**
31-
* Look at the cache headers to know how long this response is going to be cached.
31+
* Look at the cache headers to know whether this response may be cached and to
32+
* decide how it can be cached.
3233
*
33-
* @var bool
34+
* @var bool Defaults to true
3435
*/
3536
private $respectCacheHeaders;
3637

3738
/**
39+
* Available options are
40+
* - respect_cache_headers: Whether to look at the cache directives or ignore them.
41+
*
3842
* @param CacheItemPoolInterface $pool
3943
* @param array $options
4044
*/
@@ -86,21 +90,26 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
8690
*/
8791
protected function isCacheable(ResponseInterface $response)
8892
{
89-
$cachableCodes = [200, 203, 300, 301, 302, 404, 410];
90-
$privateHeaders = $this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private');
93+
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) {
94+
return false;
95+
}
96+
if (!$this->respectCacheHeaders) {
97+
return true;
98+
}
99+
if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) {
100+
return false;
101+
}
91102

92-
// If http status code is cachable and if we respect the headers, make sure there is no private cache headers.
93-
return in_array($response->getStatusCode(), $cachableCodes) && !($this->respectCacheHeaders && $privateHeaders);
103+
return true;
94104
}
95105

96106
/**
97-
* Returns the value of a parameter in the cache control header. If not found we return false. If found with no
98-
* value return true.
107+
* Get the value of a parameter in the cache control header.
99108
*
100109
* @param ResponseInterface $response
101-
* @param string $name
110+
* @param string $name The field of Cache-Control to fetch
102111
*
103-
* @return bool|string
112+
* @return bool|string The value of the directive, true if directive without value, false if directive not present.
104113
*/
105114
private function getCacheControlDirective(ResponseInterface $response, $name)
106115
{

0 commit comments

Comments
 (0)