vendor/symfony/var-exporter/LazyGhostTrait.php line 283

Open in your IDE?
  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|array $initializer, ?array $skippedProperties = null, ?object $instance = null): static
  31. {
  32. if (\is_array($initializer)) {
  33. trigger_deprecation('symfony/var-exporter', '6.4', 'Per-property lazy-initializers are deprecated and won\'t be supported anymore in 7.0, use an object initializer instead.');
  34. }
  35. $onlyProperties = null === $skippedProperties && \is_array($initializer) ? $initializer : null;
  36. if (self::class !== $class = $instance ? $instance::class : static::class) {
  37. $skippedProperties["\0".self::class."\0lazyObjectState"] = true;
  38. } elseif (\defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
  39. Hydrator::$propertyScopes[$class] ??= $class::LAZY_OBJECT_PROPERTY_SCOPES;
  40. }
  41. $instance ??= (Registry::$classReflectors[$class] ??= new \ReflectionClass($class))->newInstanceWithoutConstructor();
  42. Registry::$defaultProperties[$class] ??= (array) $instance;
  43. $instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);
  44. foreach (Registry::$classResetters[$class] ??= Registry::getClassResetters($class) as $reset) {
  45. $reset($instance, $skippedProperties, $onlyProperties);
  46. }
  47. return $instance;
  48. }
  49. /**
  50. * Returns whether the object is initialized.
  51. *
  52. * @param $partial Whether partially initialized objects should be considered as initialized
  53. */
  54. #[Ignore]
  55. public function isLazyObjectInitialized(bool $partial = false): bool
  56. {
  57. if (!$state = $this->lazyObjectState ?? null) {
  58. return true;
  59. }
  60. if (!\is_array($state->initializer)) {
  61. return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
  62. }
  63. $class = $this::class;
  64. $properties = (array) $this;
  65. if ($partial) {
  66. return (bool) array_intersect_key($state->initializer, $properties);
  67. }
  68. $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
  69. foreach ($state->initializer as $key => $initializer) {
  70. if (!\array_key_exists($key, $properties) && isset($propertyScopes[$key])) {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. /**
  77. * Forces initialization of a lazy object and returns it.
  78. */
  79. public function initializeLazyObject(): static
  80. {
  81. if (!$state = $this->lazyObjectState ?? null) {
  82. return $this;
  83. }
  84. if (!\is_array($state->initializer)) {
  85. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
  86. $state->initialize($this, '', null);
  87. }
  88. return $this;
  89. }
  90. $values = isset($state->initializer["\0"]) ? null : [];
  91. $class = $this::class;
  92. $properties = (array) $this;
  93. $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
  94. foreach ($state->initializer as $key => $initializer) {
  95. if (\array_key_exists($key, $properties) || ![$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
  96. continue;
  97. }
  98. $scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
  99. if (null === $values) {
  100. if (!\is_array($values = ($state->initializer["\0"])($this, Registry::$defaultProperties[$class]))) {
  101. throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
  102. }
  103. if (\array_key_exists($key, $properties = (array) $this)) {
  104. continue;
  105. }
  106. }
  107. if (\array_key_exists($key, $values)) {
  108. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  109. $accessor['set']($this, $name, $properties[$key] = $values[$key]);
  110. } else {
  111. $state->initialize($this, $name, $scope);
  112. $properties = (array) $this;
  113. }
  114. }
  115. return $this;
  116. }
  117. /**
  118. * @return bool Returns false when the object cannot be reset, ie when it's not a lazy object
  119. */
  120. public function resetLazyObject(): bool
  121. {
  122. if (!$state = $this->lazyObjectState ?? null) {
  123. return false;
  124. }
  125. if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $state->status) {
  126. $state->reset($this);
  127. }
  128. return true;
  129. }
  130. public function &__get($name): mixed
  131. {
  132. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  133. $scope = null;
  134. if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
  135. $scope = Registry::getScope($propertyScopes, $class, $name);
  136. $state = $this->lazyObjectState ?? null;
  137. if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
  138. if (LazyObjectState::STATUS_INITIALIZED_FULL === $state->status) {
  139. // Work around php/php-src#12695
  140. $property = null === $scope ? $name : "\0$scope\0$name";
  141. $property = $propertyScopes[$property][3]
  142. ?? Hydrator::$propertyScopes[$this::class][$property][3] = new \ReflectionProperty($scope ?? $class, $name);
  143. } else {
  144. $property = null;
  145. }
  146. if ($property?->isInitialized($this) ?? LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)) {
  147. goto get_in_scope;
  148. }
  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. if (preg_match('/^Cannot access uninitialized non-nullable property ([^ ]++) by reference$/', $e->getMessage(), $matches)) {
  186. throw new \Error('Typed property '.$matches[1].' must not be accessed before initialization', $e->getCode(), $e->getPrevious());
  187. }
  188. throw $e;
  189. }
  190. }
  191. }
  192. public function __set($name, $value): void
  193. {
  194. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  195. $scope = null;
  196. if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
  197. $scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
  198. $state = $this->lazyObjectState ?? null;
  199. if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
  200. && LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
  201. ) {
  202. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
  203. $state->initialize($this, $name, $readonlyScope ?? $scope);
  204. }
  205. goto set_in_scope;
  206. }
  207. }
  208. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['set']) {
  209. parent::__set($name, $value);
  210. return;
  211. }
  212. set_in_scope:
  213. if (null === $scope) {
  214. $this->$name = $value;
  215. } else {
  216. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  217. $accessor['set']($this, $name, $value);
  218. }
  219. }
  220. public function __isset($name): bool
  221. {
  222. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  223. $scope = null;
  224. if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
  225. $scope = Registry::getScope($propertyScopes, $class, $name);
  226. $state = $this->lazyObjectState ?? null;
  227. if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
  228. && LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
  229. && LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
  230. ) {
  231. goto isset_in_scope;
  232. }
  233. }
  234. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['isset']) {
  235. return parent::__isset($name);
  236. }
  237. isset_in_scope:
  238. if (null === $scope) {
  239. return isset($this->$name);
  240. }
  241. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  242. return $accessor['isset']($this, $name);
  243. }
  244. public function __unset($name): void
  245. {
  246. $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
  247. $scope = null;
  248. if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
  249. $scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
  250. $state = $this->lazyObjectState ?? null;
  251. if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))
  252. && LazyObjectState::STATUS_INITIALIZED_FULL !== $state->status
  253. ) {
  254. if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
  255. $state->initialize($this, $name, $readonlyScope ?? $scope);
  256. }
  257. goto unset_in_scope;
  258. }
  259. }
  260. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['unset']) {
  261. parent::__unset($name);
  262. return;
  263. }
  264. unset_in_scope:
  265. if (null === $scope) {
  266. unset($this->$name);
  267. } else {
  268. $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
  269. $accessor['unset']($this, $name);
  270. }
  271. }
  272. public function __clone(): void
  273. {
  274. if ($state = $this->lazyObjectState ?? null) {
  275. $this->lazyObjectState = clone $state;
  276. }
  277. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['clone']) {
  278. parent::__clone();
  279. }
  280. }
  281. public function __serialize(): array
  282. {
  283. $class = self::class;
  284. if ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['serialize']) {
  285. $properties = parent::__serialize();
  286. } else {
  287. $this->initializeLazyObject();
  288. $properties = (array) $this;
  289. }
  290. unset($properties["\0$class\0lazyObjectState"]);
  291. if (Registry::$parentMethods[$class]['serialize'] || !Registry::$parentMethods[$class]['sleep']) {
  292. return $properties;
  293. }
  294. $scope = get_parent_class($class);
  295. $data = [];
  296. foreach (parent::__sleep() as $name) {
  297. $value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$class\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
  298. if (null === $k) {
  299. trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $name), \E_USER_NOTICE);
  300. } else {
  301. $data[$k] = $value;
  302. }
  303. }
  304. return $data;
  305. }
  306. public function __destruct()
  307. {
  308. $state = $this->lazyObjectState ?? null;
  309. if ($state && \in_array($state->status, [LazyObjectState::STATUS_UNINITIALIZED_FULL, LazyObjectState::STATUS_UNINITIALIZED_PARTIAL], true)) {
  310. return;
  311. }
  312. if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['destruct']) {
  313. parent::__destruct();
  314. }
  315. }
  316. #[Ignore]
  317. private function setLazyObjectAsInitialized(bool $initialized): void
  318. {
  319. $state = $this->lazyObjectState ?? null;
  320. if ($state && !\is_array($state->initializer)) {
  321. $state->status = $initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL;
  322. }
  323. }
  324. }