1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Utils; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
7
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore; |
8
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore; |
9
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore; |
10
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore; |
11
|
|
|
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException; |
12
|
|
|
use Silviooosilva\CacheerPhp\Helpers\EnvHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CacheDriver |
16
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
17
|
|
|
* @package Silviooosilva\CacheerPhp |
18
|
|
|
*/ |
19
|
|
|
class CacheDriver |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Cacheer |
24
|
|
|
*/ |
25
|
|
|
protected Cacheer $cacheer; |
26
|
|
|
|
27
|
|
|
/** @param string $logPath */ |
28
|
|
|
public string $logPath = 'cacheer.log'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* CacheDriver constructor. |
32
|
|
|
* |
33
|
|
|
* @param Cacheer $cacheer |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Cacheer $cacheer) |
36
|
|
|
{ |
37
|
|
|
$this->cacheer = $cacheer; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Uses the database driver for caching. |
42
|
|
|
* |
43
|
|
|
* @return Cacheer |
44
|
|
|
*/ |
45
|
|
|
public function useDatabaseDriver(): Cacheer |
46
|
|
|
{ |
47
|
|
|
$this->cacheer->cacheStore = new DatabaseCacheStore($this->logPath, $this->cacheer->options); |
48
|
|
|
return $this->cacheer; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Uses the file driver for caching. |
53
|
|
|
* |
54
|
|
|
* @return Cacheer |
55
|
|
|
*/ |
56
|
|
|
public function useFileDriver(): Cacheer |
57
|
|
|
{ |
58
|
|
|
$this->cacheer->options['loggerPath'] = $this->logPath; |
59
|
|
|
$this->cacheer->cacheStore = new FileCacheStore($this->cacheer->options); |
60
|
|
|
return $this->cacheer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Uses the Redis driver for caching. |
65
|
|
|
* |
66
|
|
|
* @return Cacheer |
67
|
|
|
*/ |
68
|
|
|
public function useRedisDriver(): Cacheer |
69
|
|
|
{ |
70
|
|
|
$this->cacheer->cacheStore = new RedisCacheStore($this->logPath, $this->cacheer->options); |
71
|
|
|
return $this->cacheer; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Uses the array driver for caching. |
76
|
|
|
* |
77
|
|
|
* @return Cacheer |
78
|
|
|
*/ |
79
|
|
|
public function useArrayDriver(): Cacheer |
80
|
|
|
{ |
81
|
|
|
$this->cacheer->cacheStore = new ArrayCacheStore($this->logPath); |
82
|
|
|
return $this->cacheer; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Uses the default driver for caching. |
87
|
|
|
* |
88
|
|
|
* @return Cacheer |
89
|
|
|
*/ |
90
|
|
|
public function useDefaultDriver(): Cacheer |
91
|
|
|
{ |
92
|
|
|
if (!isset($this->cacheer->options['cacheDir'])) { |
93
|
|
|
$projectRoot = EnvHelper::getRootPath(); |
94
|
|
|
$cacheDir = $projectRoot . DIRECTORY_SEPARATOR . "CacheerPHP" . DIRECTORY_SEPARATOR . "Cache"; |
95
|
|
|
if ($this->isDir($cacheDir)) { |
96
|
|
|
$this->cacheer->options['cacheDir'] = $cacheDir; |
97
|
|
|
} else { |
98
|
|
|
throw CacheFileException::create("Failed to create cache directory: " . $cacheDir); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
$this->useFileDriver(); |
102
|
|
|
return $this->cacheer; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks if the directory exists or creates it. |
107
|
|
|
* |
108
|
|
|
* @param mixed $dirName |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
private function isDir(mixed $dirName): bool |
112
|
|
|
{ |
113
|
|
|
if (is_dir($dirName)) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
return mkdir($dirName, 0755, true); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|