LazyGhostTrait.php 14 KB

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