Hydrator.php 11 KB

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