PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.3
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Cache/FileSystemCacheProvider.php +135 -48 2.3.202.2.3 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * FileSystemCacheProvider.php
4 + *
5 + * The FileSystemCacheProvider interface file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Cache;
@@ -6,36 +19,90 @@
6 19
7 20 use Exception;
8 21 use UserAccessManager\Config\Config;
9 22 use UserAccessManager\Config\ConfigFactory;
10 -use UserAccessManager\Config\Parameter\ConfigParameterFactory;
23 +use UserAccessManager\Config\ConfigParameterFactory;
11 24 use UserAccessManager\Util\Util;
12 25 use UserAccessManager\Wrapper\Php;
13 26 use UserAccessManager\Wrapper\Wordpress;
14 27
28 +/**
29 + * Class FileSystemCacheProvider
30 + *
31 + * @package UserAccessManager\Cache
32 + */
15 33 class FileSystemCacheProvider implements CacheProviderInterface
16 34 {
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';
35 + const ID = 'FileSystemCacheProvider';
36 + const CONFIG_KEY = 'uam_file_system_cache_provider';
37 + const CONFIG_PATH = 'fs_cache_path';
38 + const CONFIG_METHOD = 'fs_cache_method';
39 + const METHOD_SERIALIZE = 'serialize';
40 + const METHOD_IGBINARY = 'igbinary';
41 + const METHOD_JSON = 'json';
42 + const METHOD_VAR_EXPORT = 'var_export';
25 43
26 - private ?Config $config = null;
27 - private ?string $path = null;
44 + /**
45 + * @var Php
46 + */
47 + private $php;
28 48
49 + /**
50 + * @var Wordpress
51 + */
52 + private $wordpress;
53 +
54 + /**
55 + * @var Util
56 + */
57 + private $util;
58 +
59 + /**
60 + * @var ConfigFactory
61 + */
62 + private $configFactory;
63 +
64 + /**
65 + * @var ConfigParameterFactory
66 + */
67 + private $configParameterFactory;
68 +
69 + /**
70 + * @var null|Config
71 + */
72 + private $config = null;
73 +
74 + /**
75 + * @var string
76 + */
77 + private $path = null;
78 +
79 + /**
80 + * FileSystemCacheProvider constructor.
81 + * @param Php $php
82 + * @param Wordpress $wordpress
83 + * @param Util $util
84 + * @param ConfigFactory $configFactory
85 + * @param ConfigParameterFactory $configParameterFactory
86 + */
29 87 public function __construct(
30 - private Php $php,
31 - private Wordpress $wordpress,
32 - private Util $util,
33 - private ConfigFactory $configFactory,
34 - private ConfigParameterFactory $configParameterFactory
88 + Php $php,
89 + Wordpress $wordpress,
90 + Util $util,
91 + ConfigFactory $configFactory,
92 + ConfigParameterFactory $configParameterFactory
35 93 ) {
94 + $this->php = $php;
95 + $this->wordpress = $wordpress;
96 + $this->util = $util;
97 + $this->configFactory = $configFactory;
98 + $this->configParameterFactory = $configParameterFactory;
36 99 }
37 100
101 + /**
102 + * Returns the id.
103 + * @return string
104 + */
38 105 public function getId(): string
39 106 {
40 107 return self::ID;
41 108 }
@@ -40,8 +107,10 @@
40 107 return self::ID;
41 108 }
42 109
43 110 /**
111 + * Returns the cache path.
112 + * @return string
44 113 * @throws Exception
45 114 */
46 115 private function getPath(): string
47 116 {
@@ -56,11 +125,12 @@
56 125 return $this->path;
57 126 }
58 127
59 128 /**
129 + * Initialise the caching path.
60 130 * @throws Exception
61 131 */
62 - public function init(): void
132 + public function init()
63 133 {
64 134 $path = $this->getPath();
65 135
66 136 if (is_dir($path) === false) {
@@ -74,8 +144,10 @@
74 144 }
75 145 }
76 146
77 147 /**
148 + * Returns the cache config.
149 + * @return Config
78 150 * @throws Exception
79 151 */
80 152 public function getConfig(): Config
81 153 {
@@ -109,8 +181,10 @@
109 181 return $this->config;
110 182 }
111 183
112 184 /**
185 + * Returns the caching method.
186 + * @return string|null
113 187 * @throws Exception
114 188 */
115 189 private function getCacheMethod(): ?string
116 190 {
@@ -126,8 +200,12 @@
126 200 return $method;
127 201 }
128 202
129 203 /**
204 + * Returns the cache file name with path.
205 + * @param string|null $method
206 + * @param string $key
207 + * @return string
130 208 * @throws Exception
131 209 */
132 210 private function getCacheFile(?string $method, string $key): string
133 211 {
@@ -137,66 +215,75 @@
137 215 return $cacheFile;
138 216 }
139 217
140 218 /**
219 + * Adds a value to the cache.
220 + * @param string $key
221 + * @param mixed $value
141 222 * @throws Exception
142 223 */
143 - public function add(string $key, mixed $value): void
224 + public function add(string $key, $value)
144 225 {
145 226 $method = $this->getCacheMethod();
146 227 $cacheFile = $this->getCacheFile($method, $key);
147 228
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);
229 + if ($method === self::METHOD_SERIALIZE) {
230 + $this->php->filePutContents($cacheFile, base64_encode(serialize($value)), LOCK_EX);
231 + } elseif ($method === self::METHOD_IGBINARY) {
232 + /** @noinspection PhpUndefinedFunctionInspection */
233 + $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX);
234 + } elseif ($method === self::METHOD_JSON) {
235 + $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX);
236 + } elseif ($method === self::METHOD_VAR_EXPORT) {
237 + $this->php->filePutContents(
238 + $cacheFile,
239 + "<?php\n\$cachedValue = " . var_export($value, true) . ';',
240 + LOCK_EX
241 + );
158 242 }
159 243 }
160 244
161 - private function includeCachedValue(string $cacheFile): mixed
162 - {
163 - $cachedValue = null;
164 - include($cacheFile);
165 -
166 - return $cachedValue;
167 - }
168 -
169 245 /**
246 + * Returns a value from the cache.
247 + * @param string $key
248 + * @return mixed
170 249 * @throws Exception
171 250 */
172 - public function get(string $key): mixed
251 + public function get(string $key)
173 252 {
174 253 $method = $this->getCacheMethod();
175 254 $cacheFile = $this->getCacheFile($method, $key);
176 255
177 - if (file_exists($cacheFile) === false) {
178 - return null;
256 + if ((file_exists($cacheFile) === true)) {
257 + if ($method === self::METHOD_SERIALIZE) {
258 + return unserialize(base64_decode(file_get_contents($cacheFile)));
259 + } elseif ($method === self::METHOD_IGBINARY) {
260 + /** @noinspection PhpUndefinedFunctionInspection */
261 + return $this->php->igbinaryUnserialize(file_get_contents($cacheFile));
262 + } elseif ($method === self::METHOD_JSON) {
263 + return json_decode(file_get_contents($cacheFile), true);
264 + } elseif ($method === self::METHOD_VAR_EXPORT) {
265 + $cachedValue = null;
266 + /** @noinspection PhpIncludeInspection */
267 + include($cacheFile);
268 + return $cachedValue;
269 + }
179 270 }
180 271
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 - };
272 + return null;
188 273 }
189 274
190 275 /**
276 + * Invalidates the cache.
277 + * @param string $key
191 278 * @throws Exception
192 279 */
193 - public function invalidate(string $key): void
280 + public function invalidate(string $key)
194 281 {
195 282 $method = $this->getCacheMethod();
196 283 $cacheFile = $this->getCacheFile($method, $key);
197 284
198 - if (file_exists($cacheFile) === true) {
285 + if ((file_exists($cacheFile) === true)) {
199 286 unlink($cacheFile);
200 287 }
201 288 }
202 289 }