|
| 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 | +/** |
| 15 | + * Stores the state of lazy ghost objects and caches related reflection information. |
| 16 | + * |
| 17 | + * As a micro-optimization, this class uses no type-hints. |
| 18 | + * |
| 19 | + * @internal |
| 20 | + */ |
| 21 | +class GhostObjectRegistry |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var array<int, GhostObjectState> |
| 25 | + */ |
| 26 | + public static $states = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var array<class-string, \ReflectionClass> |
| 30 | + */ |
| 31 | + public static $classReflectors = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var array<class-string, array<string, mixed>> |
| 35 | + */ |
| 36 | + public static $defaultProperties = []; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var array<class-string, list<\Closure>> |
| 40 | + */ |
| 41 | + public static $classResetters = []; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var array<class-string, array{get: \Closure, set: \Closure, isset: \Closure, unset: \Closure}> |
| 45 | + */ |
| 46 | + public static $classAccessors = []; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var array<class-string, array{get: int, set: bool, isset: bool, unset: bool, clone: bool, serialize: bool, sleep: bool, destruct: bool}> |
| 50 | + */ |
| 51 | + public static $parentMethods = []; |
| 52 | + |
| 53 | + public static function getClassResetters($class) |
| 54 | + { |
| 55 | + $classProperties = []; |
| 56 | + $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class); |
| 57 | + |
| 58 | + foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) { |
| 59 | + if ('lazyGhostObjectId' !== $name && null !== ($propertyScopes["\0$scope\0$name"] ?? $propertyScopes["\0*\0$name"] ?? $readonlyScope)) { |
| 60 | + $classProperties[$readonlyScope ?? $scope][$name] = $key; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $resetters = []; |
| 65 | + foreach ($classProperties as $scope => $properties) { |
| 66 | + $resetters[] = \Closure::bind(static function ($instance, $skippedProperties = []) use ($properties) { |
| 67 | + foreach ($properties as $name => $key) { |
| 68 | + if (!\array_key_exists($key, $skippedProperties)) { |
| 69 | + unset($instance->$name); |
| 70 | + } |
| 71 | + } |
| 72 | + }, null, $scope); |
| 73 | + } |
| 74 | + |
| 75 | + $resetters[] = static function ($instance, $skippedProperties = []) { |
| 76 | + foreach ((array) $instance as $name => $value) { |
| 77 | + if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties)) { |
| 78 | + unset($instance->$name); |
| 79 | + } |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + return $resetters; |
| 84 | + } |
| 85 | + |
| 86 | + public static function getClassAccessors($class) |
| 87 | + { |
| 88 | + return \Closure::bind(static function () { |
| 89 | + return [ |
| 90 | + 'get' => static function &($instance, $name, $readonly) { |
| 91 | + if (!$readonly) { |
| 92 | + return $instance->$name; |
| 93 | + } |
| 94 | + $value = $instance->$name; |
| 95 | + |
| 96 | + return $value; |
| 97 | + }, |
| 98 | + 'set' => static function ($instance, $name, $value) { |
| 99 | + $instance->$name = $value; |
| 100 | + }, |
| 101 | + 'isset' => static function ($instance, $name) { |
| 102 | + return isset($instance->$name); |
| 103 | + }, |
| 104 | + 'unset' => static function ($instance, $name) { |
| 105 | + unset($instance->$name); |
| 106 | + }, |
| 107 | + ]; |
| 108 | + }, null, $class)(); |
| 109 | + } |
| 110 | + |
| 111 | + public static function getParentMethods($class) |
| 112 | + { |
| 113 | + $parent = get_parent_class($class); |
| 114 | + |
| 115 | + return [ |
| 116 | + 'get' => $parent && method_exists($parent, '__get') ? ((new \ReflectionMethod($parent, '__get'))->returnsReference() ? 2 : 1) : 0, |
| 117 | + 'set' => $parent && method_exists($parent, '__set'), |
| 118 | + 'isset' => $parent && method_exists($parent, '__isset'), |
| 119 | + 'unset' => $parent && method_exists($parent, '__unset'), |
| 120 | + 'clone' => $parent && method_exists($parent, '__clone'), |
| 121 | + 'serialize' => $parent && method_exists($parent, '__serialize'), |
| 122 | + 'sleep' => $parent && method_exists($parent, '__sleep'), |
| 123 | + 'destruct' => $parent && method_exists($parent, '__destruct'), |
| 124 | + ]; |
| 125 | + } |
| 126 | +} |
0 commit comments