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

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