LazyGhostTrait.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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;
  11. use Symfony\Component\Serializer\Attribute\Ignore;
  12. use Symfony\Component\VarExporter\Internal\Hydrator;
  13. use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry;
  14. use Symfony\Component\VarExporter\Internal\LazyObjectState;
  15. use Symfony\Component\VarExporter\Internal\LazyObjectTrait;
  16. trait LazyGhostTrait
  17. {
  18. use LazyObjectTrait;
  19. /**
  20. * Creates a lazy-loading ghost instance.
  21. *
  22. * Skipped properties should be indexed by their array-cast identifier, see
  23. * https://php.net/manual/language.types.array#language.types.array.casting
  24. *
  25. * @param \Closure(static):void $initializer The closure should initialize the object it receives as argument
  26. * @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, a.k.a. the ones
  27. * that the initializer doesn't initialize, if any
  28. * @param static|null $instance
  29. */
  30. public static function createLazyGhost(\Closure $initializer, ?array $skippedProperties = null, ?object $instance = null): static
  31. {
  32. if (self::class !== $class = $instance ? $instance::class : static::class) {
  33. $skippedProperties["\0".self::class."\0lazyObjectState"] = true;
  34. }
  35. if (!isset(Registry::$defaultProperties[$class])) {
  36. Registry::$classReflectors[$class] ??= new \ReflectionClass($class);
  37. $instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
  38. Registry::$defaultProperties[$class] ??= (array) $instance;
  39. Registry::$classResetters[$class] ??= Registry::getClassResetters($class);
  40. if (self::class === $class && \defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
  41. Hydrator::$propertyScopes[$class] ??= $class::LAZY_OBJECT_PROPERTY_SCOPES;
  42. }
  43. } else {
  44. $instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
  45. }
  46. if (isset($instance->lazyObjectState)) {
  47. $instance->lazyObjectState->initializer = $initializer;
  48. $instance->lazyObjectState->skippedProperties = $skippedProperties ??= [];
  49. if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $instance->lazyObjectState->status) {
  50. $instance->lazyObjectState->reset($instance);
  51. }
  52. return $instance;
  53. }
  54. $instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);
  55. foreach (Registry::$classResetters[$class] as $reset) {
  56. $reset($instance, $skippedProperties);
  57. }
  58. return $instance;
  59. }
  60. /**
  61. * Returns whether the object is initialized.
  62. *
  63. * @param bool $partial Whether partially initialized objects should be considered as initialized
  64. */
  65. #[Ignore]
  66. public function isLazyObjectInitialized(bool $partial = false): bool
  67. {
  68. if (!$state = $this->lazyObjectState ?? null) {
  69. return true;
  70. }
  71. return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
  72. }
  73. /**
  74. * Forces initialization of a lazy object and returns it.
  75. */
  76. public function initializeLazyObject(): static
  77. {
  78. if (!$state = $this->lazyObjectState ?? null) {
  79. return $this;
  80. }
  81. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
  82. $state->initialize($this, '', null);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * @return bool Returns false when the object cannot be reset, ie when it's not a lazy object
  88. */
  89. public function resetLazyObject(): bool
  90. {
  91. if (!$state = $this->lazyObjectState ?? null) {
  92. return false;
  93. }
  94. if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $state->status) {
  95. $state->reset($this);
  96. }
  97. return true;
  98. }
  99. public function &__get($name): mixed
  100. {
  101. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  102. $scope = null;
  103. $notByRef = 0;
  104. if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
  105. $scope = Registry::getScopeForRead($propertyScopes, $class, $name);
  106. $state = $this->lazyObjectState ?? null;
  107. if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
  108. $notByRef = $access & Hydrator::PROPERTY_NOT_BY_REF;
  109. if (LazyObjectState::STATUS_INITIALIZED_FULL === $state->status) {
  110. // Work around php/php-src#12695
  111. $property = null === $scope ? $name : "\0$scope\0$name";
  112. $property = $propertyScopes[$property][4]
  113. ?? Hydrator::$propertyScopes[$this::class][$property][4] = new \ReflectionProperty($scope ?? $class, $name);
  114. } else {
  115. $property = null;
  116. }
  117. if (\PHP_VERSION_ID >= 80400 && !$notByRef && ($access >> 2) & \ReflectionProperty::IS_PRIVATE_SET) {
  118. $scope ??= $writeScope;
  119. }
  120. if ($property?->isInitialized($this) ?? LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $writeScope ?? $scope)) {
  121. goto get_in_scope;
  122. }
  123. }
  124. }
  125. if ($parent = (Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['get']) {
  126. if (2 === $parent) {
  127. return parent::__get($name);
  128. }
  129. $value = parent::__get($name);
  130. return $value;
  131. }
  132. if (null === $class) {
  133. $frame = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
  134. trigger_error(\sprintf('Undefined property: %s::$%s in %s on line %s', $this::class, $name, $frame['file'], $frame['line']), \E_USER_NOTICE);
  135. }
  136. get_in_scope:
  137. try {
  138. if (null === $scope) {
  139. if (!$notByRef) {
  140. return $this->$name;
  141. }
  142. $value = $this->$name;
  143. return $value;
  144. }
  145. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  146. return $accessor['get']($this, $name, $notByRef);
  147. } catch (\Error $e) {
  148. if (\Error::class !== $e::class || !str_starts_with($e->getMessage(), 'Cannot access uninitialized non-nullable property')) {
  149. throw $e;
  150. }
  151. try {
  152. if (null === $scope) {
  153. $this->$name = [];
  154. return $this->$name;
  155. }
  156. $accessor['set']($this, $name, []);
  157. return $accessor['get']($this, $name, $notByRef);
  158. } catch (\Error) {
  159. if (preg_match('/^Cannot access uninitialized non-nullable property ([^ ]++) by reference$/', $e->getMessage(), $matches)) {
  160. throw new \Error('Typed property '.$matches[1].' must not be accessed before initialization', $e->getCode(), $e->getPrevious());
  161. }
  162. throw $e;
  163. }
  164. }
  165. }
  166. public function __set($name, $value): void
  167. {
  168. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  169. $scope = null;
  170. if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
  171. $scope = Registry::getScopeForWrite($propertyScopes, $class, $name, $access >> 2);
  172. $state = $this->lazyObjectState ?? null;
  173. if ($state && ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
  174. && LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
  175. ) {
  176. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
  177. $state->initialize($this, $name, $writeScope ?? $scope);
  178. }
  179. goto set_in_scope;
  180. }
  181. }
  182. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['set']) {
  183. parent::__set($name, $value);
  184. return;
  185. }
  186. set_in_scope:
  187. if (null === $scope) {
  188. $this->$name = $value;
  189. } else {
  190. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  191. $accessor['set']($this, $name, $value);
  192. }
  193. }
  194. public function __isset($name): bool
  195. {
  196. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  197. $scope = null;
  198. if ([$class, , $writeScope] = $propertyScopes[$name] ?? null) {
  199. $scope = Registry::getScopeForRead($propertyScopes, $class, $name);
  200. $state = $this->lazyObjectState ?? null;
  201. if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
  202. && LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
  203. && LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $writeScope ?? $scope)
  204. ) {
  205. goto isset_in_scope;
  206. }
  207. }
  208. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['isset']) {
  209. return parent::__isset($name);
  210. }
  211. isset_in_scope:
  212. if (null === $scope) {
  213. return isset($this->$name);
  214. }
  215. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  216. return $accessor['isset']($this, $name);
  217. }
  218. public function __unset($name): void
  219. {
  220. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  221. $scope = null;
  222. if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
  223. $scope = Registry::getScopeForWrite($propertyScopes, $class, $name, $access >> 2);
  224. $state = $this->lazyObjectState ?? null;
  225. if ($state && ($writeScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
  226. && LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
  227. ) {
  228. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
  229. $state->initialize($this, $name, $writeScope ?? $scope);
  230. }
  231. goto unset_in_scope;
  232. }
  233. }
  234. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['unset']) {
  235. parent::__unset($name);
  236. return;
  237. }
  238. unset_in_scope:
  239. if (null === $scope) {
  240. unset($this->$name);
  241. } else {
  242. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  243. $accessor['unset']($this, $name);
  244. }
  245. }
  246. public function __clone(): void
  247. {
  248. if ($state = $this->lazyObjectState ?? null) {
  249. $this->lazyObjectState = clone $state;
  250. }
  251. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['clone']) {
  252. parent::__clone();
  253. }
  254. }
  255. public function __serialize(): array
  256. {
  257. $class = self::class;
  258. if ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['serialize']) {
  259. $properties = parent::__serialize();
  260. } else {
  261. $this->initializeLazyObject();
  262. $properties = (array) $this;
  263. }
  264. unset($properties["\0$class\0lazyObjectState"]);
  265. if (Registry::$parentMethods[$class]['serialize'] || !Registry::$parentMethods[$class]['sleep']) {
  266. return $properties;
  267. }
  268. $scope = get_parent_class($class);
  269. $data = [];
  270. foreach (parent::__sleep() as $name) {
  271. $value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$class\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
  272. if (null === $k) {
  273. trigger_error(\sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $name), \E_USER_NOTICE);
  274. } else {
  275. $data[$k] = $value;
  276. }
  277. }
  278. return $data;
  279. }
  280. public function __destruct()
  281. {
  282. $state = $this->lazyObjectState ?? null;
  283. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state?->status) {
  284. return;
  285. }
  286. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['destruct']) {
  287. parent::__destruct();
  288. }
  289. }
  290. #[Ignore]
  291. private function setLazyObjectAsInitialized(bool $initialized): void
  292. {
  293. if ($state = $this->lazyObjectState ?? null) {
  294. $state->status = $initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL;
  295. }
  296. }
  297. }