1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Cache; |
5
|
|
|
|
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A cache proxy to be able to use the cache adapters provided by the WurflCache package |
12
|
|
|
*/ |
13
|
|
|
final class BrowscapCache implements BrowscapCacheInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Path to the cache directory |
17
|
|
|
* |
18
|
|
|
* @var \Psr\SimpleCache\CacheInterface |
19
|
|
|
*/ |
20
|
|
|
private $cache = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Psr\Log\LoggerInterface|null |
24
|
|
|
*/ |
25
|
|
|
private $logger; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Detected browscap version (read from INI file) |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $version = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Release date of the Browscap data (read from INI file) |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $releaseDate; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Type of the Browscap data (read from INI file) |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $type; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor class, checks for the existence of (and loads) the cache and |
50
|
|
|
* if needed updated the definitions |
51
|
|
|
* |
52
|
|
|
* @param \Psr\SimpleCache\CacheInterface $adapter |
53
|
|
|
* @param LoggerInterface $logger |
54
|
|
|
*/ |
55
|
4 |
|
public function __construct(CacheInterface $adapter, LoggerInterface $logger) |
56
|
|
|
{ |
57
|
4 |
|
$this->cache = $adapter; |
58
|
4 |
|
$this->logger = $logger; |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets the version of the Browscap data |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
2 |
|
public function getVersion() : ?int |
67
|
|
|
{ |
68
|
2 |
|
if ($this->version === null) { |
69
|
2 |
|
$success = null; |
70
|
|
|
|
71
|
|
|
try { |
72
|
2 |
|
$version = $this->getItem('browscap.version', false, $success); |
73
|
|
|
} catch (InvalidArgumentException $e) { |
74
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while reading the data version from the cache', 0, $e)); |
75
|
|
|
$version = null; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if ($version !== null && $success) { |
79
|
1 |
|
$this->version = (int) $version; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return $this->version; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the release date of the Browscap data |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
1 |
|
public function getReleaseDate() : ?string |
92
|
|
|
{ |
93
|
1 |
|
if ($this->releaseDate === null) { |
94
|
1 |
|
$success = null; |
95
|
|
|
|
96
|
|
|
try { |
97
|
1 |
|
$releaseDate = $this->getItem('browscap.releaseDate', false, $success); |
98
|
|
|
} catch (InvalidArgumentException $e) { |
99
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while reading the data release date from the cache', 0, $e)); |
100
|
|
|
$releaseDate = null; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
if ($releaseDate !== null && $success) { |
104
|
1 |
|
$this->releaseDate = $releaseDate; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $this->releaseDate; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets the type of the Browscap data |
113
|
|
|
*/ |
114
|
1 |
|
public function getType() : ?string |
115
|
|
|
{ |
116
|
1 |
|
if ($this->type === null) { |
117
|
1 |
|
$success = null; |
118
|
|
|
|
119
|
|
|
try { |
120
|
1 |
|
$type = $this->getItem('browscap.type', false, $success); |
121
|
|
|
} catch (InvalidArgumentException $e) { |
122
|
|
|
$this->logger->error(new \InvalidArgumentException('an error occured while reading the data type from the cache', 0, $e)); |
123
|
|
|
$type = null; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
if ($type !== null && $success) { |
127
|
1 |
|
$this->type = $type; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return $this->type; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get an item. |
136
|
|
|
* |
137
|
|
|
* @param string $cacheId |
138
|
|
|
* @param bool $withVersion |
139
|
|
|
* @param bool &$success |
140
|
|
|
* |
141
|
|
|
* @return mixed Data on success, null on failure |
142
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
143
|
|
|
*/ |
144
|
3 |
|
public function getItem(string $cacheId, bool $withVersion = true, ?bool &$success = null) |
145
|
|
|
{ |
146
|
3 |
|
if ($withVersion) { |
147
|
|
|
$cacheId .= '.' . $this->getVersion(); |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
if (! $this->cache->has($cacheId)) { |
151
|
3 |
|
$success = false; |
152
|
|
|
|
153
|
3 |
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
3 |
|
$data = $this->cache->get($cacheId); |
157
|
|
|
|
158
|
3 |
|
if (! is_array($data) || ! array_key_exists('content', $data)) { |
159
|
|
|
$success = false; |
160
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
$success = true; |
165
|
|
|
|
166
|
3 |
|
return unserialize($data['content']); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* save the content into an php file |
171
|
|
|
* |
172
|
|
|
* @param string $cacheId The cache id |
173
|
|
|
* @param mixed $content The content to store |
174
|
|
|
* @param bool $withVersion |
175
|
|
|
* |
176
|
|
|
* @return bool whether the file was correctly written to the disk |
177
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
178
|
|
|
*/ |
179
|
3 |
|
public function setItem(string $cacheId, $content, bool $withVersion = true) : bool |
180
|
|
|
{ |
181
|
|
|
// Get the whole PHP code |
182
|
|
|
$data = [ |
183
|
3 |
|
'content' => serialize($content), |
184
|
|
|
]; |
185
|
|
|
|
186
|
3 |
|
if ($withVersion) { |
187
|
|
|
$cacheId .= '.' . $this->getVersion(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// Save and return |
191
|
3 |
|
return $this->cache->set($cacheId, $data); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Test if an item exists. |
196
|
|
|
* |
197
|
|
|
* @param string $cacheId |
198
|
|
|
* @param bool $withVersion |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
202
|
|
|
*/ |
203
|
|
|
public function hasItem(string $cacheId, bool $withVersion = true) : bool |
204
|
|
|
{ |
205
|
|
|
if ($withVersion) { |
206
|
|
|
$cacheId .= '.' . $this->getVersion(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
return $this->cache->has($cacheId); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Remove an item. |
215
|
|
|
* |
216
|
|
|
* @param string $cacheId |
217
|
|
|
* @param bool $withVersion |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
221
|
|
|
*/ |
222
|
|
|
public function removeItem(string $cacheId, bool $withVersion = true) : bool |
223
|
|
|
{ |
224
|
|
|
if ($withVersion) { |
225
|
|
|
$cacheId .= '.' . $this->getVersion(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $this->cache->delete($cacheId); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Flush the whole storage |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
|
|
public function flush() : bool |
237
|
|
|
{ |
238
|
|
|
return $this->cache->clear(); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|