PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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 +100 -11 2.3.142.2.2 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;
@@ -11,8 +24,13 @@
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 35 const ID = 'FileSystemCacheProvider';
18 36 const CONFIG_KEY = 'uam_file_system_cache_provider';
@@ -22,20 +40,69 @@
22 40 const METHOD_IGBINARY = 'igbinary';
23 41 const METHOD_JSON = 'json';
24 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,11 +215,14 @@
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
@@ -147,8 +228,9 @@
147 228
148 229 if ($method === self::METHOD_SERIALIZE) {
149 230 $this->php->filePutContents($cacheFile, base64_encode(serialize($value)), LOCK_EX);
150 231 } elseif ($method === self::METHOD_IGBINARY) {
232 + /** @noinspection PhpUndefinedFunctionInspection */
151 233 $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX);
152 234 } elseif ($method === self::METHOD_JSON) {
153 235 $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX);
154 236 } elseif ($method === self::METHOD_VAR_EXPORT) {
@@ -160,11 +242,14 @@
160 242 }
161 243 }
162 244
163 245 /**
246 + * Returns a value from the cache.
247 + * @param string $key
248 + * @return mixed
164 249 * @throws Exception
165 250 */
166 - public function get(string $key): mixed
251 + public function get(string $key)
167 252 {
168 253 $method = $this->getCacheMethod();
169 254 $cacheFile = $this->getCacheFile($method, $key);
170 255
@@ -171,13 +256,15 @@
171 256 if ((file_exists($cacheFile) === true)) {
172 257 if ($method === self::METHOD_SERIALIZE) {
173 258 return unserialize(base64_decode(file_get_contents($cacheFile)));
174 259 } elseif ($method === self::METHOD_IGBINARY) {
260 + /** @noinspection PhpUndefinedFunctionInspection */
175 261 return $this->php->igbinaryUnserialize(file_get_contents($cacheFile));
176 262 } elseif ($method === self::METHOD_JSON) {
177 263 return json_decode(file_get_contents($cacheFile), true);
178 264 } elseif ($method === self::METHOD_VAR_EXPORT) {
179 265 $cachedValue = null;
266 + /** @noinspection PhpIncludeInspection */
180 267 include($cacheFile);
181 268 return $cachedValue;
182 269 }
183 270 }
@@ -185,11 +272,13 @@
185 272 return null;
186 273 }
187 274
188 275 /**
276 + * Invalidates the cache.
277 + * @param string $key
189 278 * @throws Exception
190 279 */
191 - public function invalidate(string $key): void
280 + public function invalidate(string $key)
192 281 {
193 282 $method = $this->getCacheMethod();
194 283 $cacheFile = $this->getCacheFile($method, $key);
195 284