|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 1998-2015 Browser Capabilities Project |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a |
|
6
|
|
|
* copy of this software and associated documentation files (the "Software"), |
|
7
|
|
|
* to deal in the Software without restriction, including without limitation |
|
8
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|
9
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
|
10
|
|
|
* Software is furnished to do so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included |
|
13
|
|
|
* in all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
16
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
* THE SOFTWARE. |
|
22
|
|
|
* |
|
23
|
|
|
* @category Browscap-PHP |
|
24
|
|
|
* @package Cache |
|
25
|
|
|
* @copyright 1998-2015 Browser Capabilities Project |
|
26
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
|
27
|
|
|
* @link https://github.com/browscap/browscap-php/ |
|
28
|
|
|
* @since added with version 3.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
namespace BrowscapPHP\Cache; |
|
32
|
|
|
|
|
33
|
|
|
use WurflCache\Adapter\AdapterInterface; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* a cache proxy to be able to use the cache adapters provided by the WurflCache package |
|
37
|
|
|
* |
|
38
|
|
|
* @category Browscap-PHP |
|
39
|
|
|
* @package Cache |
|
40
|
|
|
* @author Thomas Müller <[email protected]> |
|
41
|
|
|
* @copyright Copyright (c) 1998-2015 Browser Capabilities Project |
|
42
|
|
|
* @version 3.0 |
|
43
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
|
44
|
|
|
* @link https://github.com/browscap/browscap-php/ |
|
45
|
|
|
*/ |
|
46
|
|
|
class BrowscapCache implements BrowscapCacheInterface |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* Path to the cache directory |
|
50
|
|
|
* |
|
51
|
|
|
* @var \WurflCache\Adapter\AdapterInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
private $cache = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Detected browscap version (read from INI file) |
|
57
|
|
|
* |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
private $version = null; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Release date of the Browscap data (read from INI file) |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
private $releaseDate; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Constructor class, checks for the existence of (and loads) the cache and |
|
71
|
|
|
* if needed updated the definitions |
|
72
|
|
|
* |
|
73
|
|
|
* @param \WurflCache\Adapter\AdapterInterface $adapter |
|
74
|
|
|
* @param int $updateInterval |
|
75
|
|
|
*/ |
|
76
|
12 |
|
public function __construct(AdapterInterface $adapter, $updateInterval = BrowscapCacheInterface::CACHE_LIVETIME) |
|
77
|
|
|
{ |
|
78
|
12 |
|
$this->cache = $adapter; |
|
79
|
12 |
|
$this->cache->setExpiration((int) $updateInterval); |
|
80
|
12 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Gets the version of the Browscap data |
|
84
|
|
|
* |
|
85
|
|
|
* @return int |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function getVersion() |
|
88
|
|
|
{ |
|
89
|
4 |
|
if ($this->version === null) { |
|
90
|
4 |
|
$success = true; |
|
91
|
|
|
|
|
92
|
4 |
|
$version = $this->getItem('browscap.version', false, $success); |
|
93
|
|
|
|
|
94
|
4 |
|
if ($version !== null && $success) { |
|
95
|
3 |
|
$this->version = (int) $version; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
return $this->version; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets the release date of the Browscap data |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getReleaseDate() |
|
108
|
|
|
{ |
|
109
|
|
|
if ($this->releaseDate === null) { |
|
110
|
|
|
$success = true; |
|
111
|
|
|
|
|
112
|
|
|
$releaseDate = $this->getItem('browscap.releaseDate', false, $success); |
|
113
|
|
|
|
|
114
|
|
|
if ($releaseDate !== null && $success) { |
|
115
|
|
|
$this->releaseDate = $releaseDate; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->releaseDate; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get an item. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $cacheId |
|
126
|
|
|
* @param bool $withVersion |
|
127
|
|
|
* @param bool $success |
|
128
|
|
|
* |
|
129
|
|
|
* @return mixed Data on success, null on failure |
|
130
|
|
|
*/ |
|
131
|
4 |
|
public function getItem($cacheId, $withVersion = true, & $success = null) |
|
132
|
|
|
{ |
|
133
|
4 |
|
if ($withVersion) { |
|
134
|
|
|
$cacheId .= '.'.$this->getVersion(); |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
4 |
|
if (!$this->cache->hasItem($cacheId)) { |
|
138
|
4 |
|
$success = false; |
|
139
|
|
|
|
|
140
|
4 |
|
return null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
3 |
|
$success = null; |
|
144
|
3 |
|
$data = $this->cache->getItem($cacheId, $success); |
|
145
|
|
|
|
|
146
|
3 |
|
if (!$success) { |
|
|
|
|
|
|
147
|
|
|
$success = false; |
|
148
|
|
|
|
|
149
|
|
|
return null; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
3 |
|
if (!isset($data['content'])) { |
|
153
|
|
|
$success = false; |
|
154
|
|
|
|
|
155
|
|
|
return null; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
$success = true; |
|
159
|
|
|
|
|
160
|
3 |
|
return unserialize($data['content']); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* save the content into an php file |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $cacheId The cache id |
|
167
|
|
|
* @param mixed $content The content to store |
|
168
|
|
|
* @param bool $withVersion |
|
169
|
|
|
* |
|
170
|
|
|
* @return boolean whether the file was correctly written to the disk |
|
171
|
|
|
*/ |
|
172
|
3 |
|
public function setItem($cacheId, $content, $withVersion = true) |
|
173
|
|
|
{ |
|
174
|
|
|
// Get the whole PHP code |
|
175
|
|
|
$data = array( |
|
176
|
3 |
|
'content' => serialize($content), |
|
177
|
3 |
|
); |
|
178
|
|
|
|
|
179
|
3 |
|
if ($withVersion) { |
|
180
|
3 |
|
$cacheId .= '.'.$this->getVersion(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// Save and return |
|
184
|
3 |
|
return $this->cache->setItem($cacheId, $data); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Test if an item exists. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $cacheId |
|
191
|
|
|
* @param bool $withVersion |
|
192
|
|
|
* |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function hasItem($cacheId, $withVersion = true) |
|
196
|
|
|
{ |
|
197
|
1 |
|
if ($withVersion) { |
|
198
|
1 |
|
$cacheId .= '.'.$this->getVersion(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
1 |
|
return $this->cache->hasItem($cacheId); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Remove an item. |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $cacheId |
|
208
|
|
|
* @param bool $withVersion |
|
209
|
|
|
* |
|
210
|
|
|
* @return bool |
|
211
|
|
|
*/ |
|
212
|
|
|
public function removeItem($cacheId, $withVersion = true) |
|
213
|
|
|
{ |
|
214
|
|
|
if ($withVersion) { |
|
215
|
|
|
$cacheId .= '.'.$this->getVersion(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $this->cache->removeItem($cacheId); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Flush the whole storage |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool |
|
225
|
|
|
*/ |
|
226
|
|
|
public function flush() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->cache->flush(); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.