|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\VarExporter\Internal; |
| 13 | + |
| 14 | +use Symfony\Component\VarExporter\Hydrator; |
| 15 | +use Symfony\Component\VarExporter\Internal\Hydrator as InternalHydrator; |
| 16 | + |
| 17 | +/** |
| 18 | + * @internal |
| 19 | + */ |
| 20 | +class LazyGhostObjectRegistry |
| 21 | +{ |
| 22 | + public const STATUS_UNINITIALIZED = 1; |
| 23 | + public const STATUS_INITIALIZING = 2; |
| 24 | + public const STATUS_INITIALIZED = 3; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var array<int, \Closure> |
| 28 | + */ |
| 29 | + public static $initializers = []; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var array<int, array<string, mixed>> |
| 33 | + */ |
| 34 | + public static $knownProperties = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var array<class-string, array{0: array<string, mixed>, 1: list<\Closure>}> |
| 38 | + */ |
| 39 | + public static $propertiesDefaults = []; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var array<class-string, \ReflectionClass> |
| 43 | + */ |
| 44 | + public static $classReflectors = []; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var array<class-string, list<\Closure>> |
| 48 | + */ |
| 49 | + public static $classResetters = []; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var array<class-string, array<string, int>> |
| 53 | + */ |
| 54 | + public static $parentMethods = []; |
| 55 | + |
| 56 | + public static function getParentMethods($class) |
| 57 | + { |
| 58 | + $parent = get_parent_class($class); |
| 59 | + |
| 60 | + return [ |
| 61 | + '__get' => $parent && method_exists($parent, '__get') ? ((new \ReflectionMethod($parent, '__get'))->returnsReference() ? 2 : 1) : 0, |
| 62 | + '__set' => $parent && method_exists($parent, '__set') ? 1 : 0, |
| 63 | + '__isset' => $parent && method_exists($parent, '__isset') ? 1 : 0, |
| 64 | + '__unset' => $parent && method_exists($parent, '__unset') ? 1 : 0, |
| 65 | + '__clone' => $parent && method_exists($parent, '__clone') ? 1 : 0, |
| 66 | + '__serialize' => $parent && method_exists($parent, '__serialize') ? 1 : 0, |
| 67 | + '__sleep' => $parent && method_exists($parent, '__sleep') ? 1 : 0, |
| 68 | + '__destruct' => $parent && method_exists($parent, '__destruct') ? 1 : 0, |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + public static function getClassResetters($class) |
| 73 | + { |
| 74 | + $classProperties = []; |
| 75 | + foreach (InternalHydrator::$propertyScopes[$class] ??= InternalHydrator::getPropertyScopes($class) as $key => [$scope, $name]) { |
| 76 | + if ('lazyGhostObjectState' !== $name && null !== $scope) { |
| 77 | + $classProperties[$scope][$name] = $key; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $resetters = []; |
| 82 | + foreach ($classProperties as $scope => $properties) { |
| 83 | + $resetters[] = \Closure::bind(static function ($instance, $knownProperties) use ($properties) { |
| 84 | + foreach ($properties as $name => $key) { |
| 85 | + if (!\array_key_exists($key, $knownProperties)) { |
| 86 | + unset($instance->$name); |
| 87 | + continue; |
| 88 | + } |
| 89 | + $value = $knownProperties[$key]; |
| 90 | + |
| 91 | + if (null !== $value && $value !== $instance->$name ??= $value) { |
| 92 | + $instance->$name = $value; |
| 93 | + } |
| 94 | + } |
| 95 | + }, null, $scope); |
| 96 | + } |
| 97 | + |
| 98 | + $resetters[] = static function ($instance, $knownProperties) { |
| 99 | + foreach ((array) $instance as $name => $value) { |
| 100 | + if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $knownProperties)) { |
| 101 | + unset($instance->$name); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + foreach ($knownProperties as $name => $value) { |
| 106 | + if (null !== $value && "\0" !== ($name[0] ?? '') && $value !== $instance->$name ??= $value) { |
| 107 | + $instance->$name = $value; |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + return $resetters; |
| 113 | + } |
| 114 | + |
| 115 | + public static function initialize($instance, &$state) |
| 116 | + { |
| 117 | + $id = $state & ~3; |
| 118 | + $state = $id | self::STATUS_INITIALIZING; |
| 119 | + |
| 120 | + $propertiesDefaults = self::$propertiesDefaults[\get_class($instance)]; |
| 121 | + |
| 122 | + foreach (self::$knownProperties[$id] ?? [] as $key => $value) { |
| 123 | + if (null !== $value) { |
| 124 | + $propertiesDefaults[$key] = $value; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + Hydrator::hydrate($instance, $propertiesDefaults); |
| 129 | + self::$initializers[$id]($instance); |
| 130 | + |
| 131 | + $state = $id | self::STATUS_INITIALIZED; |
| 132 | + |
| 133 | + return true; |
| 134 | + } |
| 135 | +} |
0 commit comments