| @@ -1,11 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -declare(strict_types=1); | |
| 4 | - | |
| 2 | +/** | |
| 3 | + * FileSystemCacheProvider.php | |
| 4 | + * | |
| 5 | + * The FileSystemCacheProvider interface file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <[email protected]> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 5 | 15 | namespace UserAccessManager\Cache; |
| 6 | 16 | |
| 7 | -use Exception; | |
| 8 | 17 | use UserAccessManager\Config\Config; |
| 9 | 18 | use UserAccessManager\Config\ConfigFactory; |
| 10 | 19 | use UserAccessManager\Config\ConfigParameterFactory; |
| 11 | 20 | use UserAccessManager\Util\Util; |
| @@ -11,8 +20,13 @@ | ||
| 11 | 20 | use UserAccessManager\Util\Util; |
| 12 | 21 | use UserAccessManager\Wrapper\Php; |
| 13 | 22 | use UserAccessManager\Wrapper\Wordpress; |
| 14 | 23 | |
| 24 | +/** | |
| 25 | + * Class FileSystemCacheProvider | |
| 26 | + * | |
| 27 | + * @package UserAccessManager\Cache | |
| 28 | + */ | |
| 15 | 29 | class FileSystemCacheProvider implements CacheProviderInterface |
| 16 | 30 | { |
| 17 | 31 | const ID = 'FileSystemCacheProvider'; |
| 18 | 32 | const CONFIG_KEY = 'uam_file_system_cache_provider'; |
| @@ -22,29 +36,82 @@ | ||
| 22 | 36 | const METHOD_IGBINARY = 'igbinary'; |
| 23 | 37 | const METHOD_JSON = 'json'; |
| 24 | 38 | const METHOD_VAR_EXPORT = 'var_export'; |
| 25 | 39 | |
| 26 | - private ?Config $config = null; | |
| 27 | - private ?string $path = null; | |
| 40 | + /** | |
| 41 | + * @var Php | |
| 42 | + */ | |
| 43 | + private $php; | |
| 28 | 44 | |
| 45 | + /** | |
| 46 | + * @var Wordpress | |
| 47 | + */ | |
| 48 | + private $wordpress; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * @var Util | |
| 52 | + */ | |
| 53 | + private $util; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * @var ConfigFactory | |
| 57 | + */ | |
| 58 | + private $configFactory; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * @var ConfigParameterFactory | |
| 62 | + */ | |
| 63 | + private $configParameterFactory; | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * @var null|Config | |
| 67 | + */ | |
| 68 | + private $config = null; | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * @var string | |
| 72 | + */ | |
| 73 | + private $path = null; | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * FileSystemCacheProvider constructor. | |
| 77 | + * | |
| 78 | + * @param Php $php | |
| 79 | + * @param Wordpress $wordpress | |
| 80 | + * @param Util $util | |
| 81 | + * @param ConfigFactory $configFactory | |
| 82 | + * @param ConfigParameterFactory $configParameterFactory | |
| 83 | + */ | |
| 29 | 84 | public function __construct( |
| 30 | - private Php $php, | |
| 31 | - private Wordpress $wordpress, | |
| 32 | - private Util $util, | |
| 33 | - private ConfigFactory $configFactory, | |
| 34 | - private ConfigParameterFactory $configParameterFactory | |
| 85 | + Php $php, | |
| 86 | + Wordpress $wordpress, | |
| 87 | + Util $util, | |
| 88 | + ConfigFactory $configFactory, | |
| 89 | + ConfigParameterFactory $configParameterFactory | |
| 35 | 90 | ) { |
| 91 | + $this->php = $php; | |
| 92 | + $this->wordpress = $wordpress; | |
| 93 | + $this->util = $util; | |
| 94 | + $this->configFactory = $configFactory; | |
| 95 | + $this->configParameterFactory = $configParameterFactory; | |
| 36 | 96 | } |
| 37 | - | |
| 38 | - public function getId(): string | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Returns the id. | |
| 100 | + * | |
| 101 | + * @return string | |
| 102 | + */ | |
| 103 | + public function getId() | |
| 39 | 104 | { |
| 40 | 105 | return self::ID; |
| 41 | 106 | } |
| 42 | 107 | |
| 43 | 108 | /** |
| 44 | - * @throws Exception | |
| 109 | + * Returns the cache path. | |
| 110 | + * | |
| 111 | + * @return string | |
| 45 | 112 | */ |
| 46 | - private function getPath(): string | |
| 113 | + private function getPath() | |
| 47 | 114 | { |
| 48 | 115 | if ($this->path === null) { |
| 49 | 116 | $this->path = $this->getConfig()->getParameterValue(self::CONFIG_PATH); |
| 50 | 117 | |
| @@ -56,11 +123,11 @@ | ||
| 56 | 123 | return $this->path; |
| 57 | 124 | } |
| 58 | 125 | |
| 59 | 126 | /** |
| 60 | - * @throws Exception | |
| 127 | + * Initialise the caching path. | |
| 61 | 128 | */ |
| 62 | - public function init(): void | |
| 129 | + public function init() | |
| 63 | 130 | { |
| 64 | 131 | $path = $this->getPath(); |
| 65 | 132 | |
| 66 | 133 | if (is_dir($path) === false) { |
| @@ -66,9 +133,9 @@ | ||
| 66 | 133 | if (is_dir($path) === false) { |
| 67 | 134 | $this->php->mkdir($path, 0775, true); |
| 68 | 135 | } |
| 69 | 136 | |
| 70 | - $htaccessFile = $path . '.htaccess'; | |
| 137 | + $htaccessFile = $path.'.htaccess'; | |
| 71 | 138 | |
| 72 | 139 | if (file_exists($htaccessFile) === false) { |
| 73 | 140 | file_put_contents($htaccessFile, 'Deny from all'); |
| 74 | 141 | } |
| @@ -74,11 +141,13 @@ | ||
| 74 | 141 | } |
| 75 | 142 | } |
| 76 | 143 | |
| 77 | 144 | /** |
| 78 | - * @throws Exception | |
| 145 | + * Returns the cache config. | |
| 146 | + * | |
| 147 | + * @return Config | |
| 79 | 148 | */ |
| 80 | - public function getConfig(): Config | |
| 149 | + public function getConfig() | |
| 81 | 150 | { |
| 82 | 151 | if ($this->config === null) { |
| 83 | 152 | $this->config = $this->configFactory->createConfig(self::CONFIG_KEY); |
| 84 | 153 | |
| @@ -94,9 +163,9 @@ | ||
| 94 | 163 | |
| 95 | 164 | $configParameters = [ |
| 96 | 165 | self::CONFIG_PATH => $this->configParameterFactory->createStringConfigParameter( |
| 97 | 166 | self::CONFIG_PATH, |
| 98 | - $this->wordpress->getHomePath() . 'cache/uam' | |
| 167 | + $this->wordpress->getHomePath().'cache/uam' | |
| 99 | 168 | ), |
| 100 | 169 | self::CONFIG_METHOD => $this->configParameterFactory->createSelectionConfigParameter( |
| 101 | 170 | self::CONFIG_METHOD, |
| 102 | 171 | self::METHOD_VAR_EXPORT, |
| @@ -109,13 +178,15 @@ | ||
| 109 | 178 | return $this->config; |
| 110 | 179 | } |
| 111 | 180 | |
| 112 | 181 | /** |
| 113 | - * @throws Exception | |
| 182 | + * Returns the caching method. | |
| 183 | + * | |
| 184 | + * @return string|null | |
| 114 | 185 | */ |
| 115 | - private function getCacheMethod(): ?string | |
| 186 | + private function getCacheMethod() | |
| 116 | 187 | { |
| 117 | - $method = (string) $this->getConfig()->getParameterValue(self::CONFIG_METHOD); | |
| 188 | + $method = (string)$this->getConfig()->getParameterValue(self::CONFIG_METHOD); | |
| 118 | 189 | |
| 119 | 190 | if ($method === self::METHOD_IGBINARY |
| 120 | 191 | && ($this->php->functionExists('igbinary_serialize') === false |
| 121 | 192 | || $this->php->functionExists('igbinary_unserialize') === false) |
| @@ -126,13 +197,18 @@ | ||
| 126 | 197 | return $method; |
| 127 | 198 | } |
| 128 | 199 | |
| 129 | 200 | /** |
| 130 | - * @throws Exception | |
| 201 | + * Returns the cache file name with path. | |
| 202 | + * | |
| 203 | + * @param string $method | |
| 204 | + * @param string $key | |
| 205 | + * | |
| 206 | + * @return string | |
| 131 | 207 | */ |
| 132 | - private function getCacheFile(?string $method, string $key): string | |
| 208 | + private function getCacheFile($method, $key) | |
| 133 | 209 | { |
| 134 | - $cacheFile = $this->getPath() . $key; | |
| 210 | + $cacheFile = $this->getPath().$key; | |
| 135 | 211 | $cacheFile .= ($method === self::METHOD_VAR_EXPORT) ? '.php' : '.cache'; |
| 136 | 212 | |
| 137 | 213 | return $cacheFile; |
| 138 | 214 | } |
| @@ -137,11 +213,14 @@ | ||
| 137 | 213 | return $cacheFile; |
| 138 | 214 | } |
| 139 | 215 | |
| 140 | 216 | /** |
| 141 | - * @throws Exception | |
| 217 | + * Adds a value to the cache. | |
| 218 | + * | |
| 219 | + * @param string $key | |
| 220 | + * @param mixed $value | |
| 142 | 221 | */ |
| 143 | - public function add(string $key, mixed $value): void | |
| 222 | + public function add($key, $value) | |
| 144 | 223 | { |
| 145 | 224 | $method = $this->getCacheMethod(); |
| 146 | 225 | $cacheFile = $this->getCacheFile($method, $key); |
| 147 | 226 | |
| @@ -147,24 +226,25 @@ | ||
| 147 | 226 | |
| 148 | 227 | if ($method === self::METHOD_SERIALIZE) { |
| 149 | 228 | $this->php->filePutContents($cacheFile, base64_encode(serialize($value)), LOCK_EX); |
| 150 | 229 | } elseif ($method === self::METHOD_IGBINARY) { |
| 230 | + /** @noinspection PhpUndefinedFunctionInspection */ | |
| 151 | 231 | $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX); |
| 152 | 232 | } elseif ($method === self::METHOD_JSON) { |
| 153 | 233 | $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX); |
| 154 | 234 | } elseif ($method === self::METHOD_VAR_EXPORT) { |
| 155 | - $this->php->filePutContents( | |
| 156 | - $cacheFile, | |
| 157 | - "<?php\n\$cachedValue = " . var_export($value, true) . ';', | |
| 158 | - LOCK_EX | |
| 159 | - ); | |
| 235 | + $this->php->filePutContents($cacheFile, "<?php\n\$cachedValue = ".var_export($value, true).';', LOCK_EX); | |
| 160 | 236 | } |
| 161 | 237 | } |
| 162 | 238 | |
| 163 | 239 | /** |
| 164 | - * @throws Exception | |
| 240 | + * Returns a value from the cache. | |
| 241 | + * | |
| 242 | + * @param string $key | |
| 243 | + * | |
| 244 | + * @return mixed | |
| 165 | 245 | */ |
| 166 | - public function get(string $key): mixed | |
| 246 | + public function get($key) | |
| 167 | 247 | { |
| 168 | 248 | $method = $this->getCacheMethod(); |
| 169 | 249 | $cacheFile = $this->getCacheFile($method, $key); |
| 170 | 250 | |
| @@ -171,13 +251,15 @@ | ||
| 171 | 251 | if ((file_exists($cacheFile) === true)) { |
| 172 | 252 | if ($method === self::METHOD_SERIALIZE) { |
| 173 | 253 | return unserialize(base64_decode(file_get_contents($cacheFile))); |
| 174 | 254 | } elseif ($method === self::METHOD_IGBINARY) { |
| 255 | + /** @noinspection PhpUndefinedFunctionInspection */ | |
| 175 | 256 | return $this->php->igbinaryUnserialize(file_get_contents($cacheFile)); |
| 176 | 257 | } elseif ($method === self::METHOD_JSON) { |
| 177 | 258 | return json_decode(file_get_contents($cacheFile), true); |
| 178 | 259 | } elseif ($method === self::METHOD_VAR_EXPORT) { |
| 179 | 260 | $cachedValue = null; |
| 261 | + /** @noinspection PhpIncludeInspection */ | |
| 180 | 262 | include($cacheFile); |
| 181 | 263 | return $cachedValue; |
| 182 | 264 | } |
| 183 | 265 | } |
| @@ -185,11 +267,13 @@ | ||
| 185 | 267 | return null; |
| 186 | 268 | } |
| 187 | 269 | |
| 188 | 270 | /** |
| 189 | - * @throws Exception | |
| 271 | + * Invalidates the cache. | |
| 272 | + * | |
| 273 | + * @param string $key | |
| 190 | 274 | */ |
| 191 | - public function invalidate(string $key): void | |
| 275 | + public function invalidate($key) | |
| 192 | 276 | { |
| 193 | 277 | $method = $this->getCacheMethod(); |
| 194 | 278 | $cacheFile = $this->getCacheFile($method, $key); |
| 195 | 279 | |