1 | <?php |
||
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() |
|
101 | |||
102 | /** |
||
103 | * Gets the release date of the Browscap data |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function getReleaseDate() |
||
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() |
||
230 | } |
||
231 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.