summaryrefslogtreecommitdiff
path: root/vendor/symfony/var-exporter/Internal/Hydrator.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/var-exporter/Internal/Hydrator.php
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/var-exporter/Internal/Hydrator.php')
-rw-r--r--vendor/symfony/var-exporter/Internal/Hydrator.php299
1 files changed, 299 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/Internal/Hydrator.php b/vendor/symfony/var-exporter/Internal/Hydrator.php
new file mode 100644
index 0000000..65fdcd1
--- /dev/null
+++ b/vendor/symfony/var-exporter/Internal/Hydrator.php
@@ -0,0 +1,299 @@
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\Internal;
13
14use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
15
16/**
17 * @author Nicolas Grekas <p@tchwork.com>
18 *
19 * @internal
20 */
21class Hydrator
22{
23 public static array $hydrators = [];
24 public static array $simpleHydrators = [];
25 public static array $propertyScopes = [];
26
27 public function __construct(
28 public readonly Registry $registry,
29 public readonly ?Values $values,
30 public readonly array $properties,
31 public readonly mixed $value,
32 public readonly array $wakeups,
33 ) {
34 }
35
36 public static function hydrate($objects, $values, $properties, $value, $wakeups)
37 {
38 foreach ($properties as $class => $vars) {
39 (self::$hydrators[$class] ??= self::getHydrator($class))($vars, $objects);
40 }
41 foreach ($wakeups as $k => $v) {
42 if (\is_array($v)) {
43 $objects[-$k]->__unserialize($v);
44 } else {
45 $objects[$v]->__wakeup();
46 }
47 }
48
49 return $value;
50 }
51
52 public static function getHydrator($class)
53 {
54 $baseHydrator = self::$hydrators['stdClass'] ??= static function ($properties, $objects) {
55 foreach ($properties as $name => $values) {
56 foreach ($values as $i => $v) {
57 $objects[$i]->$name = $v;
58 }
59 }
60 };
61
62 switch ($class) {
63 case 'stdClass':
64 return $baseHydrator;
65
66 case 'ErrorException':
67 return $baseHydrator->bindTo(null, new class() extends \ErrorException {
68 });
69
70 case 'TypeError':
71 return $baseHydrator->bindTo(null, new class() extends \Error {
72 });
73
74 case 'SplObjectStorage':
75 return static function ($properties, $objects) {
76 foreach ($properties as $name => $values) {
77 if ("\0" === $name) {
78 foreach ($values as $i => $v) {
79 for ($j = 0; $j < \count($v); ++$j) {
80 $objects[$i]->attach($v[$j], $v[++$j]);
81 }
82 }
83 continue;
84 }
85 foreach ($values as $i => $v) {
86 $objects[$i]->$name = $v;
87 }
88 }
89 };
90 }
91
92 if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
93 throw new ClassNotFoundException($class);
94 }
95 $classReflector = new \ReflectionClass($class);
96
97 switch ($class) {
98 case 'ArrayIterator':
99 case 'ArrayObject':
100 $constructor = $classReflector->getConstructor()->invokeArgs(...);
101
102 return static function ($properties, $objects) use ($constructor) {
103 foreach ($properties as $name => $values) {
104 if ("\0" !== $name) {
105 foreach ($values as $i => $v) {
106 $objects[$i]->$name = $v;
107 }
108 }
109 }
110 foreach ($properties["\0"] ?? [] as $i => $v) {
111 $constructor($objects[$i], $v);
112 }
113 };
114 }
115
116 if (!$classReflector->isInternal()) {
117 return $baseHydrator->bindTo(null, $class);
118 }
119
120 if ($classReflector->name !== $class) {
121 return self::$hydrators[$classReflector->name] ??= self::getHydrator($classReflector->name);
122 }
123
124 $propertySetters = [];
125 foreach ($classReflector->getProperties() as $propertyReflector) {
126 if (!$propertyReflector->isStatic()) {
127 $propertySetters[$propertyReflector->name] = $propertyReflector->setValue(...);
128 }
129 }
130
131 if (!$propertySetters) {
132 return $baseHydrator;
133 }
134
135 return static function ($properties, $objects) use ($propertySetters) {
136 foreach ($properties as $name => $values) {
137 if ($setValue = $propertySetters[$name] ?? null) {
138 foreach ($values as $i => $v) {
139 $setValue($objects[$i], $v);
140 }
141 continue;
142 }
143 foreach ($values as $i => $v) {
144 $objects[$i]->$name = $v;
145 }
146 }
147 };
148 }
149
150 public static function getSimpleHydrator($class)
151 {
152 $baseHydrator = self::$simpleHydrators['stdClass'] ??= (function ($properties, $object) {
153 $readonly = (array) $this;
154
155 foreach ($properties as $name => &$value) {
156 $object->$name = $value;
157
158 if (!($readonly[$name] ?? false)) {
159 $object->$name = &$value;
160 }
161 }
162 })->bindTo(new \stdClass());
163
164 switch ($class) {
165 case 'stdClass':
166 return $baseHydrator;
167
168 case 'ErrorException':
169 return $baseHydrator->bindTo(new \stdClass(), new class() extends \ErrorException {
170 });
171
172 case 'TypeError':
173 return $baseHydrator->bindTo(new \stdClass(), new class() extends \Error {
174 });
175
176 case 'SplObjectStorage':
177 return static function ($properties, $object) {
178 foreach ($properties as $name => &$value) {
179 if ("\0" !== $name) {
180 $object->$name = $value;
181 $object->$name = &$value;
182 continue;
183 }
184 for ($i = 0; $i < \count($value); ++$i) {
185 $object->attach($value[$i], $value[++$i]);
186 }
187 }
188 };
189 }
190
191 if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
192 throw new ClassNotFoundException($class);
193 }
194 $classReflector = new \ReflectionClass($class);
195
196 switch ($class) {
197 case 'ArrayIterator':
198 case 'ArrayObject':
199 $constructor = $classReflector->getConstructor()->invokeArgs(...);
200
201 return static function ($properties, $object) use ($constructor) {
202 foreach ($properties as $name => &$value) {
203 if ("\0" === $name) {
204 $constructor($object, $value);
205 } else {
206 $object->$name = $value;
207 $object->$name = &$value;
208 }
209 }
210 };
211 }
212
213 if (!$classReflector->isInternal()) {
214 $readonly = new \stdClass();
215 foreach ($classReflector->getProperties(\ReflectionProperty::IS_READONLY) as $propertyReflector) {
216 if ($class === $propertyReflector->class) {
217 $readonly->{$propertyReflector->name} = true;
218 }
219 }
220
221 return $baseHydrator->bindTo($readonly, $class);
222 }
223
224 if ($classReflector->name !== $class) {
225 return self::$simpleHydrators[$classReflector->name] ??= self::getSimpleHydrator($classReflector->name);
226 }
227
228 $propertySetters = [];
229 foreach ($classReflector->getProperties() as $propertyReflector) {
230 if (!$propertyReflector->isStatic()) {
231 $propertySetters[$propertyReflector->name] = $propertyReflector->setValue(...);
232 }
233 }
234
235 if (!$propertySetters) {
236 return $baseHydrator;
237 }
238
239 return static function ($properties, $object) use ($propertySetters) {
240 foreach ($properties as $name => &$value) {
241 if ($setValue = $propertySetters[$name] ?? null) {
242 $setValue($object, $value);
243 } else {
244 $object->$name = $value;
245 $object->$name = &$value;
246 }
247 }
248 };
249 }
250
251 public static function getPropertyScopes($class): array
252 {
253 $propertyScopes = [];
254 $r = new \ReflectionClass($class);
255
256 foreach ($r->getProperties() as $property) {
257 $flags = $property->getModifiers();
258
259 if (\ReflectionProperty::IS_STATIC & $flags) {
260 continue;
261 }
262 $name = $property->name;
263
264 if (\ReflectionProperty::IS_PRIVATE & $flags) {
265 $readonlyScope = null;
266 if ($flags & \ReflectionProperty::IS_READONLY) {
267 $readonlyScope = $class;
268 }
269 $propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, $readonlyScope, $property];
270
271 continue;
272 }
273 $readonlyScope = null;
274 if ($flags & \ReflectionProperty::IS_READONLY) {
275 $readonlyScope = $property->class;
276 }
277 $propertyScopes[$name] = [$class, $name, $readonlyScope, $property];
278
279 if (\ReflectionProperty::IS_PROTECTED & $flags) {
280 $propertyScopes["\0*\0$name"] = $propertyScopes[$name];
281 }
282 }
283
284 while ($r = $r->getParentClass()) {
285 $class = $r->name;
286
287 foreach ($r->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
288 if (!$property->isStatic()) {
289 $name = $property->name;
290 $readonlyScope = $property->isReadOnly() ? $class : null;
291 $propertyScopes["\0$class\0$name"] = [$class, $name, $readonlyScope, $property];
292 $propertyScopes[$name] ??= [$class, $name, $readonlyScope, $property];
293 }
294 }
295 }
296
297 return $propertyScopes;
298 }
299}