PluginProbe
User Access Manager / 2.2.13
User Access Manager v2.2.13
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 +133 -48 2.3.202.2.13 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,73 @@
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 + $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX);
233 + } elseif ($method === self::METHOD_JSON) {
234 + $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX);
235 + } elseif ($method === self::METHOD_VAR_EXPORT) {
236 + $this->php->filePutContents(
237 + $cacheFile,
238 + "<?php\n\$cachedValue = " . var_export($value, true) . ';',
239 + LOCK_EX
240 + );
158 241 }
159 242 }
160 243
161 - private function includeCachedValue(string $cacheFile): mixed
162 - {
163 - $cachedValue = null;
164 - include($cacheFile);
165 -
166 - return $cachedValue;
167 - }
168 -
169 244 /**
245 + * Returns a value from the cache.
246 + * @param string $key
247 + * @return mixed
170 248 * @throws Exception
171 249 */
172 - public function get(string $key): mixed
250 + public function get(string $key)
173 251 {
174 252 $method = $this->getCacheMethod();
175 253 $cacheFile = $this->getCacheFile($method, $key);
176 254
177 - if (file_exists($cacheFile) === false) {
178 - return null;
255 + if ((file_exists($cacheFile) === true)) {
256 + if ($method === self::METHOD_SERIALIZE) {
257 + return unserialize(base64_decode(file_get_contents($cacheFile)));
258 + } elseif ($method === self::METHOD_IGBINARY) {
259 + return $this->php->igbinaryUnserialize(file_get_contents($cacheFile));
260 + } elseif ($method === self::METHOD_JSON) {
261 + return json_decode(file_get_contents($cacheFile), true);
262 + } elseif ($method === self::METHOD_VAR_EXPORT) {
263 + $cachedValue = null;
264 + /** @noinspection PhpIncludeInspection */
265 + include($cacheFile);
266 + return $cachedValue;
267 + }
179 268 }
180 269
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 - };
270 + return null;
188 271 }
189 272
190 273 /**
274 + * Invalidates the cache.
275 + * @param string $key
191 276 * @throws Exception
192 277 */
193 - public function invalidate(string $key): void
278 + public function invalidate(string $key)
194 279 {
195 280 $method = $this->getCacheMethod();
196 281 $cacheFile = $this->getCacheFile($method, $key);
197 282
198 - if (file_exists($cacheFile) === true) {
283 + if ((file_exists($cacheFile) === true)) {
199 284 unlink($cacheFile);
200 285 }
201 286 }
202 287 }