diff options
Diffstat (limited to 'vendor/symfony/cache/Adapter/ProxyAdapter.php')
-rw-r--r-- | vendor/symfony/cache/Adapter/ProxyAdapter.php | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Adapter/ProxyAdapter.php b/vendor/symfony/cache/Adapter/ProxyAdapter.php new file mode 100644 index 0000000..c022dd5 --- /dev/null +++ b/vendor/symfony/cache/Adapter/ProxyAdapter.php | |||
@@ -0,0 +1,206 @@ | |||
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 | |||
12 | namespace Symfony\Component\Cache\Adapter; | ||
13 | |||
14 | use Psr\Cache\CacheItemInterface; | ||
15 | use Psr\Cache\CacheItemPoolInterface; | ||
16 | use Symfony\Component\Cache\CacheItem; | ||
17 | use Symfony\Component\Cache\PruneableInterface; | ||
18 | use Symfony\Component\Cache\ResettableInterface; | ||
19 | use Symfony\Component\Cache\Traits\ContractsTrait; | ||
20 | use Symfony\Component\Cache\Traits\ProxyTrait; | ||
21 | use Symfony\Contracts\Cache\CacheInterface; | ||
22 | |||
23 | /** | ||
24 | * @author Nicolas Grekas <p@tchwork.com> | ||
25 | */ | ||
26 | class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface | ||
27 | { | ||
28 | use ContractsTrait; | ||
29 | use ProxyTrait; | ||
30 | |||
31 | private string $namespace = ''; | ||
32 | private int $namespaceLen; | ||
33 | private string $poolHash; | ||
34 | private int $defaultLifetime; | ||
35 | |||
36 | private static \Closure $createCacheItem; | ||
37 | private static \Closure $setInnerItem; | ||
38 | |||
39 | public function __construct(CacheItemPoolInterface $pool, string $namespace = '', int $defaultLifetime = 0) | ||
40 | { | ||
41 | $this->pool = $pool; | ||
42 | $this->poolHash = spl_object_hash($pool); | ||
43 | if ('' !== $namespace) { | ||
44 | \assert('' !== CacheItem::validateKey($namespace)); | ||
45 | $this->namespace = $namespace; | ||
46 | } | ||
47 | $this->namespaceLen = \strlen($namespace); | ||
48 | $this->defaultLifetime = $defaultLifetime; | ||
49 | self::$createCacheItem ??= \Closure::bind( | ||
50 | static function ($key, $innerItem, $poolHash) { | ||
51 | $item = new CacheItem(); | ||
52 | $item->key = $key; | ||
53 | |||
54 | if (null === $innerItem) { | ||
55 | return $item; | ||
56 | } | ||
57 | |||
58 | $item->value = $innerItem->get(); | ||
59 | $item->isHit = $innerItem->isHit(); | ||
60 | $item->innerItem = $innerItem; | ||
61 | $item->poolHash = $poolHash; | ||
62 | |||
63 | if (!$item->unpack() && $innerItem instanceof CacheItem) { | ||
64 | $item->metadata = $innerItem->metadata; | ||
65 | } | ||
66 | $innerItem->set(null); | ||
67 | |||
68 | return $item; | ||
69 | }, | ||
70 | null, | ||
71 | CacheItem::class | ||
72 | ); | ||
73 | self::$setInnerItem ??= \Closure::bind( | ||
74 | static function (CacheItemInterface $innerItem, CacheItem $item, $expiry = null) { | ||
75 | $innerItem->set($item->pack()); | ||
76 | $innerItem->expiresAt(($expiry ?? $item->expiry) ? \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', $expiry ?? $item->expiry)) : null); | ||
77 | }, | ||
78 | null, | ||
79 | CacheItem::class | ||
80 | ); | ||
81 | } | ||
82 | |||
83 | public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed | ||
84 | { | ||
85 | if (!$this->pool instanceof CacheInterface) { | ||
86 | return $this->doGet($this, $key, $callback, $beta, $metadata); | ||
87 | } | ||
88 | |||
89 | return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) { | ||
90 | $item = (self::$createCacheItem)($key, $innerItem, $this->poolHash); | ||
91 | $item->set($value = $callback($item, $save)); | ||
92 | (self::$setInnerItem)($innerItem, $item); | ||
93 | |||
94 | return $value; | ||
95 | }, $beta, $metadata); | ||
96 | } | ||
97 | |||
98 | public function getItem(mixed $key): CacheItem | ||
99 | { | ||
100 | $item = $this->pool->getItem($this->getId($key)); | ||
101 | |||
102 | return (self::$createCacheItem)($key, $item, $this->poolHash); | ||
103 | } | ||
104 | |||
105 | public function getItems(array $keys = []): iterable | ||
106 | { | ||
107 | if ($this->namespaceLen) { | ||
108 | foreach ($keys as $i => $key) { | ||
109 | $keys[$i] = $this->getId($key); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | return $this->generateItems($this->pool->getItems($keys)); | ||
114 | } | ||
115 | |||
116 | public function hasItem(mixed $key): bool | ||
117 | { | ||
118 | return $this->pool->hasItem($this->getId($key)); | ||
119 | } | ||
120 | |||
121 | public function clear(string $prefix = ''): bool | ||
122 | { | ||
123 | if ($this->pool instanceof AdapterInterface) { | ||
124 | return $this->pool->clear($this->namespace.$prefix); | ||
125 | } | ||
126 | |||
127 | return $this->pool->clear(); | ||
128 | } | ||
129 | |||
130 | public function deleteItem(mixed $key): bool | ||
131 | { | ||
132 | return $this->pool->deleteItem($this->getId($key)); | ||
133 | } | ||
134 | |||
135 | public function deleteItems(array $keys): bool | ||
136 | { | ||
137 | if ($this->namespaceLen) { | ||
138 | foreach ($keys as $i => $key) { | ||
139 | $keys[$i] = $this->getId($key); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | return $this->pool->deleteItems($keys); | ||
144 | } | ||
145 | |||
146 | public function save(CacheItemInterface $item): bool | ||
147 | { | ||
148 | return $this->doSave($item, __FUNCTION__); | ||
149 | } | ||
150 | |||
151 | public function saveDeferred(CacheItemInterface $item): bool | ||
152 | { | ||
153 | return $this->doSave($item, __FUNCTION__); | ||
154 | } | ||
155 | |||
156 | public function commit(): bool | ||
157 | { | ||
158 | return $this->pool->commit(); | ||
159 | } | ||
160 | |||
161 | private function doSave(CacheItemInterface $item, string $method): bool | ||
162 | { | ||
163 | if (!$item instanceof CacheItem) { | ||
164 | return false; | ||
165 | } | ||
166 | $castItem = (array) $item; | ||
167 | |||
168 | if (null === $castItem["\0*\0expiry"] && 0 < $this->defaultLifetime) { | ||
169 | $castItem["\0*\0expiry"] = microtime(true) + $this->defaultLifetime; | ||
170 | } | ||
171 | |||
172 | if ($castItem["\0*\0poolHash"] === $this->poolHash && $castItem["\0*\0innerItem"]) { | ||
173 | $innerItem = $castItem["\0*\0innerItem"]; | ||
174 | } elseif ($this->pool instanceof AdapterInterface) { | ||
175 | // this is an optimization specific for AdapterInterface implementations | ||
176 | // so we can save a round-trip to the backend by just creating a new item | ||
177 | $innerItem = (self::$createCacheItem)($this->namespace.$castItem["\0*\0key"], null, $this->poolHash); | ||
178 | } else { | ||
179 | $innerItem = $this->pool->getItem($this->namespace.$castItem["\0*\0key"]); | ||
180 | } | ||
181 | |||
182 | (self::$setInnerItem)($innerItem, $item, $castItem["\0*\0expiry"]); | ||
183 | |||
184 | return $this->pool->$method($innerItem); | ||
185 | } | ||
186 | |||
187 | private function generateItems(iterable $items): \Generator | ||
188 | { | ||
189 | $f = self::$createCacheItem; | ||
190 | |||
191 | foreach ($items as $key => $item) { | ||
192 | if ($this->namespaceLen) { | ||
193 | $key = substr($key, $this->namespaceLen); | ||
194 | } | ||
195 | |||
196 | yield $key => $f($key, $item, $this->poolHash); | ||
197 | } | ||
198 | } | ||
199 | |||
200 | private function getId(mixed $key): string | ||
201 | { | ||
202 | \assert('' !== CacheItem::validateKey($key)); | ||
203 | |||
204 | return $this->namespace.$key; | ||
205 | } | ||
206 | } | ||