|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright (c) 2021-2025 guanguans<[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @see https://github.com/guanguans/notify |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Guanguans\Notify\Foundation\Caches; |
| 15 | + |
| 16 | +use Psr\SimpleCache\CacheInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see https://github.com/pestphp/pest-plugin-mutate/blob/4.x/src/Cache/FileStore.php |
| 20 | + */ |
| 21 | +class FileCache implements CacheInterface |
| 22 | +{ |
| 23 | + /** @var string */ |
| 24 | + private const CACHE_FOLDER_NAME = 'pest-mutate-cache'; |
| 25 | + |
| 26 | + /** @readonly */ |
| 27 | + private string $directory; |
| 28 | + |
| 29 | + public function __construct(?string $directory = null) |
| 30 | + { |
| 31 | + $this->directory = $directory ?? (sys_get_temp_dir().\DIRECTORY_SEPARATOR.self::CACHE_FOLDER_NAME); // @pest-mutate-ignore |
| 32 | + |
| 33 | + if (!is_dir($this->directory)) { // @pest-mutate-ignore |
| 34 | + mkdir($this->directory, recursive: true); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public function get(string $key, mixed $default = null): mixed |
| 39 | + { |
| 40 | + return $this->getPayload($key) ?? $default; |
| 41 | + } |
| 42 | + |
| 43 | + public function set(string $key, mixed $value, null|\DateInterval|int $ttl = null): bool |
| 44 | + { |
| 45 | + $payload = serialize($value); |
| 46 | + |
| 47 | + $expire = $this->expiration($ttl); |
| 48 | + |
| 49 | + $content = $expire.$payload; |
| 50 | + |
| 51 | + $result = file_put_contents($this->filePathFromKey($key), $content); |
| 52 | + |
| 53 | + return false !== $result; // @pest-mutate-ignore FalseToTrue |
| 54 | + } |
| 55 | + |
| 56 | + public function delete(string $key): bool |
| 57 | + { |
| 58 | + return unlink($this->filePathFromKey($key)); |
| 59 | + } |
| 60 | + |
| 61 | + public function clear(): bool |
| 62 | + { |
| 63 | + foreach ((array) glob($this->directory.\DIRECTORY_SEPARATOR.'*') as $fileName) { |
| 64 | + // @pest-mutate-ignore |
| 65 | + if (false === $fileName) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (!str_starts_with(basename($fileName), 'cache-')) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // @pest-mutate-ignore |
| 74 | + unlink($fileName); |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + public function getMultiple(iterable $keys, mixed $default = null): iterable |
| 81 | + { |
| 82 | + $result = []; |
| 83 | + |
| 84 | + foreach ($keys as $key) { |
| 85 | + $result[$key] = $this->get($key, $default); |
| 86 | + } |
| 87 | + |
| 88 | + return $result; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param iterable<string, mixed> $values |
| 93 | + * |
| 94 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 95 | + */ |
| 96 | + public function setMultiple(iterable $values, null|\DateInterval|int $ttl = null): bool |
| 97 | + { |
| 98 | + $result = true; |
| 99 | + |
| 100 | + foreach ($values as $key => $value) { |
| 101 | + if (!$this->set($key, $value, $ttl)) { |
| 102 | + $result = false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $result; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param iterable<string> $keys |
| 111 | + * |
| 112 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 113 | + */ |
| 114 | + public function deleteMultiple(iterable $keys): bool |
| 115 | + { |
| 116 | + $result = true; |
| 117 | + |
| 118 | + foreach ($keys as $key) { |
| 119 | + if (!$this->delete($key)) { |
| 120 | + $result = false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return $result; |
| 125 | + } |
| 126 | + |
| 127 | + public function has(string $key): bool |
| 128 | + { |
| 129 | + return file_exists($this->filePathFromKey($key)); |
| 130 | + } |
| 131 | + |
| 132 | + public function directory(): string |
| 133 | + { |
| 134 | + return $this->directory; |
| 135 | + } |
| 136 | + |
| 137 | + protected function emptyPayload(): mixed |
| 138 | + { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + private function filePathFromKey(string $key): string |
| 143 | + { |
| 144 | + return $this->directory.\DIRECTORY_SEPARATOR.'cache-'.hash('md5', $key); // @pest-mutate-ignore |
| 145 | + } |
| 146 | + |
| 147 | + private function expiration(null|\DateInterval|int $seconds): int |
| 148 | + { |
| 149 | + if ($seconds instanceof \DateInterval) { |
| 150 | + return (new \DateTimeImmutable)->add($seconds)->getTimestamp(); |
| 151 | + } |
| 152 | + |
| 153 | + $seconds ??= 0; |
| 154 | + |
| 155 | + if (0 === $seconds) { |
| 156 | + return 9_999_999_999; // @pest-mutate-ignore |
| 157 | + } |
| 158 | + |
| 159 | + return time() + $seconds; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 164 | + */ |
| 165 | + private function getPayload(string $key): mixed |
| 166 | + { |
| 167 | + if (!file_exists($this->filePathFromKey($key))) { |
| 168 | + return $this->emptyPayload(); |
| 169 | + } |
| 170 | + |
| 171 | + $content = file_get_contents($this->filePathFromKey($key)); |
| 172 | + |
| 173 | + if (false === $content) { // @pest-mutate-ignore |
| 174 | + return $this->emptyPayload(); |
| 175 | + } |
| 176 | + |
| 177 | + try { |
| 178 | + $expire = (int) substr( |
| 179 | + $content, |
| 180 | + 0, |
| 181 | + 10 |
| 182 | + ); |
| 183 | + } catch (\Exception) { |
| 184 | + $this->delete($key); |
| 185 | + |
| 186 | + return $this->emptyPayload(); |
| 187 | + } |
| 188 | + |
| 189 | + if (time() >= $expire) { |
| 190 | + $this->delete($key); |
| 191 | + |
| 192 | + return $this->emptyPayload(); |
| 193 | + } |
| 194 | + |
| 195 | + try { |
| 196 | + $data = unserialize(substr($content, 10)); |
| 197 | + } catch (\Exception) { |
| 198 | + $this->delete($key); |
| 199 | + |
| 200 | + return $this->emptyPayload(); |
| 201 | + } |
| 202 | + |
| 203 | + return $data; |
| 204 | + } |
| 205 | +} |
0 commit comments