diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/var-exporter/Internal/Exporter.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/var-exporter/Internal/Exporter.php')
-rw-r--r-- | vendor/symfony/var-exporter/Internal/Exporter.php | 418 |
1 files changed, 418 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/Internal/Exporter.php b/vendor/symfony/var-exporter/Internal/Exporter.php new file mode 100644 index 0000000..9f4f593 --- /dev/null +++ b/vendor/symfony/var-exporter/Internal/Exporter.php | |||
@@ -0,0 +1,418 @@ | |||
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 | namespace Symfony\Component\VarExporter\Internal; | ||
13 | |||
14 | use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException; | ||
15 | |||
16 | /** | ||
17 | * @author Nicolas Grekas <p@tchwork.com> | ||
18 | * | ||
19 | * @internal | ||
20 | */ | ||
21 | class Exporter | ||
22 | { | ||
23 | /** | ||
24 | * Prepares an array of values for VarExporter. | ||
25 | * | ||
26 | * For performance this method is public and has no type-hints. | ||
27 | * | ||
28 | * @param array &$values | ||
29 | * @param \SplObjectStorage $objectsPool | ||
30 | * @param array &$refsPool | ||
31 | * @param int &$objectsCount | ||
32 | * @param bool &$valuesAreStatic | ||
33 | * | ||
34 | * @return array | ||
35 | * | ||
36 | * @throws NotInstantiableTypeException When a value cannot be serialized | ||
37 | */ | ||
38 | public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount, &$valuesAreStatic) | ||
39 | { | ||
40 | $refs = $values; | ||
41 | foreach ($values as $k => $value) { | ||
42 | if (\is_resource($value)) { | ||
43 | throw new NotInstantiableTypeException(get_resource_type($value).' resource'); | ||
44 | } | ||
45 | $refs[$k] = $objectsPool; | ||
46 | |||
47 | if ($isRef = !$valueIsStatic = $values[$k] !== $objectsPool) { | ||
48 | $values[$k] = &$value; // Break hard references to make $values completely | ||
49 | unset($value); // independent from the original structure | ||
50 | $refs[$k] = $value = $values[$k]; | ||
51 | if ($value instanceof Reference && 0 > $value->id) { | ||
52 | $valuesAreStatic = false; | ||
53 | ++$value->count; | ||
54 | continue; | ||
55 | } | ||
56 | $refsPool[] = [&$refs[$k], $value, &$value]; | ||
57 | $refs[$k] = $values[$k] = new Reference(-\count($refsPool), $value); | ||
58 | } | ||
59 | |||
60 | if (\is_array($value)) { | ||
61 | if ($value) { | ||
62 | $value = self::prepare($value, $objectsPool, $refsPool, $objectsCount, $valueIsStatic); | ||
63 | } | ||
64 | goto handle_value; | ||
65 | } elseif (!\is_object($value) || $value instanceof \UnitEnum) { | ||
66 | goto handle_value; | ||
67 | } | ||
68 | |||
69 | $valueIsStatic = false; | ||
70 | if (isset($objectsPool[$value])) { | ||
71 | ++$objectsCount; | ||
72 | $value = new Reference($objectsPool[$value][0]); | ||
73 | goto handle_value; | ||
74 | } | ||
75 | |||
76 | $class = $value::class; | ||
77 | $reflector = Registry::$reflectors[$class] ??= Registry::getClassReflector($class); | ||
78 | $properties = []; | ||
79 | |||
80 | if ($reflector->hasMethod('__serialize')) { | ||
81 | if (!$reflector->getMethod('__serialize')->isPublic()) { | ||
82 | throw new \Error(sprintf('Call to %s method "%s::__serialize()".', $reflector->getMethod('__serialize')->isProtected() ? 'protected' : 'private', $class)); | ||
83 | } | ||
84 | |||
85 | if (!\is_array($serializeProperties = $value->__serialize())) { | ||
86 | throw new \TypeError($class.'::__serialize() must return an array'); | ||
87 | } | ||
88 | |||
89 | if ($reflector->hasMethod('__unserialize')) { | ||
90 | $properties = $serializeProperties; | ||
91 | } else { | ||
92 | foreach ($serializeProperties as $n => $v) { | ||
93 | $c = $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass'; | ||
94 | $properties[$c][$n] = $v; | ||
95 | } | ||
96 | } | ||
97 | |||
98 | goto prepare_value; | ||
99 | } | ||
100 | |||
101 | $sleep = null; | ||
102 | $proto = Registry::$prototypes[$class]; | ||
103 | |||
104 | if (($value instanceof \ArrayIterator || $value instanceof \ArrayObject) && null !== $proto) { | ||
105 | // ArrayIterator and ArrayObject need special care because their "flags" | ||
106 | // option changes the behavior of the (array) casting operator. | ||
107 | [$arrayValue, $properties] = self::getArrayObjectProperties($value, $proto); | ||
108 | |||
109 | // populates Registry::$prototypes[$class] with a new instance | ||
110 | Registry::getClassReflector($class, Registry::$instantiableWithoutConstructor[$class], Registry::$cloneable[$class]); | ||
111 | } elseif ($value instanceof \SplObjectStorage && Registry::$cloneable[$class] && null !== $proto) { | ||
112 | // By implementing Serializable, SplObjectStorage breaks | ||
113 | // internal references; let's deal with it on our own. | ||
114 | foreach (clone $value as $v) { | ||
115 | $properties[] = $v; | ||
116 | $properties[] = $value[$v]; | ||
117 | } | ||
118 | $properties = ['SplObjectStorage' => ["\0" => $properties]]; | ||
119 | $arrayValue = (array) $value; | ||
120 | } elseif ($value instanceof \Serializable | ||
121 | || $value instanceof \__PHP_Incomplete_Class | ||
122 | ) { | ||
123 | ++$objectsCount; | ||
124 | $objectsPool[$value] = [$id = \count($objectsPool), serialize($value), [], 0]; | ||
125 | $value = new Reference($id); | ||
126 | goto handle_value; | ||
127 | } else { | ||
128 | if (method_exists($class, '__sleep')) { | ||
129 | if (!\is_array($sleep = $value->__sleep())) { | ||
130 | trigger_error('serialize(): __sleep should return an array only containing the names of instance-variables to serialize', \E_USER_NOTICE); | ||
131 | $value = null; | ||
132 | goto handle_value; | ||
133 | } | ||
134 | $sleep = array_flip($sleep); | ||
135 | } | ||
136 | |||
137 | $arrayValue = (array) $value; | ||
138 | } | ||
139 | |||
140 | $proto = (array) $proto; | ||
141 | |||
142 | foreach ($arrayValue as $name => $v) { | ||
143 | $i = 0; | ||
144 | $n = (string) $name; | ||
145 | if ('' === $n || "\0" !== $n[0]) { | ||
146 | $c = $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass'; | ||
147 | } elseif ('*' === $n[1]) { | ||
148 | $n = substr($n, 3); | ||
149 | $c = $reflector->getProperty($n)->class; | ||
150 | if ('Error' === $c) { | ||
151 | $c = 'TypeError'; | ||
152 | } elseif ('Exception' === $c) { | ||
153 | $c = 'ErrorException'; | ||
154 | } | ||
155 | } else { | ||
156 | $i = strpos($n, "\0", 2); | ||
157 | $c = substr($n, 1, $i - 1); | ||
158 | $n = substr($n, 1 + $i); | ||
159 | } | ||
160 | if (null !== $sleep) { | ||
161 | if (!isset($sleep[$name]) && (!isset($sleep[$n]) || ($i && $c !== $class))) { | ||
162 | unset($arrayValue[$name]); | ||
163 | continue; | ||
164 | } | ||
165 | unset($sleep[$name], $sleep[$n]); | ||
166 | } | ||
167 | if (!\array_key_exists($name, $proto) || $proto[$name] !== $v || "\x00Error\x00trace" === $name || "\x00Exception\x00trace" === $name) { | ||
168 | $properties[$c][$n] = $v; | ||
169 | } | ||
170 | } | ||
171 | if ($sleep) { | ||
172 | foreach ($sleep as $n => $v) { | ||
173 | trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), \E_USER_NOTICE); | ||
174 | } | ||
175 | } | ||
176 | if (method_exists($class, '__unserialize')) { | ||
177 | $properties = $arrayValue; | ||
178 | } | ||
179 | |||
180 | prepare_value: | ||
181 | $objectsPool[$value] = [$id = \count($objectsPool)]; | ||
182 | $properties = self::prepare($properties, $objectsPool, $refsPool, $objectsCount, $valueIsStatic); | ||
183 | ++$objectsCount; | ||
184 | $objectsPool[$value] = [$id, $class, $properties, method_exists($class, '__unserialize') ? -$objectsCount : (method_exists($class, '__wakeup') ? $objectsCount : 0)]; | ||
185 | |||
186 | $value = new Reference($id); | ||
187 | |||
188 | handle_value: | ||
189 | if ($isRef) { | ||
190 | unset($value); // Break the hard reference created above | ||
191 | } elseif (!$valueIsStatic) { | ||
192 | $values[$k] = $value; | ||
193 | } | ||
194 | $valuesAreStatic = $valueIsStatic && $valuesAreStatic; | ||
195 | } | ||
196 | |||
197 | return $values; | ||
198 | } | ||
199 | |||
200 | public static function export($value, $indent = '') | ||
201 | { | ||
202 | switch (true) { | ||
203 | case \is_int($value) || \is_float($value): return var_export($value, true); | ||
204 | case [] === $value: return '[]'; | ||
205 | case false === $value: return 'false'; | ||
206 | case true === $value: return 'true'; | ||
207 | case null === $value: return 'null'; | ||
208 | case '' === $value: return "''"; | ||
209 | case $value instanceof \UnitEnum: return '\\'.ltrim(var_export($value, true), '\\'); | ||
210 | } | ||
211 | |||
212 | if ($value instanceof Reference) { | ||
213 | if (0 <= $value->id) { | ||
214 | return '$o['.$value->id.']'; | ||
215 | } | ||
216 | if (!$value->count) { | ||
217 | return self::export($value->value, $indent); | ||
218 | } | ||
219 | $value = -$value->id; | ||
220 | |||
221 | return '&$r['.$value.']'; | ||
222 | } | ||
223 | $subIndent = $indent.' '; | ||
224 | |||
225 | if (\is_string($value)) { | ||
226 | $code = sprintf("'%s'", addcslashes($value, "'\\")); | ||
227 | |||
228 | $code = preg_replace_callback("/((?:[\\0\\r\\n]|\u{202A}|\u{202B}|\u{202D}|\u{202E}|\u{2066}|\u{2067}|\u{2068}|\u{202C}|\u{2069})++)(.)/", function ($m) use ($subIndent) { | ||
229 | $m[1] = sprintf('\'."%s".\'', str_replace( | ||
230 | ["\0", "\r", "\n", "\u{202A}", "\u{202B}", "\u{202D}", "\u{202E}", "\u{2066}", "\u{2067}", "\u{2068}", "\u{202C}", "\u{2069}", '\n\\'], | ||
231 | ['\0', '\r', '\n', '\u{202A}', '\u{202B}', '\u{202D}', '\u{202E}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{202C}', '\u{2069}', '\n"'."\n".$subIndent.'."\\'], | ||
232 | $m[1] | ||
233 | )); | ||
234 | |||
235 | if ("'" === $m[2]) { | ||
236 | return substr($m[1], 0, -2); | ||
237 | } | ||
238 | |||
239 | if (str_ends_with($m[1], 'n".\'')) { | ||
240 | return substr_replace($m[1], "\n".$subIndent.".'".$m[2], -2); | ||
241 | } | ||
242 | |||
243 | return $m[1].$m[2]; | ||
244 | }, $code, -1, $count); | ||
245 | |||
246 | if ($count && str_starts_with($code, "''.")) { | ||
247 | $code = substr($code, 3); | ||
248 | } | ||
249 | |||
250 | return $code; | ||
251 | } | ||
252 | |||
253 | if (\is_array($value)) { | ||
254 | $j = -1; | ||
255 | $code = ''; | ||
256 | foreach ($value as $k => $v) { | ||
257 | $code .= $subIndent; | ||
258 | if (!\is_int($k) || 1 !== $k - $j) { | ||
259 | $code .= self::export($k, $subIndent).' => '; | ||
260 | } | ||
261 | if (\is_int($k) && $k > $j) { | ||
262 | $j = $k; | ||
263 | } | ||
264 | $code .= self::export($v, $subIndent).",\n"; | ||
265 | } | ||
266 | |||
267 | return "[\n".$code.$indent.']'; | ||
268 | } | ||
269 | |||
270 | if ($value instanceof Values) { | ||
271 | $code = $subIndent."\$r = [],\n"; | ||
272 | foreach ($value->values as $k => $v) { | ||
273 | $code .= $subIndent.'$r['.$k.'] = '.self::export($v, $subIndent).",\n"; | ||
274 | } | ||
275 | |||
276 | return "[\n".$code.$indent.']'; | ||
277 | } | ||
278 | |||
279 | if ($value instanceof Registry) { | ||
280 | return self::exportRegistry($value, $indent, $subIndent); | ||
281 | } | ||
282 | |||
283 | if ($value instanceof Hydrator) { | ||
284 | return self::exportHydrator($value, $indent, $subIndent); | ||
285 | } | ||
286 | |||
287 | throw new \UnexpectedValueException(sprintf('Cannot export value of type "%s".', get_debug_type($value))); | ||
288 | } | ||
289 | |||
290 | private static function exportRegistry(Registry $value, string $indent, string $subIndent): string | ||
291 | { | ||
292 | $code = ''; | ||
293 | $serializables = []; | ||
294 | $seen = []; | ||
295 | $prototypesAccess = 0; | ||
296 | $factoriesAccess = 0; | ||
297 | $r = '\\'.Registry::class; | ||
298 | $j = -1; | ||
299 | |||
300 | foreach ($value->classes as $k => $class) { | ||
301 | if (':' === ($class[1] ?? null)) { | ||
302 | $serializables[$k] = $class; | ||
303 | continue; | ||
304 | } | ||
305 | if (!Registry::$instantiableWithoutConstructor[$class]) { | ||
306 | if (is_subclass_of($class, 'Serializable') && !method_exists($class, '__unserialize')) { | ||
307 | $serializables[$k] = 'C:'.\strlen($class).':"'.$class.'":0:{}'; | ||
308 | } else { | ||
309 | $serializables[$k] = 'O:'.\strlen($class).':"'.$class.'":0:{}'; | ||
310 | } | ||
311 | if (is_subclass_of($class, 'Throwable')) { | ||
312 | $eol = is_subclass_of($class, 'Error') ? "\0Error\0" : "\0Exception\0"; | ||
313 | $serializables[$k] = substr_replace($serializables[$k], '1:{s:'.(5 + \strlen($eol)).':"'.$eol.'trace";a:0:{}}', -4); | ||
314 | } | ||
315 | continue; | ||
316 | } | ||
317 | $code .= $subIndent.(1 !== $k - $j ? $k.' => ' : ''); | ||
318 | $j = $k; | ||
319 | $eol = ",\n"; | ||
320 | $c = '['.self::export($class).']'; | ||
321 | |||
322 | if ($seen[$class] ?? false) { | ||
323 | if (Registry::$cloneable[$class]) { | ||
324 | ++$prototypesAccess; | ||
325 | $code .= 'clone $p'.$c; | ||
326 | } else { | ||
327 | ++$factoriesAccess; | ||
328 | $code .= '$f'.$c.'()'; | ||
329 | } | ||
330 | } else { | ||
331 | $seen[$class] = true; | ||
332 | if (Registry::$cloneable[$class]) { | ||
333 | $code .= 'clone ('.($prototypesAccess++ ? '$p' : '($p = &'.$r.'::$prototypes)').$c.' ?? '.$r.'::p'; | ||
334 | } else { | ||
335 | $code .= '('.($factoriesAccess++ ? '$f' : '($f = &'.$r.'::$factories)').$c.' ?? '.$r.'::f'; | ||
336 | $eol = '()'.$eol; | ||
337 | } | ||
338 | $code .= '('.substr($c, 1, -1).'))'; | ||
339 | } | ||
340 | $code .= $eol; | ||
341 | } | ||
342 | |||
343 | if (1 === $prototypesAccess) { | ||
344 | $code = str_replace('($p = &'.$r.'::$prototypes)', $r.'::$prototypes', $code); | ||
345 | } | ||
346 | if (1 === $factoriesAccess) { | ||
347 | $code = str_replace('($f = &'.$r.'::$factories)', $r.'::$factories', $code); | ||
348 | } | ||
349 | if ('' !== $code) { | ||
350 | $code = "\n".$code.$indent; | ||
351 | } | ||
352 | |||
353 | if ($serializables) { | ||
354 | $code = $r.'::unserialize(['.$code.'], '.self::export($serializables, $indent).')'; | ||
355 | } else { | ||
356 | $code = '['.$code.']'; | ||
357 | } | ||
358 | |||
359 | return '$o = '.$code; | ||
360 | } | ||
361 | |||
362 | private static function exportHydrator(Hydrator $value, string $indent, string $subIndent): string | ||
363 | { | ||
364 | $code = ''; | ||
365 | foreach ($value->properties as $class => $properties) { | ||
366 | $code .= $subIndent.' '.self::export($class).' => '.self::export($properties, $subIndent.' ').",\n"; | ||
367 | } | ||
368 | |||
369 | $code = [ | ||
370 | self::export($value->registry, $subIndent), | ||
371 | self::export($value->values, $subIndent), | ||
372 | '' !== $code ? "[\n".$code.$subIndent.']' : '[]', | ||
373 | self::export($value->value, $subIndent), | ||
374 | self::export($value->wakeups, $subIndent), | ||
375 | ]; | ||
376 | |||
377 | return '\\'.$value::class."::hydrate(\n".$subIndent.implode(",\n".$subIndent, $code)."\n".$indent.')'; | ||
378 | } | ||
379 | |||
380 | /** | ||
381 | * @param \ArrayIterator|\ArrayObject $value | ||
382 | * @param \ArrayIterator|\ArrayObject $proto | ||
383 | */ | ||
384 | private static function getArrayObjectProperties($value, $proto): array | ||
385 | { | ||
386 | $reflector = $value instanceof \ArrayIterator ? 'ArrayIterator' : 'ArrayObject'; | ||
387 | $reflector = Registry::$reflectors[$reflector] ??= Registry::getClassReflector($reflector); | ||
388 | |||
389 | $properties = [ | ||
390 | $arrayValue = (array) $value, | ||
391 | $reflector->getMethod('getFlags')->invoke($value), | ||
392 | $value instanceof \ArrayObject ? $reflector->getMethod('getIteratorClass')->invoke($value) : 'ArrayIterator', | ||
393 | ]; | ||
394 | |||
395 | $reflector = $reflector->getMethod('setFlags'); | ||
396 | $reflector->invoke($proto, \ArrayObject::STD_PROP_LIST); | ||
397 | |||
398 | if ($properties[1] & \ArrayObject::STD_PROP_LIST) { | ||
399 | $reflector->invoke($value, 0); | ||
400 | $properties[0] = (array) $value; | ||
401 | } else { | ||
402 | $reflector->invoke($value, \ArrayObject::STD_PROP_LIST); | ||
403 | $arrayValue = (array) $value; | ||
404 | } | ||
405 | $reflector->invoke($value, $properties[1]); | ||
406 | |||
407 | if ([[], 0, 'ArrayIterator'] === $properties) { | ||
408 | $properties = []; | ||
409 | } else { | ||
410 | if ('ArrayIterator' === $properties[2]) { | ||
411 | unset($properties[2]); | ||
412 | } | ||
413 | $properties = [$reflector->class => ["\0" => $properties]]; | ||
414 | } | ||
415 | |||
416 | return [$arrayValue, $properties]; | ||
417 | } | ||
418 | } | ||