Hydrator.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarExporter\Internal;
  11. use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. *
  15. * @internal
  16. */
  17. class Hydrator
  18. {
  19. public const PROPERTY_HAS_HOOKS = 1;
  20. public const PROPERTY_NOT_BY_REF = 2;
  21. public static array $hydrators = [];
  22. public static array $simpleHydrators = [];
  23. public static array $propertyScopes = [];
  24. public function __construct(
  25. public readonly Registry $registry,
  26. public readonly ?Values $values,
  27. public readonly array $properties,
  28. public readonly mixed $value,
  29. public readonly array $wakeups,
  30. ) {
  31. }
  32. public static function hydrate($objects, $values, $properties, $value, $wakeups)
  33. {
  34. foreach ($properties as $class => $vars) {
  35. (self::$hydrators[$class] ??= self::getHydrator($class))($vars, $objects);
  36. }
  37. foreach ($wakeups as $k => $v) {
  38. if (\is_array($v)) {
  39. $objects[-$k]->__unserialize($v);
  40. } else {
  41. $objects[$v]->__wakeup();
  42. }
  43. }
  44. return $value;
  45. }
  46. public static function getHydrator($class)
  47. {
  48. $baseHydrator = self::$hydrators['stdClass'] ??= static function ($properties, $objects) {
  49. foreach ($properties as $name => $values) {
  50. foreach ($values as $i => $v) {
  51. $objects[$i]->$name = $v;
  52. }
  53. }
  54. };
  55. switch ($class) {
  56. case 'stdClass':
  57. return $baseHydrator;
  58. case 'ErrorException':
  59. return $baseHydrator->bindTo(null, new class extends \ErrorException {
  60. });
  61. case 'TypeError':
  62. return $baseHydrator->bindTo(null, new class extends \Error {
  63. });
  64. case 'SplObjectStorage':
  65. return static function ($properties, $objects) {
  66. foreach ($properties as $name => $values) {
  67. if ("\0" === $name) {
  68. foreach ($values as $i => $v) {
  69. for ($j = 0; $j < \count($v); ++$j) {
  70. $objects[$i][$v[$j]] = $v[++$j];
  71. }
  72. }
  73. continue;
  74. }
  75. foreach ($values as $i => $v) {
  76. $objects[$i]->$name = $v;
  77. }
  78. }
  79. };
  80. }
  81. if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
  82. throw new ClassNotFoundException($class);
  83. }
  84. $classReflector = new \ReflectionClass($class);
  85. switch ($class) {
  86. case 'ArrayIterator':
  87. case 'ArrayObject':
  88. $constructor = $classReflector->getConstructor()->invokeArgs(...);
  89. return static function ($properties, $objects) use ($constructor) {
  90. foreach ($properties as $name => $values) {
  91. if ("\0" !== $name) {
  92. foreach ($values as $i => $v) {
  93. $objects[$i]->$name = $v;
  94. }
  95. }
  96. }
  97. foreach ($properties["\0"] ?? [] as $i => $v) {
  98. $constructor($objects[$i], $v);
  99. }
  100. };
  101. }
  102. if (!$classReflector->isInternal()) {
  103. return $baseHydrator->bindTo(null, $class);
  104. }
  105. if ($classReflector->name !== $class) {
  106. return self::$hydrators[$classReflector->name] ??= self::getHydrator($classReflector->name);
  107. }
  108. $propertySetters = [];
  109. foreach ($classReflector->getProperties() as $propertyReflector) {
  110. if (!$propertyReflector->isStatic()) {
  111. $propertySetters[$propertyReflector->name] = $propertyReflector->setValue(...);
  112. }
  113. }
  114. if (!$propertySetters) {
  115. return $baseHydrator;
  116. }
  117. return static function ($properties, $objects) use ($propertySetters) {
  118. foreach ($properties as $name => $values) {
  119. if ($setValue = $propertySetters[$name] ?? null) {
  120. foreach ($values as $i => $v) {
  121. $setValue($objects[$i], $v);
  122. }
  123. continue;
  124. }
  125. foreach ($values as $i => $v) {
  126. $objects[$i]->$name = $v;
  127. }
  128. }
  129. };
  130. }
  131. public static function getSimpleHydrator($class)
  132. {
  133. $baseHydrator = self::$simpleHydrators['stdClass'] ??= (function ($properties, $object) {
  134. $notByRef = (array) $this;
  135. foreach ($properties as $name => &$value) {
  136. if (!$noRef = $notByRef[$name] ?? false) {
  137. $object->$name = $value;
  138. $object->$name = &$value;
  139. } elseif (true !== $noRef) {
  140. $noRef($object, $value);
  141. } else {
  142. $object->$name = $value;
  143. }
  144. }
  145. })->bindTo(new \stdClass());
  146. switch ($class) {
  147. case 'stdClass':
  148. return $baseHydrator;
  149. case 'ErrorException':
  150. return $baseHydrator->bindTo(new \stdClass(), new class extends \ErrorException {
  151. });
  152. case 'TypeError':
  153. return $baseHydrator->bindTo(new \stdClass(), new class extends \Error {
  154. });
  155. case 'SplObjectStorage':
  156. return static function ($properties, $object) {
  157. foreach ($properties as $name => &$value) {
  158. if ("\0" !== $name) {
  159. $object->$name = $value;
  160. $object->$name = &$value;
  161. continue;
  162. }
  163. for ($i = 0; $i < \count($value); ++$i) {
  164. $object[$value[$i]] = $value[++$i];
  165. }
  166. }
  167. };
  168. }
  169. if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) {
  170. throw new ClassNotFoundException($class);
  171. }
  172. $classReflector = new \ReflectionClass($class);
  173. switch ($class) {
  174. case 'ArrayIterator':
  175. case 'ArrayObject':
  176. $constructor = $classReflector->getConstructor()->invokeArgs(...);
  177. return static function ($properties, $object) use ($constructor) {
  178. foreach ($properties as $name => &$value) {
  179. if ("\0" === $name) {
  180. $constructor($object, $value);
  181. } else {
  182. $object->$name = $value;
  183. $object->$name = &$value;
  184. }
  185. }
  186. };
  187. }
  188. if (!$classReflector->isInternal()) {
  189. $notByRef = new \stdClass();
  190. foreach ($classReflector->getProperties() as $propertyReflector) {
  191. if ($propertyReflector->isStatic()) {
  192. continue;
  193. }
  194. if (\PHP_VERSION_ID >= 80400 && !$propertyReflector->isAbstract() && $propertyReflector->getHooks()) {
  195. $notByRef->{$propertyReflector->name} = $propertyReflector->setRawValue(...);
  196. } elseif ($propertyReflector->isReadOnly()) {
  197. $notByRef->{$propertyReflector->name} = static function ($object, $value) use ($propertyReflector) {
  198. if (!$propertyReflector->isInitialized($object)) {
  199. $propertyReflector->setValue($object, $value);
  200. }
  201. };
  202. }
  203. }
  204. return $baseHydrator->bindTo($notByRef, $class);
  205. }
  206. if ($classReflector->name !== $class) {
  207. return self::$simpleHydrators[$classReflector->name] ??= self::getSimpleHydrator($classReflector->name);
  208. }
  209. $propertySetters = [];
  210. foreach ($classReflector->getProperties() as $propertyReflector) {
  211. if (!$propertyReflector->isStatic()) {
  212. $propertySetters[$propertyReflector->name] = $propertyReflector->setValue(...);
  213. }
  214. }
  215. if (!$propertySetters) {
  216. return $baseHydrator;
  217. }
  218. return static function ($properties, $object) use ($propertySetters) {
  219. foreach ($properties as $name => &$value) {
  220. if ($setValue = $propertySetters[$name] ?? null) {
  221. $setValue($object, $value);
  222. } else {
  223. $object->$name = $value;
  224. $object->$name = &$value;
  225. }
  226. }
  227. };
  228. }
  229. public static function getPropertyScopes($class): array
  230. {
  231. $propertyScopes = [];
  232. $r = new \ReflectionClass($class);
  233. foreach ($r->getProperties() as $property) {
  234. $flags = $property->getModifiers();
  235. if (\ReflectionProperty::IS_STATIC & $flags) {
  236. continue;
  237. }
  238. $name = $property->name;
  239. $access = ($flags << 2) | ($flags & \ReflectionProperty::IS_READONLY ? self::PROPERTY_NOT_BY_REF : 0);
  240. if (\PHP_VERSION_ID >= 80400 && !$property->isAbstract() && $h = $property->getHooks()) {
  241. $access |= self::PROPERTY_HAS_HOOKS | (isset($h['get']) && !$h['get']->returnsReference() ? self::PROPERTY_NOT_BY_REF : 0);
  242. }
  243. if (\ReflectionProperty::IS_PRIVATE & $flags) {
  244. $propertyScopes["\0$class\0$name"] = $propertyScopes[$name] = [$class, $name, null, $access, $property];
  245. continue;
  246. }
  247. $propertyScopes[$name] = [$class, $name, null, $access, $property];
  248. if ($flags & (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY)) {
  249. $propertyScopes[$name][2] = $property->class;
  250. }
  251. if (\ReflectionProperty::IS_PROTECTED & $flags) {
  252. $propertyScopes["\0*\0$name"] = $propertyScopes[$name];
  253. }
  254. }
  255. while ($r = $r->getParentClass()) {
  256. $class = $r->name;
  257. foreach ($r->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
  258. $flags = $property->getModifiers();
  259. if (\ReflectionProperty::IS_STATIC & $flags) {
  260. continue;
  261. }
  262. $name = $property->name;
  263. $access = ($flags << 2) | ($flags & \ReflectionProperty::IS_READONLY ? self::PROPERTY_NOT_BY_REF : 0);
  264. if (\PHP_VERSION_ID >= 80400 && $h = $property->getHooks()) {
  265. $access |= self::PROPERTY_HAS_HOOKS | (isset($h['get']) && !$h['get']->returnsReference() ? self::PROPERTY_NOT_BY_REF : 0);
  266. }
  267. $propertyScopes["\0$class\0$name"] = [$class, $name, null, $access, $property];
  268. $propertyScopes[$name] ??= $propertyScopes["\0$class\0$name"];
  269. }
  270. }
  271. return $propertyScopes;
  272. }
  273. }