summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Marshaller
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Marshaller')
-rw-r--r--vendor/symfony/cache/Marshaller/DefaultMarshaller.php98
-rw-r--r--vendor/symfony/cache/Marshaller/DeflateMarshaller.php44
-rw-r--r--vendor/symfony/cache/Marshaller/MarshallerInterface.php38
-rw-r--r--vendor/symfony/cache/Marshaller/SodiumMarshaller.php74
-rw-r--r--vendor/symfony/cache/Marshaller/TagAwareMarshaller.php83
5 files changed, 337 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Marshaller/DefaultMarshaller.php b/vendor/symfony/cache/Marshaller/DefaultMarshaller.php
new file mode 100644
index 0000000..34bbeb8
--- /dev/null
+++ b/vendor/symfony/cache/Marshaller/DefaultMarshaller.php
@@ -0,0 +1,98 @@
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\Marshaller;
13
14use Symfony\Component\Cache\Exception\CacheException;
15
16/**
17 * Serializes/unserializes values using igbinary_serialize() if available, serialize() otherwise.
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 */
21class DefaultMarshaller implements MarshallerInterface
22{
23 private bool $useIgbinarySerialize = true;
24 private bool $throwOnSerializationFailure = false;
25
26 public function __construct(?bool $useIgbinarySerialize = null, bool $throwOnSerializationFailure = false)
27 {
28 if (null === $useIgbinarySerialize) {
29 $useIgbinarySerialize = \extension_loaded('igbinary') && version_compare('3.1.6', phpversion('igbinary'), '<=');
30 } elseif ($useIgbinarySerialize && (!\extension_loaded('igbinary') || version_compare('3.1.6', phpversion('igbinary'), '>'))) {
31 throw new CacheException(\extension_loaded('igbinary') ? 'Please upgrade the "igbinary" PHP extension to v3.1.6 or higher.' : 'The "igbinary" PHP extension is not loaded.');
32 }
33 $this->useIgbinarySerialize = $useIgbinarySerialize;
34 $this->throwOnSerializationFailure = $throwOnSerializationFailure;
35 }
36
37 public function marshall(array $values, ?array &$failed): array
38 {
39 $serialized = $failed = [];
40
41 foreach ($values as $id => $value) {
42 try {
43 if ($this->useIgbinarySerialize) {
44 $serialized[$id] = igbinary_serialize($value);
45 } else {
46 $serialized[$id] = serialize($value);
47 }
48 } catch (\Exception $e) {
49 if ($this->throwOnSerializationFailure) {
50 throw new \ValueError($e->getMessage(), 0, $e);
51 }
52 $failed[] = $id;
53 }
54 }
55
56 return $serialized;
57 }
58
59 public function unmarshall(string $value): mixed
60 {
61 if ('b:0;' === $value) {
62 return false;
63 }
64 if ('N;' === $value) {
65 return null;
66 }
67 static $igbinaryNull;
68 if ($value === $igbinaryNull ??= \extension_loaded('igbinary') ? igbinary_serialize(null) : false) {
69 return null;
70 }
71 $unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
72 try {
73 if (':' === ($value[1] ?? ':')) {
74 if (false !== $value = unserialize($value)) {
75 return $value;
76 }
77 } elseif (false === $igbinaryNull) {
78 throw new \RuntimeException('Failed to unserialize values, did you forget to install the "igbinary" extension?');
79 } elseif (null !== $value = igbinary_unserialize($value)) {
80 return $value;
81 }
82
83 throw new \DomainException(error_get_last() ? error_get_last()['message'] : 'Failed to unserialize values.');
84 } catch (\Error $e) {
85 throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR, $e->getFile(), $e->getLine());
86 } finally {
87 ini_set('unserialize_callback_func', $unserializeCallbackHandler);
88 }
89 }
90
91 /**
92 * @internal
93 */
94 public static function handleUnserializeCallback(string $class): never
95 {
96 throw new \DomainException('Class not found: '.$class);
97 }
98}
diff --git a/vendor/symfony/cache/Marshaller/DeflateMarshaller.php b/vendor/symfony/cache/Marshaller/DeflateMarshaller.php
new file mode 100644
index 0000000..f35241c
--- /dev/null
+++ b/vendor/symfony/cache/Marshaller/DeflateMarshaller.php
@@ -0,0 +1,44 @@
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\Marshaller;
13
14use Symfony\Component\Cache\Exception\CacheException;
15
16/**
17 * Compresses values using gzdeflate().
18 *
19 * @author Nicolas Grekas <p@tchwork.com>
20 */
21class DeflateMarshaller implements MarshallerInterface
22{
23 public function __construct(
24 private MarshallerInterface $marshaller,
25 ) {
26 if (!\function_exists('gzdeflate')) {
27 throw new CacheException('The "zlib" PHP extension is not loaded.');
28 }
29 }
30
31 public function marshall(array $values, ?array &$failed): array
32 {
33 return array_map('gzdeflate', $this->marshaller->marshall($values, $failed));
34 }
35
36 public function unmarshall(string $value): mixed
37 {
38 if (false !== $inflatedValue = @gzinflate($value)) {
39 $value = $inflatedValue;
40 }
41
42 return $this->marshaller->unmarshall($value);
43 }
44}
diff --git a/vendor/symfony/cache/Marshaller/MarshallerInterface.php b/vendor/symfony/cache/Marshaller/MarshallerInterface.php
new file mode 100644
index 0000000..5b81aad
--- /dev/null
+++ b/vendor/symfony/cache/Marshaller/MarshallerInterface.php
@@ -0,0 +1,38 @@
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\Marshaller;
13
14/**
15 * Serializes/unserializes PHP values.
16 *
17 * Implementations of this interface MUST deal with errors carefully. They MUST
18 * also deal with forward and backward compatibility at the storage format level.
19 *
20 * @author Nicolas Grekas <p@tchwork.com>
21 */
22interface MarshallerInterface
23{
24 /**
25 * Serializes a list of values.
26 *
27 * When serialization fails for a specific value, no exception should be
28 * thrown. Instead, its key should be listed in $failed.
29 */
30 public function marshall(array $values, ?array &$failed): array;
31
32 /**
33 * Unserializes a single value and throws an exception if anything goes wrong.
34 *
35 * @throws \Exception Whenever unserialization fails
36 */
37 public function unmarshall(string $value): mixed;
38}
diff --git a/vendor/symfony/cache/Marshaller/SodiumMarshaller.php b/vendor/symfony/cache/Marshaller/SodiumMarshaller.php
new file mode 100644
index 0000000..77d16e8
--- /dev/null
+++ b/vendor/symfony/cache/Marshaller/SodiumMarshaller.php
@@ -0,0 +1,74 @@
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\Marshaller;
13
14use Symfony\Component\Cache\Exception\CacheException;
15use Symfony\Component\Cache\Exception\InvalidArgumentException;
16
17/**
18 * Encrypt/decrypt values using Libsodium.
19 *
20 * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
21 */
22class SodiumMarshaller implements MarshallerInterface
23{
24 private MarshallerInterface $marshaller;
25
26 /**
27 * @param string[] $decryptionKeys The key at index "0" is required and is used to decrypt and encrypt values;
28 * more rotating keys can be provided to decrypt values;
29 * each key must be generated using sodium_crypto_box_keypair()
30 */
31 public function __construct(
32 private array $decryptionKeys,
33 ?MarshallerInterface $marshaller = null,
34 ) {
35 if (!self::isSupported()) {
36 throw new CacheException('The "sodium" PHP extension is not loaded.');
37 }
38
39 if (!isset($decryptionKeys[0])) {
40 throw new InvalidArgumentException('At least one decryption key must be provided at index "0".');
41 }
42
43 $this->marshaller = $marshaller ?? new DefaultMarshaller();
44 }
45
46 public static function isSupported(): bool
47 {
48 return \function_exists('sodium_crypto_box_seal');
49 }
50
51 public function marshall(array $values, ?array &$failed): array
52 {
53 $encryptionKey = sodium_crypto_box_publickey($this->decryptionKeys[0]);
54
55 $encryptedValues = [];
56 foreach ($this->marshaller->marshall($values, $failed) as $k => $v) {
57 $encryptedValues[$k] = sodium_crypto_box_seal($v, $encryptionKey);
58 }
59
60 return $encryptedValues;
61 }
62
63 public function unmarshall(string $value): mixed
64 {
65 foreach ($this->decryptionKeys as $k) {
66 if (false !== $decryptedValue = @sodium_crypto_box_seal_open($value, $k)) {
67 $value = $decryptedValue;
68 break;
69 }
70 }
71
72 return $this->marshaller->unmarshall($value);
73 }
74}
diff --git a/vendor/symfony/cache/Marshaller/TagAwareMarshaller.php b/vendor/symfony/cache/Marshaller/TagAwareMarshaller.php
new file mode 100644
index 0000000..825f32c
--- /dev/null
+++ b/vendor/symfony/cache/Marshaller/TagAwareMarshaller.php
@@ -0,0 +1,83 @@
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\Marshaller;
13
14/**
15 * A marshaller optimized for data structures generated by AbstractTagAwareAdapter.
16 *
17 * @author Nicolas Grekas <p@tchwork.com>
18 */
19class TagAwareMarshaller implements MarshallerInterface
20{
21 private MarshallerInterface $marshaller;
22
23 public function __construct(?MarshallerInterface $marshaller = null)
24 {
25 $this->marshaller = $marshaller ?? new DefaultMarshaller();
26 }
27
28 public function marshall(array $values, ?array &$failed): array
29 {
30 $failed = $notSerialized = $serialized = [];
31
32 foreach ($values as $id => $value) {
33 if (\is_array($value) && \is_array($value['tags'] ?? null) && \array_key_exists('value', $value) && \count($value) === 2 + (\is_string($value['meta'] ?? null) && 8 === \strlen($value['meta']))) {
34 // if the value is an array with keys "tags", "value" and "meta", use a compact serialization format
35 // magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F allow detecting this format quickly in unmarshall()
36
37 $v = $this->marshaller->marshall($value, $f);
38
39 if ($f) {
40 $f = [];
41 $failed[] = $id;
42 } else {
43 if ([] === $value['tags']) {
44 $v['tags'] = '';
45 }
46
47 $serialized[$id] = "\x9D".($value['meta'] ?? "\0\0\0\0\0\0\0\0").pack('N', \strlen($v['tags'])).$v['tags'].$v['value'];
48 $serialized[$id][9] = "\x5F";
49 }
50 } else {
51 // other arbitrary values are serialized using the decorated marshaller below
52 $notSerialized[$id] = $value;
53 }
54 }
55
56 if ($notSerialized) {
57 $serialized += $this->marshaller->marshall($notSerialized, $f);
58 $failed = array_merge($failed, $f);
59 }
60
61 return $serialized;
62 }
63
64 public function unmarshall(string $value): mixed
65 {
66 // detect the compact format used in marshall() using magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F
67 if (13 >= \strlen($value) || "\x9D" !== $value[0] || "\0" !== $value[5] || "\x5F" !== $value[9]) {
68 return $this->marshaller->unmarshall($value);
69 }
70
71 // data consists of value, tags and metadata which we need to unpack
72 $meta = substr($value, 1, 12);
73 $meta[8] = "\0";
74 $tagLen = unpack('Nlen', $meta, 8)['len'];
75 $meta = substr($meta, 0, 8);
76
77 return [
78 'value' => $this->marshaller->unmarshall(substr($value, 13 + $tagLen)),
79 'tags' => $tagLen ? $this->marshaller->unmarshall(substr($value, 13, $tagLen)) : [],
80 'meta' => "\0\0\0\0\0\0\0\0" === $meta ? null : $meta,
81 ];
82 }
83}