| @@ -6,9 +6,9 @@ | ||
| 6 | 6 | |
| 7 | 7 | use Exception; |
| 8 | 8 | use UserAccessManager\Config\Config; |
| 9 | 9 | use UserAccessManager\Config\ConfigFactory; |
| 10 | -use UserAccessManager\Config\ConfigParameterFactory; | |
| 10 | +use UserAccessManager\Config\Parameter\ConfigParameterFactory; | |
| 11 | 11 | use UserAccessManager\Util\Util; |
| 12 | 12 | use UserAccessManager\Wrapper\Php; |
| 13 | 13 | use UserAccessManager\Wrapper\Wordpress; |
| 14 | 14 | |
| @@ -13,16 +13,16 @@ | ||
| 13 | 13 | use UserAccessManager\Wrapper\Wordpress; |
| 14 | 14 | |
| 15 | 15 | class FileSystemCacheProvider implements CacheProviderInterface |
| 16 | 16 | { |
| 17 | - const ID = 'FileSystemCacheProvider'; | |
| 18 | - const CONFIG_KEY = 'uam_file_system_cache_provider'; | |
| 19 | - const CONFIG_PATH = 'fs_cache_path'; | |
| 20 | - const CONFIG_METHOD = 'fs_cache_method'; | |
| 21 | - const METHOD_SERIALIZE = 'serialize'; | |
| 22 | - const METHOD_IGBINARY = 'igbinary'; | |
| 23 | - const METHOD_JSON = 'json'; | |
| 24 | - const METHOD_VAR_EXPORT = 'var_export'; | |
| 17 | + public const ID = 'FileSystemCacheProvider'; | |
| 18 | + public const CONFIG_KEY = 'uam_file_system_cache_provider'; | |
| 19 | + public const CONFIG_PATH = 'fs_cache_path'; | |
| 20 | + public const CONFIG_METHOD = 'fs_cache_method'; | |
| 21 | + public const METHOD_SERIALIZE = 'serialize'; | |
| 22 | + public const METHOD_IGBINARY = 'igbinary'; | |
| 23 | + public const METHOD_JSON = 'json'; | |
| 24 | + public const METHOD_VAR_EXPORT = 'var_export'; | |
| 25 | 25 | |
| 26 | 26 | private ?Config $config = null; |
| 27 | 27 | private ?string $path = null; |
| 28 | 28 | |
| @@ -144,23 +144,29 @@ | ||
| 144 | 144 | { |
| 145 | 145 | $method = $this->getCacheMethod(); |
| 146 | 146 | $cacheFile = $this->getCacheFile($method, $key); |
| 147 | 147 | |
| 148 | - if ($method === self::METHOD_SERIALIZE) { | |
| 149 | - $this->php->filePutContents($cacheFile, base64_encode(serialize($value)), LOCK_EX); | |
| 150 | - } elseif ($method === self::METHOD_IGBINARY) { | |
| 151 | - $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX); | |
| 152 | - } elseif ($method === self::METHOD_JSON) { | |
| 153 | - $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX); | |
| 154 | - } elseif ($method === self::METHOD_VAR_EXPORT) { | |
| 155 | - $this->php->filePutContents( | |
| 156 | - $cacheFile, | |
| 157 | - "<?php\n\$cachedValue = " . var_export($value, true) . ';', | |
| 158 | - LOCK_EX | |
| 159 | - ); | |
| 148 | + $content = match ($method) { | |
| 149 | + self::METHOD_SERIALIZE => base64_encode(serialize($value)), | |
| 150 | + self::METHOD_IGBINARY => $this->php->igbinarySerialize($value), | |
| 151 | + self::METHOD_JSON => json_encode($value), | |
| 152 | + self::METHOD_VAR_EXPORT => "<?php\n\$cachedValue = " . var_export($value, true) . ';', | |
| 153 | + default => null | |
| 154 | + }; | |
| 155 | + | |
| 156 | + if ($content !== null) { | |
| 157 | + $this->php->filePutContents($cacheFile, $content, LOCK_EX); | |
| 160 | 158 | } |
| 161 | 159 | } |
| 162 | 160 | |
| 161 | + private function includeCachedValue(string $cacheFile): mixed | |
| 162 | + { | |
| 163 | + $cachedValue = null; | |
| 164 | + include($cacheFile); | |
| 165 | + | |
| 166 | + return $cachedValue; | |
| 167 | + } | |
| 168 | + | |
| 163 | 169 | /** |
| 164 | 170 | * @throws Exception |
| 165 | 171 | */ |
| 166 | 172 | public function get(string $key): mixed |
| @@ -167,23 +173,19 @@ | ||
| 167 | 173 | { |
| 168 | 174 | $method = $this->getCacheMethod(); |
| 169 | 175 | $cacheFile = $this->getCacheFile($method, $key); |
| 170 | 176 | |
| 171 | - if ((file_exists($cacheFile) === true)) { | |
| 172 | - if ($method === self::METHOD_SERIALIZE) { | |
| 173 | - return unserialize(base64_decode(file_get_contents($cacheFile))); | |
| 174 | - } elseif ($method === self::METHOD_IGBINARY) { | |
| 175 | - return $this->php->igbinaryUnserialize(file_get_contents($cacheFile)); | |
| 176 | - } elseif ($method === self::METHOD_JSON) { | |
| 177 | - return json_decode(file_get_contents($cacheFile), true); | |
| 178 | - } elseif ($method === self::METHOD_VAR_EXPORT) { | |
| 179 | - $cachedValue = null; | |
| 180 | - include($cacheFile); | |
| 181 | - return $cachedValue; | |
| 182 | - } | |
| 177 | + if (file_exists($cacheFile) === false) { | |
| 178 | + return null; | |
| 183 | 179 | } |
| 184 | 180 | |
| 185 | - return null; | |
| 181 | + return match ($method) { | |
| 182 | + self::METHOD_SERIALIZE => unserialize(base64_decode(file_get_contents($cacheFile))), | |
| 183 | + self::METHOD_IGBINARY => $this->php->igbinaryUnserialize(file_get_contents($cacheFile)), | |
| 184 | + self::METHOD_JSON => json_decode(file_get_contents($cacheFile), true), | |
| 185 | + self::METHOD_VAR_EXPORT => $this->includeCachedValue($cacheFile), | |
| 186 | + default => null | |
| 187 | + }; | |
| 186 | 188 | } |
| 187 | 189 | |
| 188 | 190 | /** |
| 189 | 191 | * @throws Exception |
| @@ -192,9 +194,9 @@ | ||
| 192 | 194 | { |
| 193 | 195 | $method = $this->getCacheMethod(); |
| 194 | 196 | $cacheFile = $this->getCacheFile($method, $key); |
| 195 | 197 | |
| 196 | - if ((file_exists($cacheFile) === true)) { | |
| 198 | + if (file_exists($cacheFile) === true) { | |
| 197 | 199 | unlink($cacheFile); |
| 198 | 200 | } |
| 199 | 201 | } |
| 200 | 202 | } |