Slimore
  • Namespace
  • Class

Namespaces

  • None
  • Slimore
    • Cache
      • Exception
    • Captcha
    • Database
    • Debug
    • Http
    • Image
    • Log
    • Middleware
    • Mvc
    • Pagination
    • Upload

Classes

  • Slimore\Cache\File
  • Slimore\Captcha\Builder
  • Slimore\Database\Manager
  • Slimore\Debug\Simpler
  • Slimore\Http\Client
  • Slimore\Image\Gd
  • Slimore\Log\Writer
  • Slimore\Middleware\Exceptions
  • Slimore\Mvc\Application
  • Slimore\Mvc\Controller
  • Slimore\Mvc\Model
  • Slimore\Mvc\View
  • Slimore\Pagination\Paginator
  • Slimore\Upload\Uploader

Exceptions

  • Slimore\Cache\Exception\File
  • Slimore\Captcha\Exception
  • Slimore\Database\Exception
  • Slimore\Http\Exception
  • Slimore\Mvc\Exception
  • Slimore\Pagination\Exception
  • Slimore\Upload\Exception

Functions

  • arrayToObject
  • console
  • controller
  • ctl
  • currentUrl
  • decrypt
  • detectDevice
  • encrypt
  • fileFormatSize
  • getDirectoryItems
  • getDpi
  • hexToRGB
  • html
  • imageDataUrl
  • iPad
  • iPhone
  • iPod
  • isAndroid
  • isApache
  • isBlackberry
  • isChrome
  • isCli
  • isFirefox
  • isFreeBSD
  • isIE
  • isIIS
  • isJson
  • isLinux
  • isMacOSX
  • isMobile
  • isNginx
  • isOpera
  • isSafari
  • isTablet
  • isUnix
  • isUnixLike
  • isWebOS
  • isWindows
  • js
  • jsonToArray
  • linkTag
  • password
  • phoneMaskCode
  • randomCharacters
  • replaceDirSeparator
  • rgbToHex
  • script
  • shortUrl
  • style
  1 <?php
  2 
  3 /**
  4  * Slimore - The fully (H)MVC framework based on the Slim PHP framework.
  5  *
  6  * @author      Pandao <slimore@ipandao.com>
  7  * @copyright   2015 Pandao
  8  * @link        http://github.com/slimore/slimore
  9  * @license     MIT License https://github.com/slimore/slimore#license
 10  * @version     0.1.0
 11  * @package     Slimore\Cache
 12  */
 13 
 14 namespace Slimore\Cache;
 15 
 16 use \Slimore\Cache\Exception\File as FileCacheException;
 17 
 18 /**
 19  * Class File
 20  * @package Slimore\Cache
 21  */
 22 
 23 class File
 24 {
 25     /**
 26      * @var array
 27      */
 28 
 29     public static $keys = [];
 30 
 31     /**
 32      * @var int
 33      */
 34     public $expireTime = 3600;
 35 
 36     /**
 37      * @var string
 38      */
 39     protected $salt = 'slimore';
 40     /**
 41      * @var string
 42      */
 43     public $cachePath = '.';
 44 
 45     /**
 46      * @var string
 47      */
 48     public $cacheDirectory = '.caches';
 49 
 50     /**
 51      * @var string
 52      */
 53     public $fileExtension = '.php';
 54 
 55     /**
 56      * @var bool
 57      */
 58     public $base64Encode = true;
 59 
 60     /**
 61      * Constructor
 62      */
 63 
 64     public function __construct()
 65     {
 66 
 67     }
 68 
 69     /**
 70      * @param $salt
 71      */
 72     public function setSalt($salt)
 73     {
 74         $this->salt = $salt;
 75     }
 76 
 77     /**
 78      * Md5 encrypt cache filename
 79      *
 80      * @param string $key
 81      * @return string
 82      */
 83 
 84     protected function encrypt($key)
 85     {
 86         return md5(md5($key) . $this->salt);
 87     }
 88 
 89     /**
 90      * Get cache file path
 91      *
 92      * @return string
 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      * Get cache filename
105      *
106      * @param string $key
107      * @return string
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      * Set cache key and write to cache file
119      *
120      * @param string $key
121      * @param string $value
122      * @param int $expireTime null
123      * @return bool
124      * @throws InvalidArgumentException
125      * @throws FileCacheException
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      * Get cache key value
142      *
143      * @param $key
144      * @return mixed|null
145      */
146 
147     public function get($key)
148     {
149         return $this->read($key);
150     }
151 
152     /**
153      * Write to cache file
154      *
155      * @param string $file
156      * @param string $value
157      * @return bool
158      * @throws FileCacheException
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      * Read cache file
198      *
199      * @param string $file
200      * @return mixed|null
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             // if cache file not expire
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      * Setter
232      *
233      * @param string $key
234      * @param string $value
235      * @return bool
236      */
237 
238     public function __set($key, $value)
239     {
240         return $this->set($key, $value);
241     }
242 
243     /**
244      * Getter
245      *
246      * @param $key
247      * @return mixed|null
248      */
249 
250     public function __get($key)
251     {
252         return $this->get($key);
253     }
254 
255     /**
256      * Check has property key, when using isset() or empty()
257      *
258      * @param string $key
259      * @return bool
260      */
261 
262     public function __isset($key)
263     {
264         return $this->has($key);
265     }
266 
267     /**
268      * Check has cache key
269      *
270      * @param string $key
271      * @return bool
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         // if cache file expire
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      * Delete property key, when using unset()
301      *
302      * @param string $key
303      * @return bool
304      */
305 
306     public function __unset($key)
307     {
308         return $this->remove($key);
309     }
310 
311     /**
312      * Remove cache key
313      *
314      * @param string $key
315      * @return bool
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      * Clear expire cache file
334      *
335      * @return void
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             // if cache file expire, delete cache file
346             if ( time() > filemtime($file) )
347             {
348                 @unlink($file);
349             }
350         }
351     }
352 
353     /**
354      * Delete all cache files
355      *
356      * @return void
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 }
Slimore API documentation generated by ApiGen