1 | <?php |
||
2 | |||
3 | namespace Silviooosilva\CacheerPhp; |
||
4 | |||
5 | use Closure; |
||
6 | use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore; |
||
7 | use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore; |
||
8 | use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore; |
||
9 | use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore; |
||
10 | use Silviooosilva\CacheerPhp\Helpers\CacheConfig; |
||
11 | use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter; |
||
12 | use Silviooosilva\CacheerPhp\Utils\CacheDriver; |
||
13 | use RuntimeException; |
||
14 | use Silviooosilva\CacheerPhp\Service\CacheRetriever; |
||
15 | use Silviooosilva\CacheerPhp\Service\CacheMutator; |
||
16 | use BadMethodCallException; |
||
17 | |||
18 | /** |
||
19 | * Class CacheerPHP |
||
20 | * |
||
21 | * @author SÃlvio Silva <https://github.com/silviooosilva> |
||
22 | * @package Silviooosilva\CacheerPhp |
||
23 | * |
||
24 | * @method static bool add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
||
25 | * @method bool add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
||
26 | * @method static bool appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
||
27 | * @method bool appendCache(string $cacheKey, mixed $cacheData, string $namespace = '') |
||
28 | * @method static bool clearCache(string $cacheKey, string $namespace = '') |
||
29 | * @method bool clearCache(string $cacheKey, string $namespace = '') |
||
30 | * @method static bool decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
||
31 | * @method bool decrement(string $cacheKey, int $amount = 1, string $namespace = '') |
||
32 | * @method static bool flushCache() |
||
33 | * @method bool flushCache() |
||
34 | * @method static bool forever(string $cacheKey, mixed $cacheData) |
||
35 | * @method bool forever(string $cacheKey, mixed $cacheData) |
||
36 | * @method static mixed getAndForget(string $cacheKey, string $namespace = '') |
||
37 | * @method mixed getAndForget(string $cacheKey, string $namespace = '') |
||
38 | * @method static CacheDataFormatter|mixed getAll(string $namespace = '') |
||
39 | * @method CacheDataFormatter|mixed getAll(string $namespace = '') |
||
40 | * @method static mixed getCache(string $cacheKey, string $namespace = '', int|string $ttl = 3600) |
||
41 | * @method mixed getCache(string $cacheKey, string $namespace = '', int|string $ttl = 3600) |
||
42 | * @method static array|CacheDataFormatter getMany(array $cacheKeys, string $namespace = '', int|string $ttl = 3600) |
||
43 | * @method array|CacheDataFormatter getMany(array $cacheKeys, string $namespace = '', int|string $ttl = 3600) |
||
44 | * @method static getOptions(): array |
||
45 | * @method getOptions(): array |
||
46 | * @method static bool has(string $cacheKey, string $namespace = '') |
||
47 | * @method bool has(string $cacheKey, string $namespace = '') |
||
48 | * @method static bool increment(string $cacheKey, int $amount = 1, string $namespace = '') |
||
49 | * @method bool increment(string $cacheKey, int $amount = 1, string $namespace = '') |
||
50 | * @method static bool putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
||
51 | * @method bool putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600) |
||
52 | * @method static bool putMany(array $items, string $namespace = '', int $batchSize = 100) |
||
53 | * @method bool putMany(array $items, string $namespace = '', int $batchSize = 100) |
||
54 | * @method static mixed remember(string $cacheKey, int|string $ttl, Closure $callback) |
||
55 | * @method mixed remember(string $cacheKey, int|string $ttl, Closure $callback) |
||
56 | * @method static mixed rememberForever(string $cacheKey, Closure $callback) |
||
57 | * @method mixed rememberForever(string $cacheKey, Closure $callback) |
||
58 | * @method static bool renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
||
59 | * @method bool renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '') |
||
60 | * @method static setConfig(): CacheConfig |
||
61 | * @method setConfig(): CacheConfig |
||
62 | * @method static setDriver(): CacheDriver |
||
63 | * @method setDriver(): CacheDriver |
||
64 | * @method static setUp(array $options): void |
||
65 | * @method setUp(array $options): void |
||
66 | */ |
||
67 | final class Cacheer |
||
68 | { |
||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private string $message; |
||
73 | |||
74 | /** |
||
75 | * @var boolean |
||
76 | */ |
||
77 | private bool $success; |
||
78 | |||
79 | /** |
||
80 | * @var boolean |
||
81 | */ |
||
82 | private bool $formatted = false; |
||
83 | |||
84 | /** |
||
85 | * @var bool |
||
86 | */ |
||
87 | private bool $compression = false; |
||
88 | |||
89 | /** |
||
90 | * @var string|null |
||
91 | */ |
||
92 | private ?string $encryptionKey = null; |
||
93 | |||
94 | /** |
||
95 | * @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore |
||
96 | */ |
||
97 | public RedisCacheStore|DatabaseCacheStore|ArrayCacheStore|FileCacheStore $cacheStore; |
||
98 | |||
99 | /** |
||
100 | * @var array |
||
101 | */ |
||
102 | public array $options = []; |
||
103 | |||
104 | /** |
||
105 | * @var CacheRetriever |
||
106 | */ |
||
107 | private CacheRetriever $retriever; |
||
108 | |||
109 | /** |
||
110 | * @var CacheMutator |
||
111 | */ |
||
112 | private CacheMutator $mutator; |
||
113 | |||
114 | /** |
||
115 | * @var CacheConfig |
||
116 | */ |
||
117 | private CacheConfig $config; |
||
118 | |||
119 | /** |
||
120 | * @var Cacheer|null |
||
121 | */ |
||
122 | private static ?Cacheer $staticInstance = null; |
||
123 | |||
124 | /** |
||
125 | * Cacheer constructor. |
||
126 | * |
||
127 | * @param array $options |
||
128 | * @param bool $formatted |
||
129 | * @throws RuntimeException|Exceptions\CacheFileException |
||
130 | */ |
||
131 | public function __construct(array $options = [], bool $formatted = false) |
||
132 | { |
||
133 | $this->formatted = $formatted; |
||
134 | $this->validateOptions($options); |
||
135 | $this->retriever = new CacheRetriever($this); |
||
136 | $this->mutator = new CacheMutator($this); |
||
137 | $this->config = new CacheConfig($this); |
||
138 | $this->setDriver()->useDefaultDriver(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Dynamically handle calls to missing instance methods. |
||
143 | * |
||
144 | * @param string $method |
||
145 | * @param array $parameters |
||
146 | * @return mixed |
||
147 | * @throws BadMethodCallException |
||
148 | */ |
||
149 | public function __call(string $method, array $parameters): mixed |
||
150 | { |
||
151 | if ($method === 'setConfig') { |
||
152 | return new CacheConfig($this); |
||
153 | } |
||
154 | |||
155 | if ($method === 'setDriver') { |
||
156 | return new CacheDriver($this); |
||
157 | } |
||
158 | |||
159 | $delegates = [$this->mutator, $this->retriever, $this->config]; |
||
160 | |||
161 | foreach ($delegates as $delegate) { |
||
162 | if (method_exists($delegate, $method)) { |
||
163 | return $delegate->{$method}(...$parameters); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | throw new BadMethodCallException("Method {$method} does not exist"); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Handle dynamic static calls by routing them through an instance. |
||
172 | * |
||
173 | * @param string $method |
||
174 | * @param array $parameters |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public static function __callStatic(string $method, array $parameters): mixed |
||
178 | { |
||
179 | $instance = self::instance(); |
||
180 | |||
181 | if ($instance === null) { |
||
182 | throw new \RuntimeException("Cacheer static instance is not initialized."); |
||
183 | } |
||
184 | |||
185 | return $instance->__call($method, $parameters); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Enable encryption for cached data |
||
190 | * |
||
191 | * @param string $key |
||
192 | * @return $this |
||
193 | */ |
||
194 | public function useEncryption(string $key): Cacheer |
||
195 | { |
||
196 | $this->encryptionKey = $key; |
||
197 | return $this; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Enable or disable data compression |
||
202 | * |
||
203 | * @param bool $status |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function useCompression(bool $status = true): Cacheer |
||
207 | { |
||
208 | $this->compression = $status; |
||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Enables or disables the formatter for cache data. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | public function useFormatter(): void |
||
218 | { |
||
219 | $this->formatted = !$this->formatted; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Validates the options provided for the Cacheer instance. |
||
224 | * |
||
225 | * @param array $options |
||
226 | * @return void |
||
227 | */ |
||
228 | private function validateOptions(array $options): void |
||
229 | { |
||
230 | $this->options = $options; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Checks if the last operation was successful. |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function isSuccess(): bool |
||
239 | { |
||
240 | return $this->success; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Sets a message for the cache operation. |
||
245 | * |
||
246 | * @param string $message |
||
247 | * @param boolean $success |
||
248 | * @return void |
||
249 | */ |
||
250 | private function setMessage(string $message, bool $success): void |
||
251 | { |
||
252 | $this->message = $message; |
||
253 | $this->success = $success; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Retrieves the message from the last operation. |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getMessage(): string |
||
262 | { |
||
263 | return $this->message; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @return void |
||
268 | */ |
||
269 | public function syncState(): void |
||
270 | { |
||
271 | $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess()); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param string $message |
||
276 | * @param bool $success |
||
277 | * @return void |
||
278 | */ |
||
279 | public function setInternalState(string $message, bool $success): void |
||
280 | { |
||
281 | $this->setMessage($message, $success); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function isFormatted(): bool |
||
288 | { |
||
289 | return $this->formatted; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function isCompressionEnabled(): bool |
||
296 | { |
||
297 | return $this->compression; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @return string|null |
||
302 | */ |
||
303 | public function getEncryptionKey(): ?string |
||
304 | { |
||
305 | return $this->encryptionKey; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get or create the shared Cacheer instance for static calls. |
||
310 | * |
||
311 | * @return Cacheer |
||
312 | */ |
||
313 | private static function instance(): Cacheer |
||
314 | { |
||
315 | if (self::$staticInstance === null) { |
||
316 | self::$staticInstance = new self(); |
||
317 | } |
||
318 | return self::$staticInstance; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
319 | } |
||
320 | } |
||
321 |