-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathCachedClient.php
143 lines (112 loc) · 2.98 KB
/
CachedClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Milo\Github\Http;
use Milo\Github;
use Milo\Github\Storages;
/**
* Caching for HTTP clients.
*
* @author Miloslav Hůla (https://github.com/milo)
*/
class CachedClient extends Github\Sanity implements IClient
{
/** @var Storages\ICache|NULL */
private $cache;
/** @var IClient */
private $client;
/** @var bool */
private $forbidRecheck;
/** @var callable|NULL */
private $onResponse;
/**
* @param Storages\ICache
* @param IClient
* @param bool forbid checking Github for new data; more or less development purpose only
*/
public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE)
{
$this->cache = $cache;
$this->client = $client ?: Github\Helpers::createDefaultClient();
$this->forbidRecheck = (bool) $forbidRecheck;
}
/**
* @return IClient
*/
public function getInnerClient()
{
return $this->client;
}
/**
* @return Response
*
* @throws BadResponseException
*/
public function request(Request $request)
{
$request = clone $request;
$cacheKey = implode('.', [
$request->getMethod(),
$request->getUrl(),
/** @todo This should depend on Vary: header */
$request->getHeader('Accept'),
$request->getHeader('Accept-Encoding'),
$request->getHeader('Authorization')
]);
if ($cached = $this->cache->load($cacheKey)) {
if ($this->forbidRecheck) {
$cached = clone $cached;
$this->onResponse && call_user_func($this->onResponse, $cached);
return $cached;
}
/** @var $cached Response */
if ($cached->hasHeader('Last-Modified')) {
$request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified'));
}
if ($cached->hasHeader('ETag')) {
$request->addHeader('If-None-Match', $cached->getHeader('ETag'));
}
}
$response = $this->client->request($request);
if ($this->isCacheable($response)) {
$this->cache->save($cacheKey, clone $response);
}
if (isset($cached) && $response->getCode() === Response::S304_NOT_MODIFIED) {
$cached = clone $cached;
/** @todo Should be responses somehow combined into one? */
$response = $cached->setPrevious($response);
}
$this->onResponse && call_user_func($this->onResponse, $response);
return $response;
}
/**
* @param callable|NULL function(Request $request)
* @return self
*/
public function onRequest($callback)
{
$this->client->onRequest($callback);
return $this;
}
/**
* @param callable|NULL function(Response $response)
* @return self
*/
public function onResponse($callback)
{
$this->client->onResponse(NULL);
$this->onResponse = $callback;
return $this;
}
/**
* @return bool
*/
protected function isCacheable(Response $response)
{
/** @todo Do it properly. Vary:, Pragma:, TTL... */
if (!$response->isCode(200)) {
return FALSE;
} elseif (preg_match('#max-age=0|must-revalidate#i', $response->getHeader('Cache-Control', ''))) {
return FALSE;
}
return $response->hasHeader('ETag') || $response->hasHeader('Last-Modified');
}
}