Файл: vendor/symfony/var-exporter/LazyGhostTrait.php
Строк: 364
<?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 SymfonyComponentVarExporter;
use SymfonyComponentSerializerAttributeIgnore;
use SymfonyComponentVarExporterInternalHydrator;
use SymfonyComponentVarExporterInternalLazyObjectRegistry as Registry;
use SymfonyComponentVarExporterInternalLazyObjectState;
use SymfonyComponentVarExporterInternalLazyObjectTrait;
if (PHP_VERSION_ID >= 80400) {
trigger_deprecation('symfony/var-exporter', '7.3', 'The "%s" trait is deprecated, use native lazy objects instead.', LazyGhostTrait::class);
}
/**
* @deprecated since Symfony 7.3, use native lazy objects instead
*/
trait LazyGhostTrait
{
use LazyObjectTrait;
/**
* Creates a lazy-loading ghost instance.
*
* Skipped properties should be indexed by their array-cast identifier, see
* https://php.net/manual/language.types.array#language.types.array.casting
*
* @param Closure(static):void $initializer The closure should initialize the object it receives as argument
* @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, a.k.a. the ones
* that the initializer doesn't initialize, if any
* @param static|null $instance
*/
public static function createLazyGhost(Closure $initializer, ?array $skippedProperties = null, ?object $instance = null): static
{
if (self::class !== $class = $instance ? $instance::class : static::class) {
$skippedProperties[" ".self::class." lazyObjectState"] = true;
}
if (!isset(Registry::$defaultProperties[$class])) {
Registry::$classReflectors[$class] ??= new ReflectionClass($class);
$instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
Registry::$defaultProperties[$class] ??= (array) $instance;
Registry::$classResetters[$class] ??= Registry::getClassResetters($class);
if (self::class === $class && defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
Hydrator::$propertyScopes[$class] ??= $class::LAZY_OBJECT_PROPERTY_SCOPES;
}
} else {
$instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
}
if (isset($instance->lazyObjectState)) {
$instance->lazyObjectState->initializer = $initializer;
$instance->lazyObjectState->skippedProperties = $skippedProperties ??= [];
if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $instance->lazyObjectState->status) {
$instance->lazyObjectState->reset($instance);
}
return $instance;
}
$instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);
foreach (Registry::$classResetters[$class] as $reset) {
$reset($instance, $skippedProperties);
}
return $instance;
}
/**
* Returns whether the object is initialized.
*
* @param bool $partial Whether partially initialized objects should be considered as initialized
*/
#[Ignore]
public function isLazyObjectInitialized(bool $partial = false): bool
{
if (!$state = $this->lazyObjectState ?? null) {
return true;
}
return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
}
/**
* Forces initialization of a lazy object and returns it.
*/
public function initializeLazyObject(): static
{
if (!$state = $this->lazyObjectState ?? null) {
return $this;
}
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
$state->initialize($this, '', null);
}
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;
$notByRef = 0;
if ([$class, , $writeScope, $access] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScopeForRead($propertyScopes, $class, $name);
$state = $this->lazyObjectState ?? null;
if ($state && (null === $scope || isset($propertyScopes[" $scope