PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
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 +152 -70 2.3.20 → 2.1.11 View file →
@@ -1,50 +1,117 @@
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 -use UserAccessManager\Config\Parameter\ConfigParameterFactory;
19 +use UserAccessManager\Config\ConfigParameterFactory;
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 - 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';
31 + const ID = 'FileSystemCacheProvider';
32 + const CONFIG_KEY = 'uam_file_system_cache_provider';
33 + const CONFIG_PATH = 'fs_cache_path';
34 + const CONFIG_METHOD = 'fs_cache_method';
35 + const METHOD_SERIALIZE = 'serialize';
36 + const METHOD_IGBINARY = 'igbinary';
37 + const METHOD_JSON = 'json';
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,66 +213,72 @@
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
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);
227 + if ($method === self::METHOD_SERIALIZE) {
228 + $this->php->filePutContents($cacheFile, base64_encode(serialize($value)), LOCK_EX);
229 + } elseif ($method === self::METHOD_IGBINARY) {
230 + /** @noinspection PhpUndefinedFunctionInspection */
231 + $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX);
232 + } elseif ($method === self::METHOD_JSON) {
233 + $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX);
234 + } elseif ($method === self::METHOD_VAR_EXPORT) {
235 + $this->php->filePutContents($cacheFile, "<?php\n\$cachedValue = ".var_export($value, true).';', LOCK_EX);
158 236 }
159 237 }
160 238
161 - private function includeCachedValue(string $cacheFile): mixed
162 - {
163 - $cachedValue = null;
164 - include($cacheFile);
165 -
166 - return $cachedValue;
167 - }
168 -
169 239 /**
170 - * @throws Exception
240 + * Returns a value from the cache.
241 + *
242 + * @param string $key
243 + *
244 + * @return mixed
171 245 */
172 - public function get(string $key): mixed
246 + public function get($key)
173 247 {
174 248 $method = $this->getCacheMethod();
175 249 $cacheFile = $this->getCacheFile($method, $key);
176 250
177 - if (file_exists($cacheFile) === false) {
178 - return null;
251 + if ((file_exists($cacheFile) === true)) {
252 + if ($method === self::METHOD_SERIALIZE) {
253 + return unserialize(base64_decode(file_get_contents($cacheFile)));
254 + } elseif ($method === self::METHOD_IGBINARY) {
255 + /** @noinspection PhpUndefinedFunctionInspection */
256 + return $this->php->igbinaryUnserialize(file_get_contents($cacheFile));
257 + } elseif ($method === self::METHOD_JSON) {
258 + return json_decode(file_get_contents($cacheFile), true);
259 + } elseif ($method === self::METHOD_VAR_EXPORT) {
260 + $cachedValue = null;
261 + /** @noinspection PhpIncludeInspection */
262 + include($cacheFile);
263 + return $cachedValue;
264 + }
179 265 }
180 266
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 - };
267 + return null;
188 268 }
189 269
190 270 /**
191 - * @throws Exception
271 + * Invalidates the cache.
272 + *
273 + * @param string $key
192 274 */
193 - public function invalidate(string $key): void
275 + public function invalidate($key)
194 276 {
195 277 $method = $this->getCacheMethod();
196 278 $cacheFile = $this->getCacheFile($method, $key);
197 279
198 - if (file_exists($cacheFile) === true) {
280 + if ((file_exists($cacheFile) === true)) {
199 281 unlink($cacheFile);
200 282 }
201 283 }
202 284 }