PluginProbe
User Access Manager / 2.1.0
User Access Manager v2.1.0
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.1.0, at src/Cache/FileSystemCacheProvider.php

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