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

203 lines 5.6 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\Parameter\ConfigParameterFactory;
11 use UserAccessManager\Util\Util;
12 use UserAccessManager\Wrapper\Php;
13 use UserAccessManager\Wrapper\Wordpress;
14
15 class FileSystemCacheProvider implements CacheProviderInterface
16 {
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';
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 $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);
158 }
159 }
160
161 private function includeCachedValue(string $cacheFile): mixed
162 {
163 $cachedValue = null;
164 include($cacheFile);
165
166 return $cachedValue;
167 }
168
169 /**
170 * @throws Exception
171 */
172 public function get(string $key): mixed
173 {
174 $method = $this->getCacheMethod();
175 $cacheFile = $this->getCacheFile($method, $key);
176
177 if (file_exists($cacheFile) === false) {
178 return null;
179 }
180
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 };
188 }
189
190 /**
191 * @throws Exception
192 */
193 public function invalidate(string $key): void
194 {
195 $method = $this->getCacheMethod();
196 $cacheFile = $this->getCacheFile($method, $key);
197
198 if (file_exists($cacheFile) === true) {
199 unlink($cacheFile);
200 }
201 }
202 }
203