Cache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * All cache logic
  4. * Note : adapter are store in other place
  5. */
  6. interface CacheInterface{
  7. public function loadData();
  8. public function saveData($datas);
  9. public function getTime();
  10. }
  11. abstract class CacheAbstract implements CacheInterface{
  12. protected $param;
  13. public function prepare(array $param){
  14. $this->param = $param;
  15. return $this;
  16. }
  17. }
  18. class Cache{
  19. static protected $dirCache;
  20. public function __construct(){
  21. throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
  22. }
  23. static public function create($nameCache){
  24. if( !static::isValidNameCache($nameCache) ){
  25. throw new \InvalidArgumentException('Name cache must be at least one uppercase follow or not by alphanumeric or dash characters.');
  26. }
  27. $pathCache = self::getDir() . $nameCache . '.php';
  28. if( !file_exists($pathCache) ){
  29. throw new \Exception('The cache you looking for does not exist.');
  30. }
  31. require_once $pathCache;
  32. return new $nameCache();
  33. }
  34. static public function setDir($dirCache){
  35. if( !is_string($dirCache) ){
  36. throw new \InvalidArgumentException('Dir cache must be a string.');
  37. }
  38. if( !file_exists($dirCache) ){
  39. throw new \Exception('Dir cache does not exist.');
  40. }
  41. self::$dirCache = $dirCache;
  42. }
  43. static public function getDir(){
  44. $dirCache = self::$dirCache;
  45. if( is_null($dirCache) ){
  46. throw new \LogicException(__CLASS__ . ' class need to know cache path !');
  47. }
  48. return $dirCache;
  49. }
  50. static public function isValidNameCache($nameCache){
  51. return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
  52. }
  53. static public function utf8_encode_deep(&$input) {
  54. if (is_string($input)) {
  55. $input = utf8_encode($input);
  56. } else if (is_array($input)) {
  57. foreach ($input as &$value) {
  58. Cache::utf8_encode_deep($value);
  59. }
  60. unset($value);
  61. } else if (is_object($input)) {
  62. $vars = array_keys(get_object_vars($input));
  63. foreach ($vars as $var) {
  64. Cache::utf8_encode_deep($input->$var);
  65. }
  66. }
  67. }
  68. static public function purge() {
  69. $cacheTimeLimit = time() - 60*60*24 ;
  70. $cachePath = 'cache';
  71. if(file_exists($cachePath)) {
  72. $cacheIterator = new RecursiveIteratorIterator(
  73. new RecursiveDirectoryIterator($cachePath),
  74. RecursiveIteratorIterator::CHILD_FIRST
  75. );
  76. foreach ($cacheIterator as $cacheFile) {
  77. if (in_array($cacheFile->getBasename(), array('.', '..')))
  78. continue;
  79. elseif ($cacheFile->isFile()) {
  80. if( filemtime($cacheFile->getPathname()) < $cacheTimeLimit )
  81. unlink( $cacheFile->getPathname() );
  82. }
  83. }
  84. }
  85. }
  86. }