Hydrator.php 10 KB

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