summaryrefslogtreecommitdiff
path: root/vendor/symfony/var-exporter/VarExporter.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/var-exporter/VarExporter.php')
-rw-r--r--vendor/symfony/var-exporter/VarExporter.php114
1 files changed, 114 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/VarExporter.php b/vendor/symfony/var-exporter/VarExporter.php
new file mode 100644
index 0000000..22e9b51
--- /dev/null
+++ b/vendor/symfony/var-exporter/VarExporter.php
@@ -0,0 +1,114 @@
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\VarExporter;
13
14use Symfony\Component\VarExporter\Exception\ExceptionInterface;
15use Symfony\Component\VarExporter\Internal\Exporter;
16use Symfony\Component\VarExporter\Internal\Hydrator;
17use Symfony\Component\VarExporter\Internal\Registry;
18use Symfony\Component\VarExporter\Internal\Values;
19
20/**
21 * Exports serializable PHP values to PHP code.
22 *
23 * VarExporter allows serializing PHP data structures to plain PHP code (like var_export())
24 * while preserving all the semantics associated with serialize() (unlike var_export()).
25 *
26 * By leveraging OPcache, the generated PHP code is faster than doing the same with unserialize().
27 *
28 * @author Nicolas Grekas <p@tchwork.com>
29 */
30final class VarExporter
31{
32 /**
33 * Exports a serializable PHP value to PHP code.
34 *
35 * @param bool &$isStaticValue Set to true after execution if the provided value is static, false otherwise
36 * @param array &$foundClasses Classes found in the value are added to this list as both keys and values
37 *
38 * @throws ExceptionInterface When the provided value cannot be serialized
39 */
40 public static function export(mixed $value, ?bool &$isStaticValue = null, array &$foundClasses = []): string
41 {
42 $isStaticValue = true;
43
44 if (!\is_object($value) && !(\is_array($value) && $value) && !\is_resource($value) || $value instanceof \UnitEnum) {
45 return Exporter::export($value);
46 }
47
48 $objectsPool = new \SplObjectStorage();
49 $refsPool = [];
50 $objectsCount = 0;
51
52 try {
53 $value = Exporter::prepare([$value], $objectsPool, $refsPool, $objectsCount, $isStaticValue)[0];
54 } finally {
55 $references = [];
56 foreach ($refsPool as $i => $v) {
57 if ($v[0]->count) {
58 $references[1 + $i] = $v[2];
59 }
60 $v[0] = $v[1];
61 }
62 }
63
64 if ($isStaticValue) {
65 return Exporter::export($value);
66 }
67
68 $classes = [];
69 $values = [];
70 $states = [];
71 foreach ($objectsPool as $i => $v) {
72 [, $class, $values[], $wakeup] = $objectsPool[$v];
73 $foundClasses[$class] = $classes[] = $class;
74
75 if (0 < $wakeup) {
76 $states[$wakeup] = $i;
77 } elseif (0 > $wakeup) {
78 $states[-$wakeup] = [$i, array_pop($values)];
79 $values[] = [];
80 }
81 }
82 ksort($states);
83
84 $wakeups = [null];
85 foreach ($states as $v) {
86 if (\is_array($v)) {
87 $wakeups[-$v[0]] = $v[1];
88 } else {
89 $wakeups[] = $v;
90 }
91 }
92
93 if (null === $wakeups[0]) {
94 unset($wakeups[0]);
95 }
96
97 $properties = [];
98 foreach ($values as $i => $vars) {
99 foreach ($vars as $class => $values) {
100 foreach ($values as $name => $v) {
101 $properties[$class][$name][$i] = $v;
102 }
103 }
104 }
105
106 if ($classes || $references) {
107 $value = new Hydrator(new Registry($classes), $references ? new Values($references) : null, $properties, $value, $wakeups);
108 } else {
109 $isStaticValue = true;
110 }
111
112 return Exporter::export($value);
113 }
114}