Файл: vendor/symfony/var-exporter/LazyProxyTrait.php
Строк: 417
<?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 SymfonyComponentVarExporterHydrator as PublicHydrator;
use SymfonyComponentVarExporterInternalHydrator;
use SymfonyComponentVarExporterInternalLazyObjectRegistry as Registry;
use SymfonyComponentVarExporterInternalLazyObjectState;
use SymfonyComponentVarExporterInternalLazyObjectTrait;
trait LazyProxyTrait
{
use LazyObjectTrait;
/**
* Creates a lazy-loading virtual proxy.
*
* @param Closure():object $initializer Returns the proxied object
* @param static|null $instance
*/
public static function createLazyProxy(Closure $initializer, ?object $instance = null): static
{
if (self::class !== $class = $instance ? $instance::class : static::class) {
$skippedProperties = [" ".self::class." lazyObjectState" => true];
} elseif (defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
Hydrator::$propertyScopes[$class] ??= $class::LAZY_OBJECT_PROPERTY_SCOPES;
}
$instance ??= (Registry::$classReflectors[$class] ??= new ReflectionClass($class))->newInstanceWithoutConstructor();
$instance->lazyObjectState = new LazyObjectState($initializer);
foreach (Registry::$classResetters[$class] ??= Registry::getClassResetters($class) as $reset) {
$reset($instance, $skippedProperties ??= []);
}
return $instance;
}
/**
* Returns whether the object is initialized.
*
* @param $partial Whether partially initialized objects should be considered as initialized
*/
#[Ignore]
public function isLazyObjectInitialized(bool $partial = false): bool
{
return !isset($this->lazyObjectState) || isset($this->lazyObjectState->realInstance) || Registry::$noInitializerState === $this->lazyObjectState->initializer;
}
/**
* Forces initialization of a lazy object and returns it.
*/
public function initializeLazyObject(): parent
{
if ($state = $this->lazyObjectState ?? null) {
return $state->realInstance ??= ($state->initializer)();
}
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 (!isset($this->lazyObjectState) || Registry::$noInitializerState === $this->lazyObjectState->initializer) {
return false;
}
unset($this->lazyObjectState->realInstance);
return true;
}
public function &__get($name): mixed
{
$propertyScopes = Hydrator::$propertyScopes[$this::class] ??= Hydrator::getPropertyScopes($this::class);
$scope = null;
$instance = $this;
if ([$class, , $readonlyScope] = $propertyScopes[$name] ?? null) {
$scope = Registry::getScope($propertyScopes, $class, $name);
if (null === $scope || isset($propertyScopes[" $scope