Просмотр файла engine/classes/cache.class.php

Размер файла: 1.72Kb
  1. <?php
  2. Class Cache
  3. {
  4. private $_file, $_result;
  5.  
  6. function __construct($file)
  7. {
  8. $this->_file = $file;
  9. }
  10.  
  11. public function life($expiry = 21600)
  12. {
  13. return file_exists($this->_file) && filemtime($this->_file) > (time() - $expiry);
  14. }
  15. public function read()
  16. {
  17. return empty($this->_result) ? file_get_contents($this->_file) : $this->_result;
  18. }
  19.  
  20. public function write($data = false)
  21. {
  22. return file_put_contents($this->_file, $data ? $data : ob_get_clean());
  23. }
  24.  
  25. public function delete()
  26. {
  27. if (file_exists($this->_file))
  28. unlink($this->_file);
  29. }
  30.  
  31. public static function multi_delete($needly, $dir = tmpDir)
  32. {
  33. $dir_open = opendir($dir);
  34. while ($file = readdir($dir_open))
  35. {
  36. if ($file == '.' || $file == '..')
  37. continue;
  38. if (strstr($file, $needly))
  39. unlink($dir . $file);
  40. }
  41. }
  42.  
  43. public function replace($param1, $param2 = false)
  44. {
  45. if (empty($this->_result))
  46. $this->_result = file_get_contents($this->_file);
  47.  
  48. $this->_result = str_replace(
  49. (is_array($param1) ? array_keys($param1) : $param1),
  50. (!$param2 ? array_values($param1) : $param2),
  51. $this->_result);
  52.  
  53. return $this;
  54. }
  55.  
  56. public function helper($array = false)
  57. {
  58. if (!$array)
  59. $array = array('<a href="/login.php">Войти на сайт</a>' => '<a href="/pages/menu.php">Кабинет</a> | <a href="/login.php?exit">Выход</a>');
  60.  
  61. $this->_replace = $this->replace(!Core::$user_id ? array_flip($array) : $array);
  62. return $this;
  63. }
  64. }