LazyProxyTrait.php 13 KB

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