diff options
Diffstat (limited to 'vendor/symfony/var-exporter/Internal/LazyObjectState.php')
-rw-r--r-- | vendor/symfony/var-exporter/Internal/LazyObjectState.php | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/Internal/LazyObjectState.php b/vendor/symfony/var-exporter/Internal/LazyObjectState.php new file mode 100644 index 0000000..5fc398e --- /dev/null +++ b/vendor/symfony/var-exporter/Internal/LazyObjectState.php | |||
@@ -0,0 +1,97 @@ | |||
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 as PublicHydrator; | ||
15 | |||
16 | /** | ||
17 | * Keeps the state of lazy objects. | ||
18 | * | ||
19 | * As a micro-optimization, this class uses no type declarations. | ||
20 | * | ||
21 | * @internal | ||
22 | */ | ||
23 | class LazyObjectState | ||
24 | { | ||
25 | public const STATUS_UNINITIALIZED_FULL = 1; | ||
26 | public const STATUS_UNINITIALIZED_PARTIAL = 2; | ||
27 | public const STATUS_INITIALIZED_FULL = 3; | ||
28 | public const STATUS_INITIALIZED_PARTIAL = 4; | ||
29 | |||
30 | /** | ||
31 | * @var self::STATUS_* | ||
32 | */ | ||
33 | public int $status = self::STATUS_UNINITIALIZED_FULL; | ||
34 | |||
35 | public object $realInstance; | ||
36 | |||
37 | /** | ||
38 | * @param array<string, true> $skippedProperties | ||
39 | */ | ||
40 | public function __construct( | ||
41 | public readonly \Closure $initializer, | ||
42 | public readonly array $skippedProperties = [], | ||
43 | ) { | ||
44 | } | ||
45 | |||
46 | public function initialize($instance, $propertyName, $propertyScope) | ||
47 | { | ||
48 | if (self::STATUS_UNINITIALIZED_FULL !== $this->status) { | ||
49 | return $this->status; | ||
50 | } | ||
51 | |||
52 | $this->status = self::STATUS_INITIALIZED_PARTIAL; | ||
53 | |||
54 | try { | ||
55 | if ($defaultProperties = array_diff_key(LazyObjectRegistry::$defaultProperties[$instance::class], $this->skippedProperties)) { | ||
56 | PublicHydrator::hydrate($instance, $defaultProperties); | ||
57 | } | ||
58 | |||
59 | ($this->initializer)($instance); | ||
60 | } catch (\Throwable $e) { | ||
61 | $this->status = self::STATUS_UNINITIALIZED_FULL; | ||
62 | $this->reset($instance); | ||
63 | |||
64 | throw $e; | ||
65 | } | ||
66 | |||
67 | return $this->status = self::STATUS_INITIALIZED_FULL; | ||
68 | } | ||
69 | |||
70 | public function reset($instance): void | ||
71 | { | ||
72 | $class = $instance::class; | ||
73 | $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class); | ||
74 | $skippedProperties = $this->skippedProperties; | ||
75 | $properties = (array) $instance; | ||
76 | |||
77 | foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) { | ||
78 | $propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name; | ||
79 | |||
80 | if ($k === $key && (null !== $readonlyScope || !\array_key_exists($k, $properties))) { | ||
81 | $skippedProperties[$k] = true; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | foreach (LazyObjectRegistry::$classResetters[$class] as $reset) { | ||
86 | $reset($instance, $skippedProperties); | ||
87 | } | ||
88 | |||
89 | foreach ((array) $instance as $name => $value) { | ||
90 | if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties)) { | ||
91 | unset($instance->$name); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | $this->status = self::STATUS_UNINITIALIZED_FULL; | ||
96 | } | ||
97 | } | ||