1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Cache;
15
16 use \Slimore\Cache\Exception\File as FileCacheException;
17
18 19 20 21
22
23 class File
24 {
25 26 27
28
29 public static $keys = [];
30
31 32 33
34 public $expireTime = 3600;
35
36 37 38
39 protected $salt = 'slimore';
40 41 42
43 public $cachePath = '.';
44
45 46 47
48 public $cacheDirectory = '.caches';
49
50 51 52
53 public $fileExtension = '.php';
54
55 56 57
58 public $base64Encode = true;
59
60 61 62
63
64 public function __construct()
65 {
66
67 }
68
69 70 71
72 public function setSalt($salt)
73 {
74 $this->salt = $salt;
75 }
76
77 78 79 80 81 82
83
84 protected function encrypt($key)
85 {
86 return md5(md5($key) . $this->salt);
87 }
88
89 90 91 92 93
94
95 protected function getFilePath()
96 {
97 $path = $this->cachePath . DIRECTORY_SEPARATOR . $this->cacheDirectory . DIRECTORY_SEPARATOR;
98 $path = str_replace(['\\\\', '//'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $path);
99
100 return $path;
101 }
102
103 104 105 106 107 108
109
110 protected function getFileName($key)
111 {
112 $file = $this->getFilePath() . $this->encrypt($key) . $this->fileExtension;
113
114 return $file;
115 }
116
117 118 119 120 121 122 123 124 125 126
127
128 public function set($key, $value, $expireTime = null)
129 {
130 if ( $expireTime && !is_int($expireTime) )
131 {
132 throw new \InvalidArgumentException('cache expire time must be integer.');
133 }
134
135 $this->expireTime = ($expireTime) ? $expireTime : $this->expireTime;
136
137 return $this->write($key, $value);
138 }
139
140 141 142 143 144 145
146
147 public function get($key)
148 {
149 return $this->read($key);
150 }
151
152 153 154 155 156 157 158 159
160
161 protected function write($file, $value)
162 {
163 $key = $file;
164 $path = $this->getFilePath();
165
166 if ( !file_exists($dir) )
167 {
168 mkdir($path, 0777, true);
169 }
170
171 $file = $this->getFileName($key);
172
173 $value = serialize($value);
174 $value = ($this->base64Encode) ? base64_encode($value) : $value;
175
176 if ( !file_put_contents($file, $value) )
177 {
178 throw new FileCacheException('File write failure.');
179 }
180
181 if ( !chmod($file, 0777) )
182 {
183 throw new FileCacheException('Failed to set file permissions.');
184 }
185
186 if ( !touch($file, time() + $this->expireTime) )
187 {
188 throw new FileCacheException('Failed to change file time.');
189 }
190
191 static::$keys[$key] = true;
192
193 return true;
194 }
195
196 197 198 199 200 201
202
203 protected function read($file)
204 {
205 $data = null;
206 $key = $file;
207 $file = $this->getFileName($file);
208
209 if ( file_exists($file))
210 {
211
212 if (filemtime($file) >= time())
213 {
214 echo "read cache<br/>";
215 $data = file_get_contents($file);
216 $data = ($this->base64Encode) ? base64_decode($data) : $data;
217 $data = unserialize($data);
218 static::$keys[$key] = true;
219 }
220 else
221 {
222 unset(static::$keys[$key]);
223 unlink($file);
224 }
225 }
226
227 return $data;
228 }
229
230 231 232 233 234 235 236
237
238 public function __set($key, $value)
239 {
240 return $this->set($key, $value);
241 }
242
243 244 245 246 247 248
249
250 public function __get($key)
251 {
252 return $this->get($key);
253 }
254
255 256 257 258 259 260
261
262 public function __isset($key)
263 {
264 return $this->has($key);
265 }
266
267 268 269 270 271 272
273
274 public function has($key)
275 {
276 $file = $this->getFileName($key);
277
278 if ( !file_exists($file) )
279 {
280 return false;
281 }
282
283
284 if ( filemtime($file) >= time() )
285 {
286 static::$keys[$key] = true;
287
288 return true;
289 }
290 else
291 {
292 unset(static::$keys[$key]);
293 unlink($file);
294
295 return false;
296 }
297 }
298
299 300 301 302 303 304
305
306 public function __unset($key)
307 {
308 return $this->remove($key);
309 }
310
311 312 313 314 315 316
317
318 public function remove($key)
319 {
320 $file = $this->getFileName($key);
321
322 if ( !file_exists($file) )
323 {
324 return false;
325 }
326
327 unset(static::$keys[$key]);
328
329 return (unlink($file))? true : false;
330 }
331
332 333 334 335 336
337
338 public function clear()
339 {
340 echo $path = $this->getFilePath();
341 $files = glob($path . '*' . $this->fileExtension);
342
343 foreach ($files as $file)
344 {
345
346 if ( time() > filemtime($file) )
347 {
348 @unlink($file);
349 }
350 }
351 }
352
353 354 355 356 357
358
359 public function clearAll()
360 {
361 $path = $this->getFilePath();
362 $files = glob($path . '*' . $this->fileExtension);
363
364 foreach ($files as $file)
365 {
366 @unlink($file);
367 }
368
369 static::$keys = [];
370 }
371 }