diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/cache/Psr16Cache.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/cache/Psr16Cache.php')
-rw-r--r-- | vendor/symfony/cache/Psr16Cache.php | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Psr16Cache.php b/vendor/symfony/cache/Psr16Cache.php new file mode 100644 index 0000000..f21384f --- /dev/null +++ b/vendor/symfony/cache/Psr16Cache.php | |||
@@ -0,0 +1,240 @@ | |||
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; | ||
13 | |||
14 | use Psr\Cache\CacheException as Psr6CacheException; | ||
15 | use Psr\Cache\CacheItemPoolInterface; | ||
16 | use Psr\SimpleCache\CacheException as SimpleCacheException; | ||
17 | use Psr\SimpleCache\CacheInterface; | ||
18 | use Symfony\Component\Cache\Adapter\AdapterInterface; | ||
19 | use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
20 | use Symfony\Component\Cache\Traits\ProxyTrait; | ||
21 | |||
22 | /** | ||
23 | * Turns a PSR-6 cache into a PSR-16 one. | ||
24 | * | ||
25 | * @author Nicolas Grekas <p@tchwork.com> | ||
26 | */ | ||
27 | class Psr16Cache implements CacheInterface, PruneableInterface, ResettableInterface | ||
28 | { | ||
29 | use ProxyTrait; | ||
30 | |||
31 | private ?\Closure $createCacheItem = null; | ||
32 | private ?CacheItem $cacheItemPrototype = null; | ||
33 | private static \Closure $packCacheItem; | ||
34 | |||
35 | public function __construct(CacheItemPoolInterface $pool) | ||
36 | { | ||
37 | $this->pool = $pool; | ||
38 | |||
39 | if (!$pool instanceof AdapterInterface) { | ||
40 | return; | ||
41 | } | ||
42 | $cacheItemPrototype = &$this->cacheItemPrototype; | ||
43 | $createCacheItem = \Closure::bind( | ||
44 | static function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) { | ||
45 | $item = clone $cacheItemPrototype; | ||
46 | $item->poolHash = $item->innerItem = null; | ||
47 | if ($allowInt && \is_int($key)) { | ||
48 | $item->key = (string) $key; | ||
49 | } else { | ||
50 | \assert('' !== CacheItem::validateKey($key)); | ||
51 | $item->key = $key; | ||
52 | } | ||
53 | $item->value = $value; | ||
54 | $item->isHit = false; | ||
55 | |||
56 | return $item; | ||
57 | }, | ||
58 | null, | ||
59 | CacheItem::class | ||
60 | ); | ||
61 | $this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) { | ||
62 | if (null === $this->cacheItemPrototype) { | ||
63 | $this->get($allowInt && \is_int($key) ? (string) $key : $key); | ||
64 | } | ||
65 | $this->createCacheItem = $createCacheItem; | ||
66 | |||
67 | return $createCacheItem($key, null, $allowInt)->set($value); | ||
68 | }; | ||
69 | self::$packCacheItem ??= \Closure::bind( | ||
70 | static function (CacheItem $item) { | ||
71 | $item->newMetadata = $item->metadata; | ||
72 | |||
73 | return $item->pack(); | ||
74 | }, | ||
75 | null, | ||
76 | CacheItem::class | ||
77 | ); | ||
78 | } | ||
79 | |||
80 | public function get($key, $default = null): mixed | ||
81 | { | ||
82 | try { | ||
83 | $item = $this->pool->getItem($key); | ||
84 | } catch (SimpleCacheException $e) { | ||
85 | throw $e; | ||
86 | } catch (Psr6CacheException $e) { | ||
87 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
88 | } | ||
89 | if (null === $this->cacheItemPrototype) { | ||
90 | $this->cacheItemPrototype = clone $item; | ||
91 | $this->cacheItemPrototype->set(null); | ||
92 | } | ||
93 | |||
94 | return $item->isHit() ? $item->get() : $default; | ||
95 | } | ||
96 | |||
97 | public function set($key, $value, $ttl = null): bool | ||
98 | { | ||
99 | try { | ||
100 | if (null !== $f = $this->createCacheItem) { | ||
101 | $item = $f($key, $value); | ||
102 | } else { | ||
103 | $item = $this->pool->getItem($key)->set($value); | ||
104 | } | ||
105 | } catch (SimpleCacheException $e) { | ||
106 | throw $e; | ||
107 | } catch (Psr6CacheException $e) { | ||
108 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
109 | } | ||
110 | if (null !== $ttl) { | ||
111 | $item->expiresAfter($ttl); | ||
112 | } | ||
113 | |||
114 | return $this->pool->save($item); | ||
115 | } | ||
116 | |||
117 | public function delete($key): bool | ||
118 | { | ||
119 | try { | ||
120 | return $this->pool->deleteItem($key); | ||
121 | } catch (SimpleCacheException $e) { | ||
122 | throw $e; | ||
123 | } catch (Psr6CacheException $e) { | ||
124 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | public function clear(): bool | ||
129 | { | ||
130 | return $this->pool->clear(); | ||
131 | } | ||
132 | |||
133 | public function getMultiple($keys, $default = null): iterable | ||
134 | { | ||
135 | if ($keys instanceof \Traversable) { | ||
136 | $keys = iterator_to_array($keys, false); | ||
137 | } elseif (!\is_array($keys)) { | ||
138 | throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys))); | ||
139 | } | ||
140 | |||
141 | try { | ||
142 | $items = $this->pool->getItems($keys); | ||
143 | } catch (SimpleCacheException $e) { | ||
144 | throw $e; | ||
145 | } catch (Psr6CacheException $e) { | ||
146 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
147 | } | ||
148 | $values = []; | ||
149 | |||
150 | if (!$this->pool instanceof AdapterInterface) { | ||
151 | foreach ($items as $key => $item) { | ||
152 | $values[$key] = $item->isHit() ? $item->get() : $default; | ||
153 | } | ||
154 | |||
155 | return $values; | ||
156 | } | ||
157 | |||
158 | foreach ($items as $key => $item) { | ||
159 | $values[$key] = $item->isHit() ? (self::$packCacheItem)($item) : $default; | ||
160 | } | ||
161 | |||
162 | return $values; | ||
163 | } | ||
164 | |||
165 | public function setMultiple($values, $ttl = null): bool | ||
166 | { | ||
167 | $valuesIsArray = \is_array($values); | ||
168 | if (!$valuesIsArray && !$values instanceof \Traversable) { | ||
169 | throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given.', get_debug_type($values))); | ||
170 | } | ||
171 | $items = []; | ||
172 | |||
173 | try { | ||
174 | if (null !== $f = $this->createCacheItem) { | ||
175 | $valuesIsArray = false; | ||
176 | foreach ($values as $key => $value) { | ||
177 | $items[$key] = $f($key, $value, true); | ||
178 | } | ||
179 | } elseif ($valuesIsArray) { | ||
180 | $items = []; | ||
181 | foreach ($values as $key => $value) { | ||
182 | $items[] = (string) $key; | ||
183 | } | ||
184 | $items = $this->pool->getItems($items); | ||
185 | } else { | ||
186 | foreach ($values as $key => $value) { | ||
187 | if (\is_int($key)) { | ||
188 | $key = (string) $key; | ||
189 | } | ||
190 | $items[$key] = $this->pool->getItem($key)->set($value); | ||
191 | } | ||
192 | } | ||
193 | } catch (SimpleCacheException $e) { | ||
194 | throw $e; | ||
195 | } catch (Psr6CacheException $e) { | ||
196 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
197 | } | ||
198 | $ok = true; | ||
199 | |||
200 | foreach ($items as $key => $item) { | ||
201 | if ($valuesIsArray) { | ||
202 | $item->set($values[$key]); | ||
203 | } | ||
204 | if (null !== $ttl) { | ||
205 | $item->expiresAfter($ttl); | ||
206 | } | ||
207 | $ok = $this->pool->saveDeferred($item) && $ok; | ||
208 | } | ||
209 | |||
210 | return $this->pool->commit() && $ok; | ||
211 | } | ||
212 | |||
213 | public function deleteMultiple($keys): bool | ||
214 | { | ||
215 | if ($keys instanceof \Traversable) { | ||
216 | $keys = iterator_to_array($keys, false); | ||
217 | } elseif (!\is_array($keys)) { | ||
218 | throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys))); | ||
219 | } | ||
220 | |||
221 | try { | ||
222 | return $this->pool->deleteItems($keys); | ||
223 | } catch (SimpleCacheException $e) { | ||
224 | throw $e; | ||
225 | } catch (Psr6CacheException $e) { | ||
226 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
227 | } | ||
228 | } | ||
229 | |||
230 | public function has($key): bool | ||
231 | { | ||
232 | try { | ||
233 | return $this->pool->hasItem($key); | ||
234 | } catch (SimpleCacheException $e) { | ||
235 | throw $e; | ||
236 | } catch (Psr6CacheException $e) { | ||
237 | throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
238 | } | ||
239 | } | ||
240 | } | ||