summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Adapter/PhpFilesAdapter.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Adapter/PhpFilesAdapter.php')
-rw-r--r--vendor/symfony/cache/Adapter/PhpFilesAdapter.php314
1 files changed, 314 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Adapter/PhpFilesAdapter.php b/vendor/symfony/cache/Adapter/PhpFilesAdapter.php
new file mode 100644
index 0000000..917ff16
--- /dev/null
+++ b/vendor/symfony/cache/Adapter/PhpFilesAdapter.php
@@ -0,0 +1,314 @@
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Cache\Adapter;
13
14use Symfony\Component\Cache\Exception\CacheException;
15use Symfony\Component\Cache\Exception\InvalidArgumentException;
16use Symfony\Component\Cache\PruneableInterface;
17use Symfony\Component\Cache\Traits\FilesystemCommonTrait;
18use Symfony\Component\VarExporter\VarExporter;
19
20/**
21 * @author Piotr Stankowski <git@trakos.pl>
22 * @author Nicolas Grekas <p@tchwork.com>
23 * @author Rob Frawley 2nd <rmf@src.run>
24 */
25class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
26{
27 use FilesystemCommonTrait {
28 doClear as private doCommonClear;
29 doDelete as private doCommonDelete;
30 }
31
32 private \Closure $includeHandler;
33 private array $values = [];
34 private array $files = [];
35
36 private static int $startTime;
37 private static array $valuesCache = [];
38
39 /**
40 * @param bool $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire.
41 * Doing so is encouraged because it fits perfectly OPcache's memory model.
42 *
43 * @throws CacheException if OPcache is not enabled
44 */
45 public function __construct(
46 string $namespace = '',
47 int $defaultLifetime = 0,
48 ?string $directory = null,
49 private bool $appendOnly = false,
50 ) {
51 self::$startTime ??= $_SERVER['REQUEST_TIME'] ?? time();
52 parent::__construct('', $defaultLifetime);
53 $this->init($namespace, $directory);
54 $this->includeHandler = static function ($type, $msg, $file, $line) {
55 throw new \ErrorException($msg, 0, $type, $file, $line);
56 };
57 }
58
59 public static function isSupported(): bool
60 {
61 self::$startTime ??= $_SERVER['REQUEST_TIME'] ?? time();
62
63 return \function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOL) && (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) || filter_var(\ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOL));
64 }
65
66 public function prune(): bool
67 {
68 $time = time();
69 $pruned = true;
70 $getExpiry = true;
71
72 set_error_handler($this->includeHandler);
73 try {
74 foreach ($this->scanHashDir($this->directory) as $file) {
75 try {
76 if (\is_array($expiresAt = include $file)) {
77 $expiresAt = $expiresAt[0];
78 }
79 } catch (\ErrorException $e) {
80 $expiresAt = $time;
81 }
82
83 if ($time >= $expiresAt) {
84 $pruned = ($this->doUnlink($file) || !file_exists($file)) && $pruned;
85 }
86 }
87 } finally {
88 restore_error_handler();
89 }
90
91 return $pruned;
92 }
93
94 protected function doFetch(array $ids): iterable
95 {
96 if ($this->appendOnly) {
97 $now = 0;
98 $missingIds = [];
99 } else {
100 $now = time();
101 $missingIds = $ids;
102 $ids = [];
103 }
104 $values = [];
105
106 begin:
107 $getExpiry = false;
108
109 foreach ($ids as $id) {
110 if (null === $value = $this->values[$id] ?? null) {
111 $missingIds[] = $id;
112 } elseif ('N;' === $value) {
113 $values[$id] = null;
114 } elseif (!\is_object($value)) {
115 $values[$id] = $value;
116 } elseif (!$value instanceof LazyValue) {
117 $values[$id] = $value();
118 } elseif (false === $values[$id] = include $value->file) {
119 unset($values[$id], $this->values[$id]);
120 $missingIds[] = $id;
121 }
122 if (!$this->appendOnly) {
123 unset($this->values[$id]);
124 }
125 }
126
127 if (!$missingIds) {
128 return $values;
129 }
130
131 set_error_handler($this->includeHandler);
132 try {
133 $getExpiry = true;
134
135 foreach ($missingIds as $k => $id) {
136 try {
137 $file = $this->files[$id] ??= $this->getFile($id);
138
139 if (isset(self::$valuesCache[$file])) {
140 [$expiresAt, $this->values[$id]] = self::$valuesCache[$file];
141 } elseif (\is_array($expiresAt = include $file)) {
142 if ($this->appendOnly) {
143 self::$valuesCache[$file] = $expiresAt;
144 }
145
146 [$expiresAt, $this->values[$id]] = $expiresAt;
147 } elseif ($now < $expiresAt) {
148 $this->values[$id] = new LazyValue($file);
149 }
150
151 if ($now >= $expiresAt) {
152 unset($this->values[$id], $missingIds[$k], self::$valuesCache[$file]);
153 }
154 } catch (\ErrorException $e) {
155 unset($missingIds[$k]);
156 }
157 }
158 } finally {
159 restore_error_handler();
160 }
161
162 $ids = $missingIds;
163 $missingIds = [];
164 goto begin;
165 }
166
167 protected function doHave(string $id): bool
168 {
169 if ($this->appendOnly && isset($this->values[$id])) {
170 return true;
171 }
172
173 set_error_handler($this->includeHandler);
174 try {
175 $file = $this->files[$id] ??= $this->getFile($id);
176 $getExpiry = true;
177
178 if (isset(self::$valuesCache[$file])) {
179 [$expiresAt, $value] = self::$valuesCache[$file];
180 } elseif (\is_array($expiresAt = include $file)) {
181 if ($this->appendOnly) {
182 self::$valuesCache[$file] = $expiresAt;
183 }
184
185 [$expiresAt, $value] = $expiresAt;
186 } elseif ($this->appendOnly) {
187 $value = new LazyValue($file);
188 }
189 } catch (\ErrorException) {
190 return false;
191 } finally {
192 restore_error_handler();
193 }
194 if ($this->appendOnly) {
195 $now = 0;
196 $this->values[$id] = $value;
197 } else {
198 $now = time();
199 }
200
201 return $now < $expiresAt;
202 }
203
204 protected function doSave(array $values, int $lifetime): array|bool
205 {
206 $ok = true;
207 $expiry = $lifetime ? time() + $lifetime : 'PHP_INT_MAX';
208 $allowCompile = self::isSupported();
209
210 foreach ($values as $key => $value) {
211 unset($this->values[$key]);
212 $isStaticValue = true;
213 if (null === $value) {
214 $value = "'N;'";
215 } elseif (\is_object($value) || \is_array($value)) {
216 try {
217 $value = VarExporter::export($value, $isStaticValue);
218 } catch (\Exception $e) {
219 throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable "%s" value.', $key, get_debug_type($value)), 0, $e);
220 }
221 } elseif (\is_string($value)) {
222 // Wrap "N;" in a closure to not confuse it with an encoded `null`
223 if ('N;' === $value) {
224 $isStaticValue = false;
225 }
226 $value = var_export($value, true);
227 } elseif (!\is_scalar($value)) {
228 throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable "%s" value.', $key, get_debug_type($value)));
229 } else {
230 $value = var_export($value, true);
231 }
232
233 $encodedKey = rawurlencode($key);
234
235 if ($isStaticValue) {
236 $value = "return [{$expiry}, {$value}];";
237 } elseif ($this->appendOnly) {
238 $value = "return [{$expiry}, static fn () => {$value}];";
239 } else {
240 // We cannot use a closure here because of https://bugs.php.net/76982
241 $value = str_replace('\Symfony\Component\VarExporter\Internal\\', '', $value);
242 $value = "namespace Symfony\Component\VarExporter\Internal;\n\nreturn \$getExpiry ? {$expiry} : {$value};";
243 }
244
245 $file = $this->files[$key] = $this->getFile($key, true);
246 // Since OPcache only compiles files older than the script execution start, set the file's mtime in the past
247 $ok = $this->write($file, "<?php //{$encodedKey}\n\n{$value}\n", self::$startTime - 10) && $ok;
248
249 if ($allowCompile) {
250 @opcache_invalidate($file, true);
251 @opcache_compile_file($file);
252 }
253 unset(self::$valuesCache[$file]);
254 }
255
256 if (!$ok && !is_writable($this->directory)) {
257 throw new CacheException(sprintf('Cache directory is not writable (%s).', $this->directory));
258 }
259
260 return $ok;
261 }
262
263 protected function doClear(string $namespace): bool
264 {
265 $this->values = [];
266
267 return $this->doCommonClear($namespace);
268 }
269
270 protected function doDelete(array $ids): bool
271 {
272 foreach ($ids as $id) {
273 unset($this->values[$id]);
274 }
275
276 return $this->doCommonDelete($ids);
277 }
278
279 protected function doUnlink(string $file): bool
280 {
281 unset(self::$valuesCache[$file]);
282
283 if (self::isSupported()) {
284 @opcache_invalidate($file, true);
285 }
286
287 return @unlink($file);
288 }
289
290 private function getFileKey(string $file): string
291 {
292 if (!$h = @fopen($file, 'r')) {
293 return '';
294 }
295
296 $encodedKey = substr(fgets($h), 8);
297 fclose($h);
298
299 return rawurldecode(rtrim($encodedKey));
300 }
301}
302
303/**
304 * @internal
305 */
306class LazyValue
307{
308 public string $file;
309
310 public function __construct(string $file)
311 {
312 $this->file = $file;
313 }
314}