diff options
Diffstat (limited to 'vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php')
| -rw-r--r-- | vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php b/vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php new file mode 100644 index 0000000..f6450ce --- /dev/null +++ b/vendor/symfony/var-exporter/Internal/LazyObjectRegistry.php | |||
| @@ -0,0 +1,138 @@ | |||
| 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 objects and caches related reflection information. | ||
| 16 | * | ||
| 17 | * As a micro-optimization, this class uses no type declarations. | ||
| 18 | * | ||
| 19 | * @internal | ||
| 20 | */ | ||
| 21 | class LazyObjectRegistry | ||
| 22 | { | ||
| 23 | /** | ||
| 24 | * @var array<class-string, \ReflectionClass> | ||
| 25 | */ | ||
| 26 | public static array $classReflectors = []; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * @var array<class-string, array<string, mixed>> | ||
| 30 | */ | ||
| 31 | public static array $defaultProperties = []; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * @var array<class-string, list<\Closure>> | ||
| 35 | */ | ||
| 36 | public static array $classResetters = []; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * @var array<class-string, array{get: \Closure, set: \Closure, isset: \Closure, unset: \Closure}> | ||
| 40 | */ | ||
| 41 | public static array $classAccessors = []; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * @var array<class-string, array{set: bool, isset: bool, unset: bool, clone: bool, serialize: bool, unserialize: bool, sleep: bool, wakeup: bool, destruct: bool, get: int}> | ||
| 45 | */ | ||
| 46 | public static array $parentMethods = []; | ||
| 47 | |||
| 48 | public static ?\Closure $noInitializerState = null; | ||
| 49 | |||
| 50 | public static function getClassResetters($class) | ||
| 51 | { | ||
| 52 | $classProperties = []; | ||
| 53 | |||
| 54 | if ((self::$classReflectors[$class] ??= new \ReflectionClass($class))->isInternal()) { | ||
| 55 | $propertyScopes = []; | ||
| 56 | } else { | ||
| 57 | $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class); | ||
| 58 | } | ||
| 59 | |||
| 60 | foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) { | ||
| 61 | $propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name; | ||
| 62 | |||
| 63 | if ($k === $key && "\0$class\0lazyObjectState" !== $k) { | ||
| 64 | $classProperties[$readonlyScope ?? $scope][$name] = $key; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | $resetters = []; | ||
| 69 | foreach ($classProperties as $scope => $properties) { | ||
| 70 | $resetters[] = \Closure::bind(static function ($instance, $skippedProperties) use ($properties) { | ||
| 71 | foreach ($properties as $name => $key) { | ||
| 72 | if (!\array_key_exists($key, $skippedProperties)) { | ||
| 73 | unset($instance->$name); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | }, null, $scope); | ||
| 77 | } | ||
| 78 | |||
| 79 | return $resetters; | ||
| 80 | } | ||
| 81 | |||
| 82 | public static function getClassAccessors($class) | ||
| 83 | { | ||
| 84 | return \Closure::bind(static fn () => [ | ||
| 85 | 'get' => static function &($instance, $name, $readonly) { | ||
| 86 | if (!$readonly) { | ||
| 87 | return $instance->$name; | ||
| 88 | } | ||
| 89 | $value = $instance->$name; | ||
| 90 | |||
| 91 | return $value; | ||
| 92 | }, | ||
| 93 | 'set' => static function ($instance, $name, $value) { | ||
| 94 | $instance->$name = $value; | ||
| 95 | }, | ||
| 96 | 'isset' => static fn ($instance, $name) => isset($instance->$name), | ||
| 97 | 'unset' => static function ($instance, $name) { | ||
| 98 | unset($instance->$name); | ||
| 99 | }, | ||
| 100 | ], null, \Closure::class === $class ? null : $class)(); | ||
| 101 | } | ||
| 102 | |||
| 103 | public static function getParentMethods($class) | ||
| 104 | { | ||
| 105 | $parent = get_parent_class($class); | ||
| 106 | $methods = []; | ||
| 107 | |||
| 108 | foreach (['set', 'isset', 'unset', 'clone', 'serialize', 'unserialize', 'sleep', 'wakeup', 'destruct', 'get'] as $method) { | ||
| 109 | if (!$parent || !method_exists($parent, '__'.$method)) { | ||
| 110 | $methods[$method] = false; | ||
| 111 | } else { | ||
| 112 | $m = new \ReflectionMethod($parent, '__'.$method); | ||
| 113 | $methods[$method] = !$m->isAbstract() && !$m->isPrivate(); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | $methods['get'] = $methods['get'] ? ($m->returnsReference() ? 2 : 1) : 0; | ||
| 118 | |||
| 119 | return $methods; | ||
| 120 | } | ||
| 121 | |||
| 122 | public static function getScope($propertyScopes, $class, $property, $readonlyScope = null) | ||
| 123 | { | ||
| 124 | if (null === $readonlyScope && !isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) { | ||
| 125 | return null; | ||
| 126 | } | ||
| 127 | $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]; | ||
| 128 | |||
| 129 | if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) { | ||
| 130 | $scope = $frame['object']->class; | ||
| 131 | } | ||
| 132 | if (null === $readonlyScope && '*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) { | ||
| 133 | return null; | ||
| 134 | } | ||
| 135 | |||
| 136 | return $scope; | ||
| 137 | } | ||
| 138 | } | ||
