123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\VarExporter;
- use Symfony\Component\VarExporter\Internal\Hydrator;
- use Symfony\Component\VarExporter\Internal\LazyObjectRegistry as Registry;
- use Symfony\Component\VarExporter\Internal\LazyObjectState;
- use Symfony\Component\VarExporter\Internal\LazyObjectTrait;
- trait LazyGhostTrait
- {
- use LazyObjectTrait;
- /**
- * Creates a lazy-loading ghost instance.
- *
- * When the initializer is a closure, it should initialize all properties at
- * once and is given the instance to initialize as argument.
- *
- * When the initializer is an array of closures, it should be indexed by
- * properties and closures should accept 4 arguments: the instance to
- * initialize, the property to initialize, its write-scope, and its default
- * value. Each closure should return the value of the corresponding property.
- * The special "\0" key can be used to define a closure that returns all
- * properties at once when full-initialization is needed; it takes the
- * instance and its default properties as arguments.
- *
- * Properties should be indexed by their array-cast name, see
- * https://php.net/manual/language.types.array#language.types.array.casting
- *
- * @param (\Closure(static):void
- * |array<string, \Closure(static, string, ?string, mixed):mixed>
- * |array{"\0": \Closure(static, array<string, mixed>):array<string, mixed>}) $initializer
- * @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, aka the ones
- * that the initializer doesn't set when its a closure
- * @param static|null $instance
- */
- public static function createLazyGhost(\Closure|array $initializer, array $skippedProperties = null, object $instance = null): static
- {
- $onlyProperties = null === $skippedProperties && \is_array($initializer) ? $initializer : null;
- if (self::class !== $class = $instance ? $instance::class : static::class) {
- $skippedProperties["\0".self::class."\0lazyObjectState"] = true;
- } elseif (\defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
- Hydrator::$propertyScopes[$class] ??= $class::LAZY_OBJECT_PROPERTY_SCOPES;
- }
- $instance ??= (Registry::$classReflectors[$class] ??= new \ReflectionClass($class))->newInstanceWithoutConstructor();
- Registry::$defaultProperties[$class] ??= (array) $instance;
- $instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);
- foreach (Registry::$classResetters[$class] ??= Registry::getClassResetters($class) as $reset) {
- $reset($instance, $skippedProperties, $onlyProperties);
- }
- return $instance;
- }
- /**
- * Returns whether the object is initialized.
- *
- * @param $partial Whether partially initialized objects should be considered as initialized
- */
- public function isLazyObjectInitialized(bool $partial = false): bool
- {
- if (!$state = $this->lazyObjectState ?? null) {
- return true;
- }
- if (!\is_array($state->initializer)) {
- return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
- }
- $class = $this::class;
- $properties = (array) $this;
- if ($partial) {
- return (bool) array_intersect_key($state->initializer, $properties);
- }
- $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
- foreach ($state->initializer as $key => $initializer) {
- if (!\array_key_exists($key, $properties) && isset($propertyScopes[$key])) {
- return false;
- }
- }
- return true;
- }
- /**
- * Forces initialization of a lazy object and returns it.
- */
- public function initializeLazyObject(): static
- {
- if (!$state = $this->lazyObjectState ?? null) {
- return $this;
- }
- if (!\is_array($state->initializer)) {
- if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
- $state->initialize($this, '', null);
- }
- return $this;
- }
- $values = isset($state->initializer["\0"]) ? null : [];
- $class = $this::class;
- $properties = (array) $this;
- $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
- foreach ($state->initializer as $key => $initializer) {
- if (\array_key_exists($key, $properties) || ![$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
- continue;
- }
- $scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
- if (null === $values) {
- if (!\is_array($values = ($state->initializer["\0"])($this, Registry::$defaultProperties[$class]))) {
- throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
- }
- if (\array_key_exists($key, $properties = (array) $this)) {
- continue;
- }
- }
- if (\array_key_exists($key, $values)) {
- $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
- $accessor['set']($this, $name, $properties[$key] = $values[$key]);
- } else {
- $state->initialize($this, $name, $scope);
- $properties = (array) $this;
- }
- }
- return $this;
- }
- /**
- * @return bool Returns false when the object cannot be reset, ie when it's not a lazy object
- */
- public function resetLazyObject(): bool
- {
- if (!$state = $this->lazyObjectState ?? null) {
- return false;
- }
- if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $state->status) {
- $state->reset($this);
- }
- return true;
- }
- public function &__get($name): mixed
- {
- $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
- $scope = null;
- if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
- $scope = Registry::getScope($propertyScopes, $class, $name);
- $state = $this->lazyObjectState ?? null;
- if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
- && LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
- ) {
- goto get_in_scope;
- }
- }
- if ($parent = (Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['get']) {
- if (2 === $parent) {
- return parent::__get($name);
- }
- $value = parent::__get($name);
- return $value;
- }
- if (null === $class) {
- $frame = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
- trigger_error(sprintf('Undefined property: %s::$%s in %s on line %s', $this::class, $name, $frame['file'], $frame['line']), \E_USER_NOTICE);
- }
- get_in_scope:
- try {
- if (null === $scope) {
- if (null === $readonlyScope) {
- return $this->$name;
- }
- $value = $this->$name;
- return $value;
- }
- $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
- return $accessor['get']($this, $name, null !== $readonlyScope);
- } catch (\Error $e) {
- if (\Error::class !== $e::class || !str_starts_with($e->getMessage(), 'Cannot access uninitialized non-nullable property')) {
- throw $e;
- }
- try {
- if (null === $scope) {
- $this->$name = [];
- return $this->$name;
- }
- $accessor['set']($this, $name, []);
- return $accessor['get']($this, $name, null !== $readonlyScope);
- } catch (\Error) {
- throw $e;
- }
- }
- }
- public function __set($name, $value): void
- {
- $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
- $scope = null;
- if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
- $scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
- $state = $this->lazyObjectState ?? null;
- if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
- if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
- $state->initialize($this, $name, $readonlyScope ?? $scope);
- }
- goto set_in_scope;
- }
- }
- if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['set']) {
- parent::__set($name, $value);
- return;
- }
- set_in_scope:
- if (null === $scope) {
- $this->$name = $value;
- } else {
- $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
- $accessor['set']($this, $name, $value);
- }
- }
- public function __isset($name): bool
- {
- $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
- $scope = null;
- if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
- $scope = Registry::getScope($propertyScopes, $class, $name);
- $state = $this->lazyObjectState ?? null;
- if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
- && LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
- ) {
- goto isset_in_scope;
- }
- }
- if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['isset']) {
- return parent::__isset($name);
- }
- isset_in_scope:
- if (null === $scope) {
- return isset($this->$name);
- }
- $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
- return $accessor['isset']($this, $name);
- }
- public function __unset($name): void
- {
- $propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
- $scope = null;
- if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
- $scope = Registry::getScope($propertyScopes, $class, $name, $readonlyScope);
- $state = $this->lazyObjectState ?? null;
- if ($state && ($readonlyScope === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
- if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
- $state->initialize($this, $name, $readonlyScope ?? $scope);
- }
- goto unset_in_scope;
- }
- }
- if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['unset']) {
- parent::__unset($name);
- return;
- }
- unset_in_scope:
- if (null === $scope) {
- unset($this->$name);
- } else {
- $accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
- $accessor['unset']($this, $name);
- }
- }
- public function __clone(): void
- {
- if ($state = $this->lazyObjectState ?? null) {
- $this->lazyObjectState = clone $state;
- }
- if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['clone']) {
- parent::__clone();
- }
- }
- public function __serialize(): array
- {
- $class = self::class;
- if ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['serialize']) {
- $properties = parent::__serialize();
- } else {
- $this->initializeLazyObject();
- $properties = (array) $this;
- }
- unset($properties["\0$class\0lazyObjectState"]);
- if (Registry::$parentMethods[$class]['serialize'] || !Registry::$parentMethods[$class]['sleep']) {
- return $properties;
- }
- $scope = get_parent_class($class);
- $data = [];
- foreach (parent::__sleep() as $name) {
- $value = $properties[$k = $name] ?? $properties[$k = "\0*\0$name"] ?? $properties[$k = "\0$scope\0$name"] ?? $k = null;
- if (null === $k) {
- trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $name), \E_USER_NOTICE);
- } else {
- $data[$k] = $value;
- }
- }
- return $data;
- }
- public function __destruct()
- {
- $state = $this->lazyObjectState ?? null;
- if ($state && \in_array($state->status, [LazyObjectState::STATUS_UNINITIALIZED_FULL, LazyObjectState::STATUS_UNINITIALIZED_PARTIAL], true)) {
- return;
- }
- if ((Registry::$parentMethods[self::class] ??= Registry::getParentMethods(self::class))['destruct']) {
- parent::__destruct();
- }
- }
- private function setLazyObjectAsInitialized(bool $initialized): void
- {
- $state = $this->lazyObjectState ?? null;
- if ($state && !\is_array($state->initializer)) {
- $state->status = $initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL;
- }
- }
- }
|