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

201 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Cache;
6
7 use Exception;
8 use UserAccessManager\Config\Config;
9 use UserAccessManager\Config\ConfigFactory;
10 use UserAccessManager\Config\ConfigParameterFactory;
11 use UserAccessManager\Util\Util;
12 use UserAccessManager\Wrapper\Php;
13 use UserAccessManager\Wrapper\Wordpress;
14
15 class FileSystemCacheProvider implements CacheProviderInterface
16 {
17 const ID = 'FileSystemCacheProvider';
18 const CONFIG_KEY = 'uam_file_system_cache_provider';
19 const CONFIG_PATH = 'fs_cache_path';
20 const CONFIG_METHOD = 'fs_cache_method';
21 const METHOD_SERIALIZE = 'serialize';
22 const METHOD_IGBINARY = 'igbinary';
23 const METHOD_JSON = 'json';
24 const METHOD_VAR_EXPORT = 'var_export';
25
26 private ?Config $config = null;
27 private ?string $path = null;
28
29 public function __construct(
30 private Php $php,
31 private Wordpress $wordpress,
32 private Util $util,
33 private ConfigFactory $configFactory,
34 private ConfigParameterFactory $configParameterFactory
35 ) {
36 }
37
38 public function getId(): string
39 {
40 return self::ID;
41 }
42
43 /**
44 * @throws Exception
45 */
46 private function getPath(): string
47 {
48 if ($this->path === null) {
49 $this->path = $this->getConfig()->getParameterValue(self::CONFIG_PATH);
50
51 if ($this->util->endsWith($this->path, DIRECTORY_SEPARATOR) === false) {
52 $this->path .= DIRECTORY_SEPARATOR;
53 }
54 }
55
56 return $this->path;
57 }
58
59 /**
60 * @throws Exception
61 */
62 public function init(): void
63 {
64 $path = $this->getPath();
65
66 if (is_dir($path) === false) {
67 $this->php->mkdir($path, 0775, true);
68 }
69
70 $htaccessFile = $path . '.htaccess';
71
72 if (file_exists($htaccessFile) === false) {
73 file_put_contents($htaccessFile, 'Deny from all');
74 }
75 }
76
77 /**
78 * @throws Exception
79 */
80 public function getConfig(): Config
81 {
82 if ($this->config === null) {
83 $this->config = $this->configFactory->createConfig(self::CONFIG_KEY);
84
85 $selections = [
86 self::METHOD_SERIALIZE,
87 self::METHOD_JSON,
88 self::METHOD_VAR_EXPORT
89 ];
90
91 if ($this->php->functionExists('igbinary_serialize') === true) {
92 $selections[] = self::METHOD_IGBINARY;
93 }
94
95 $configParameters = [
96 self::CONFIG_PATH => $this->configParameterFactory->createStringConfigParameter(
97 self::CONFIG_PATH,
98 $this->wordpress->getHomePath() . 'cache/uam'
99 ),
100 self::CONFIG_METHOD => $this->configParameterFactory->createSelectionConfigParameter(
101 self::CONFIG_METHOD,
102 self::METHOD_VAR_EXPORT,
103 $selections
104 )
105 ];
106 $this->config->setDefaultConfigParameters($configParameters);
107 }
108
109 return $this->config;
110 }
111
112 /**
113 * @throws Exception
114 */
115 private function getCacheMethod(): ?string
116 {
117 $method = (string) $this->getConfig()->getParameterValue(self::CONFIG_METHOD);
118
119 if ($method === self::METHOD_IGBINARY
120 && ($this->php->functionExists('igbinary_serialize') === false
121 || $this->php->functionExists('igbinary_unserialize') === false)
122 ) {
123 $method = null;
124 }
125
126 return $method;
127 }
128
129 /**
130 * @throws Exception
131 */
132 private function getCacheFile(?string $method, string $key): string
133 {
134 $cacheFile = $this->getPath() . $key;
135 $cacheFile .= ($method === self::METHOD_VAR_EXPORT) ? '.php' : '.cache';
136
137 return $cacheFile;
138 }
139
140 /**
141 * @throws Exception
142 */
143 public function add(string $key, mixed $value): void
144 {
145 $method = $this->getCacheMethod();
146 $cacheFile = $this->getCacheFile($method, $key);
147
148 if ($method === self::METHOD_SERIALIZE) {
149 $this->php->filePutContents($cacheFile, base64_encode(serialize($value)), LOCK_EX);
150 } elseif ($method === self::METHOD_IGBINARY) {
151 $this->php->filePutContents($cacheFile, $this->php->igbinarySerialize($value), LOCK_EX);
152 } elseif ($method === self::METHOD_JSON) {
153 $this->php->filePutContents($cacheFile, json_encode($value), LOCK_EX);
154 } elseif ($method === self::METHOD_VAR_EXPORT) {
155 $this->php->filePutContents(
156 $cacheFile,
157 "<?php\n\$cachedValue = " . var_export($value, true) . ';',
158 LOCK_EX
159 );
160 }
161 }
162
163 /**
164 * @throws Exception
165 */
166 public function get(string $key): mixed
167 {
168 $method = $this->getCacheMethod();
169 $cacheFile = $this->getCacheFile($method, $key);
170
171 if ((file_exists($cacheFile) === true)) {
172 if ($method === self::METHOD_SERIALIZE) {
173 return unserialize(base64_decode(file_get_contents($cacheFile)));
174 } elseif ($method === self::METHOD_IGBINARY) {
175 return $this->php->igbinaryUnserialize(file_get_contents($cacheFile));
176 } elseif ($method === self::METHOD_JSON) {
177 return json_decode(file_get_contents($cacheFile), true);
178 } elseif ($method === self::METHOD_VAR_EXPORT) {
179 $cachedValue = null;
180 include($cacheFile);
181 return $cachedValue;
182 }
183 }
184
185 return null;
186 }
187
188 /**
189 * @throws Exception
190 */
191 public function invalidate(string $key): void
192 {
193 $method = $this->getCacheMethod();
194 $cacheFile = $this->getCacheFile($method, $key);
195
196 if ((file_exists($cacheFile) === true)) {
197 unlink($cacheFile);
198 }
199 }
200 }
201