| @@ -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\Parameter\ConfigParameterFactory; | |
| 10 | +use UserAccessManager\Config\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 | - 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'; | |
| 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'; | |
| 25 | 25 | |
| 26 | 26 | private ?Config $config = null; |
| 27 | 27 | private ?string $path = null; |
| 28 | 28 | |
| @@ -144,29 +144,23 @@ | ||
| 144 | 144 | { |
| 145 | 145 | $method = $this->getCacheMethod(); |
| 146 | 146 | $cacheFile = $this->getCacheFile($method, $key); |
| 147 | 147 | |
| 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); | |
| 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 | + ); | |
| 158 | 160 | } |
| 159 | 161 | } |
| 160 | 162 | |
| 161 | - private function includeCachedValue(string $cacheFile): mixed | |
| 162 | - { | |
| 163 | - $cachedValue = null; | |
| 164 | - include($cacheFile); | |
| 165 | - | |
| 166 | - return $cachedValue; | |
| 167 | - } | |
| 168 | - | |
| 169 | 163 | /** |
| 170 | 164 | * @throws Exception |
| 171 | 165 | */ |
| 172 | 166 | public function get(string $key): mixed |
| @@ -173,19 +167,23 @@ | ||
| 173 | 167 | { |
| 174 | 168 | $method = $this->getCacheMethod(); |
| 175 | 169 | $cacheFile = $this->getCacheFile($method, $key); |
| 176 | 170 | |
| 177 | - if (file_exists($cacheFile) === false) { | |
| 178 | - return null; | |
| 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 | + } | |
| 179 | 183 | } |
| 180 | 184 | |
| 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 | - }; | |
| 185 | + return null; | |
| 188 | 186 | } |
| 189 | 187 | |
| 190 | 188 | /** |
| 191 | 189 | * @throws Exception |
| @@ -194,9 +192,9 @@ | ||
| 194 | 192 | { |
| 195 | 193 | $method = $this->getCacheMethod(); |
| 196 | 194 | $cacheFile = $this->getCacheFile($method, $key); |
| 197 | 195 | |
| 198 | - if (file_exists($cacheFile) === true) { | |
| 196 | + if ((file_exists($cacheFile) === true)) { | |
| 199 | 197 | unlink($cacheFile); |
| 200 | 198 | } |
| 201 | 199 | } |
| 202 | 200 | } |