PluginProbe
User Access Manager / 2.2.24
User Access Manager v2.2.24
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
user-access-manager / src / Cache / FileSystemCacheProvider.php

FileSystemCacheProvider.php in User Access Manager 2.2.24, at src/Cache/FileSystemCacheProvider.php

288 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Cache;
19
20 use Exception;
21 use UserAccessManager\Config\Config;
22 use UserAccessManager\Config\ConfigFactory;
23 use UserAccessManager\Config\ConfigParameterFactory;
24 use UserAccessManager\Util\Util;
25 use UserAccessManager\Wrapper\Php;
26 use UserAccessManager\Wrapper\Wordpress;
27
28 /**
29 * Class FileSystemCacheProvider
30 *
31 * @package UserAccessManager\Cache
32 */
33 class FileSystemCacheProvider implements CacheProviderInterface
34 {
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';
43
44 /**
45 * @var Php
46 */
47 private $php;
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 */
87 public function __construct(
88 Php $php,
89 Wordpress $wordpress,
90 Util $util,
91 ConfigFactory $configFactory,
92 ConfigParameterFactory $configParameterFactory
93 ) {
94 $this->php = $php;
95 $this->wordpress = $wordpress;
96 $this->util = $util;
97 $this->configFactory = $configFactory;
98 $this->configParameterFactory = $configParameterFactory;
99 }
100
101 /**
102 * Returns the id.
103 * @return string
104 */
105 public function getId(): string
106 {
107 return self::ID;
108 }
109
110 /**
111 * Returns the cache path.
112 * @return string
113 * @throws Exception
114 */
115 private function getPath(): string
116 {
117 if ($this->path === null) {
118 $this->path = $this->getConfig()->getParameterValue(self::CONFIG_PATH);
119
120 if ($this->util->endsWith($this->path, DIRECTORY_SEPARATOR) === false) {
121 $this->path .= DIRECTORY_SEPARATOR;
122 }
123 }
124
125 return $this->path;
126 }
127
128 /**
129 * Initialise the caching path.
130 * @throws Exception
131 */
132 public function init()
133 {
134 $path = $this->getPath();
135
136 if (is_dir($path) === false) {
137 $this->php->mkdir($path, 0775, true);
138 }
139
140 $htaccessFile = $path . '.htaccess';
141
142 if (file_exists($htaccessFile) === false) {
143 file_put_contents($htaccessFile, 'Deny from all');
144 }
145 }
146
147 /**
148 * Returns the cache config.
149 * @return Config
150 * @throws Exception
151 */
152 public function getConfig(): Config
153 {
154 if ($this->config === null) {
155 $this->config = $this->configFactory->createConfig(self::CONFIG_KEY);
156
157 $selections = [
158 self::METHOD_SERIALIZE,
159 self::METHOD_JSON,
160 self::METHOD_VAR_EXPORT
161 ];
162
163 if ($this->php->functionExists('igbinary_serialize') === true) {
164 $selections[] = self::METHOD_IGBINARY;
165 }
166
167 $configParameters = [
168 self::CONFIG_PATH => $this->configParameterFactory->createStringConfigParameter(
169 self::CONFIG_PATH,
170 $this->wordpress->getHomePath() . 'cache/uam'
171 ),
172 self::CONFIG_METHOD => $this->configParameterFactory->createSelectionConfigParameter(
173 self::CONFIG_METHOD,
174 self::METHOD_VAR_EXPORT,
175 $selections
176 )
177 ];
178 $this->config->setDefaultConfigParameters($configParameters);
179 }
180
181 return $this->config;
182 }
183
184 /**
185 * Returns the caching method.
186 * @return string|null
187 * @throws Exception
188 */
189 private function getCacheMethod(): ?string
190 {
191 $method = (string) $this->getConfig()->getParameterValue(self::CONFIG_METHOD);
192
193 if ($method === self::METHOD_IGBINARY
194 && ($this->php->functionExists('igbinary_serialize') === false
195 || $this->php->functionExists('igbinary_unserialize') === false)
196 ) {
197 $method = null;
198 }
199
200 return $method;
201 }
202
203 /**
204 * Returns the cache file name with path.
205 * @param string|null $method
206 * @param string $key
207 * @return string
208 * @throws Exception
209 */
210 private function getCacheFile(?string $method, string $key): string
211 {
212 $cacheFile = $this->getPath() . $key;
213 $cacheFile .= ($method === self::METHOD_VAR_EXPORT) ? '.php' : '.cache';
214
215 return $cacheFile;
216 }
217
218 /**
219 * Adds a value to the cache.
220 * @param string $key
221 * @param mixed $value
222 * @throws Exception
223 */
224 public function add(string $key, $value)
225 {
226 $method = $this->getCacheMethod();
227 $cacheFile = $this->getCacheFile($method, $key);
228
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 );
241 }
242 }
243
244 /**
245 * Returns a value from the cache.
246 * @param string $key
247 * @return mixed
248 * @throws Exception
249 */
250 public function get(string $key)
251 {
252 $method = $this->getCacheMethod();
253 $cacheFile = $this->getCacheFile($method, $key);
254
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 }
268 }
269
270 return null;
271 }
272
273 /**
274 * Invalidates the cache.
275 * @param string $key
276 * @throws Exception
277 */
278 public function invalidate(string $key)
279 {
280 $method = $this->getCacheMethod();
281 $cacheFile = $this->getCacheFile($method, $key);
282
283 if ((file_exists($cacheFile) === true)) {
284 unlink($cacheFile);
285 }
286 }
287 }
288