|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Client\Tools\Promise\FulfilledPromise; |
|
6
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
7
|
|
|
use Psr\Http\Message\RequestInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Allow for caching a response. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class CachePlugin implements Plugin |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CacheItemPoolInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $pool; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Default time to store object in cache. This value is used if CachePlugin::respectCacheHeaders is false or |
|
24
|
|
|
* if cache headers are missing. |
|
25
|
|
|
* |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
private $defaultTtl; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Look at the cache headers to know how long this response is going to be cached. |
|
32
|
|
|
* |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
private $respectCacheHeaders; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param CacheItemPoolInterface $pool |
|
39
|
|
|
* @param array $options |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(CacheItemPoolInterface $pool, array $options = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->pool = $pool; |
|
44
|
|
|
$this->defaultTtl = isset($options['default_ttl']) ? $options['default_ttl'] : null; |
|
45
|
|
|
$this->respectCacheHeaders = isset($options['respect_cache_headers']) ? $options['respect_cache_headers'] : true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
52
|
|
|
{ |
|
53
|
|
|
$method = strtoupper($request->getMethod()); |
|
54
|
|
|
|
|
55
|
|
|
// if the request not is cachable, move to $next |
|
56
|
|
|
if ($method !== 'GET' && $method !== 'HEAD') { |
|
57
|
|
|
return $next($request); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// If we can cache the request |
|
61
|
|
|
$key = $this->createCacheKey($request); |
|
62
|
|
|
$cacheItem = $this->pool->getItem($key); |
|
63
|
|
|
|
|
64
|
|
|
if ($cacheItem->isHit()) { |
|
65
|
|
|
// return cached response |
|
66
|
|
|
return new FulfilledPromise($cacheItem->get()); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { |
|
70
|
|
|
if ($this->isCacheable($response)) { |
|
71
|
|
|
$cacheItem->set($response) |
|
72
|
|
|
->expiresAfter($this->getMaxAge($response)); |
|
73
|
|
|
$this->pool->save($cacheItem); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $response; |
|
77
|
|
|
}); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Verify that we can cache this response. |
|
82
|
|
|
* |
|
83
|
|
|
* @param ResponseInterface $response |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function isCacheable(ResponseInterface $response) |
|
88
|
|
|
{ |
|
89
|
|
|
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the value of a parameter in the cache control header. If not found we return false. If found with no |
|
101
|
|
|
* value return true. |
|
102
|
|
|
* |
|
103
|
|
|
* @param ResponseInterface $response |
|
104
|
|
|
* @param string $name |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool|string |
|
107
|
|
|
*/ |
|
108
|
|
|
private function getCacheControlDirective(ResponseInterface $response, $name) |
|
109
|
|
|
{ |
|
110
|
|
|
$headers = $response->getHeader('Cache-Control'); |
|
111
|
|
|
foreach ($headers as $header) { |
|
112
|
|
|
if (preg_match(sprintf('|%s=?([0-9]+)?|i', $name), $header, $matches)) { |
|
113
|
|
|
|
|
114
|
|
|
// return the value for $name if it exists |
|
115
|
|
|
if (isset($matches[1])) { |
|
116
|
|
|
return $matches[1]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param RequestInterface $request |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
private function createCacheKey(RequestInterface $request) |
|
132
|
|
|
{ |
|
133
|
|
|
return md5($request->getMethod().' '.$request->getUri()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get a ttl in seconds. It could return null if we do not respect cache headers and got no defaultTtl. |
|
138
|
|
|
* |
|
139
|
|
|
* @param ResponseInterface $response |
|
140
|
|
|
* |
|
141
|
|
|
* @return int|null |
|
142
|
|
|
*/ |
|
143
|
|
|
private function getMaxAge(ResponseInterface $response) |
|
144
|
|
|
{ |
|
145
|
|
|
if (!$this->respectCacheHeaders) { |
|
146
|
|
|
return $this->defaultTtl; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// check for max age in the Cache-Control header |
|
150
|
|
|
$maxAge = $this->getCacheControlDirective($response, 'max-age'); |
|
151
|
|
|
if (!is_bool($maxAge)) { |
|
152
|
|
|
$ageHeaders = $response->getHeader('Age'); |
|
153
|
|
|
foreach ($ageHeaders as $age) { |
|
154
|
|
|
return $maxAge - ((int) $age); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $maxAge; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// check for ttl in the Expires header |
|
161
|
|
|
$headers = $response->getHeader('Expires'); |
|
162
|
|
|
foreach ($headers as $header) { |
|
163
|
|
|
return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this->defaultTtl; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.