summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Traits/FilesystemTrait.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Traits/FilesystemTrait.php')
-rw-r--r--vendor/symfony/cache/Traits/FilesystemTrait.php113
1 files changed, 113 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Traits/FilesystemTrait.php b/vendor/symfony/cache/Traits/FilesystemTrait.php
new file mode 100644
index 0000000..47e9b83
--- /dev/null
+++ b/vendor/symfony/cache/Traits/FilesystemTrait.php
@@ -0,0 +1,113 @@
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\Traits;
13
14use Symfony\Component\Cache\Exception\CacheException;
15use Symfony\Component\Cache\Marshaller\MarshallerInterface;
16
17/**
18 * @author Nicolas Grekas <p@tchwork.com>
19 * @author Rob Frawley 2nd <rmf@src.run>
20 *
21 * @internal
22 */
23trait FilesystemTrait
24{
25 use FilesystemCommonTrait;
26
27 private MarshallerInterface $marshaller;
28
29 public function prune(): bool
30 {
31 $time = time();
32 $pruned = true;
33
34 foreach ($this->scanHashDir($this->directory) as $file) {
35 if (!$h = @fopen($file, 'r')) {
36 continue;
37 }
38
39 if (($expiresAt = (int) fgets($h)) && $time >= $expiresAt) {
40 fclose($h);
41 $pruned = (@unlink($file) || !file_exists($file)) && $pruned;
42 } else {
43 fclose($h);
44 }
45 }
46
47 return $pruned;
48 }
49
50 protected function doFetch(array $ids): iterable
51 {
52 $values = [];
53 $now = time();
54
55 foreach ($ids as $id) {
56 $file = $this->getFile($id);
57 if (!is_file($file) || !$h = @fopen($file, 'r')) {
58 continue;
59 }
60 if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
61 fclose($h);
62 @unlink($file);
63 } else {
64 $i = rawurldecode(rtrim(fgets($h)));
65 $value = stream_get_contents($h);
66 fclose($h);
67 if ($i === $id) {
68 $values[$id] = $this->marshaller->unmarshall($value);
69 }
70 }
71 }
72
73 return $values;
74 }
75
76 protected function doHave(string $id): bool
77 {
78 $file = $this->getFile($id);
79
80 return is_file($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
81 }
82
83 protected function doSave(array $values, int $lifetime): array|bool
84 {
85 $expiresAt = $lifetime ? (time() + $lifetime) : 0;
86 $values = $this->marshaller->marshall($values, $failed);
87
88 foreach ($values as $id => $value) {
89 if (!$this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".$value, $expiresAt)) {
90 $failed[] = $id;
91 }
92 }
93
94 if ($failed && !is_writable($this->directory)) {
95 throw new CacheException(sprintf('Cache directory is not writable (%s).', $this->directory));
96 }
97
98 return $failed;
99 }
100
101 private function getFileKey(string $file): string
102 {
103 if (!$h = @fopen($file, 'r')) {
104 return '';
105 }
106
107 fgets($h); // expiry
108 $encodedKey = fgets($h);
109 fclose($h);
110
111 return rawurldecode(rtrim($encodedKey));
112 }
113}