1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
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\VarExporter; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\VarExporter\Exception\ExceptionInterface; |
15
|
|
|
use Symfony\Component\VarExporter\Internal\Exporter; |
16
|
|
|
use Symfony\Component\VarExporter\Internal\Hydrator; |
|
|
|
|
17
|
|
|
use Symfony\Component\VarExporter\Internal\Registry; |
18
|
|
|
use 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 <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final 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<class-string, class-string> &$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
|
|
|
} |
115
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: