summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Traits/ValueWrapper.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Traits/ValueWrapper.php')
-rw-r--r--vendor/symfony/cache/Traits/ValueWrapper.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Traits/ValueWrapper.php b/vendor/symfony/cache/Traits/ValueWrapper.php
new file mode 100644
index 0000000..718a23d
--- /dev/null
+++ b/vendor/symfony/cache/Traits/ValueWrapper.php
@@ -0,0 +1,81 @@
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/**
13 * A short namespace-less class to serialize items with metadata.
14 *
15 * @author Nicolas Grekas <p@tchwork.com>
16 *
17 * @internal
18 */
19class ©
20{
21 private const EXPIRY_OFFSET = 1648206727;
22 private const INT32_MAX = 2147483647;
23
24 public readonly mixed $value;
25 public readonly array $metadata;
26
27 public function __construct(mixed $value, array $metadata)
28 {
29 $this->value = $value;
30 $this->metadata = $metadata;
31 }
32
33 public function __serialize(): array
34 {
35 // pack 31-bits ctime into 14bits
36 $c = $this->metadata['ctime'] ?? 0;
37 $c = match (true) {
38 $c > self::INT32_MAX - 2 => self::INT32_MAX,
39 $c > 0 => 1 + $c,
40 default => 1,
41 };
42 $e = 0;
43 while (!(0x40000000 & $c)) {
44 $c <<= 1;
45 ++$e;
46 }
47 $c = (0x7FE0 & ($c >> 16)) | $e;
48
49 $pack = pack('Vn', (int) (0.1 + ($this->metadata['expiry'] ?: self::INT32_MAX + self::EXPIRY_OFFSET) - self::EXPIRY_OFFSET), $c);
50
51 if (isset($this->metadata['tags'])) {
52 $pack[4] = $pack[4] | "\x80";
53 }
54
55 return [$pack => $this->value] + ($this->metadata['tags'] ?? []);
56 }
57
58 public function __unserialize(array $data): void
59 {
60 $pack = array_key_first($data);
61 $this->value = $data[$pack];
62
63 if ($hasTags = "\x80" === ($pack[4] & "\x80")) {
64 unset($data[$pack]);
65 $pack[4] = $pack[4] & "\x7F";
66 }
67
68 $metadata = unpack('Vexpiry/nctime', $pack);
69 $metadata['expiry'] += self::EXPIRY_OFFSET;
70
71 if (!$metadata['ctime'] = ((0x4000 | $metadata['ctime']) << 16 >> (0x1F & $metadata['ctime'])) - 1) {
72 unset($metadata['ctime']);
73 }
74
75 if ($hasTags) {
76 $metadata['tags'] = $data;
77 }
78
79 $this->metadata = $metadata;
80 }
81}