summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Marshaller/DefaultMarshaller.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Marshaller/DefaultMarshaller.php')
-rw-r--r--vendor/symfony/cache/Marshaller/DefaultMarshaller.php98
1 files changed, 98 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}