LazyObjectRegistry.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarExporter\Internal;
  11. /**
  12. * Stores the state of lazy objects and caches related reflection information.
  13. *
  14. * As a micro-optimization, this class uses no type declarations.
  15. *
  16. * @internal
  17. */
  18. class LazyObjectRegistry
  19. {
  20. /**
  21. * @var array<class-string, \ReflectionClass>
  22. */
  23. public static array $classReflectors = [];
  24. /**
  25. * @var array<class-string, array<string, mixed>>
  26. */
  27. public static array $defaultProperties = [];
  28. /**
  29. * @var array<class-string, list<\Closure>>
  30. */
  31. public static array $classResetters = [];
  32. /**
  33. * @var array<class-string, array{get: \Closure, set: \Closure, isset: \Closure, unset: \Closure}>
  34. */
  35. public static array $classAccessors = [];
  36. /**
  37. * @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}>
  38. */
  39. public static array $parentMethods = [];
  40. public static ?\Closure $noInitializerState = null;
  41. public static function getClassResetters($class)
  42. {
  43. $classProperties = [];
  44. $hookedProperties = [];
  45. if ((self::$classReflectors[$class] ??= new \ReflectionClass($class))->isInternal()) {
  46. $propertyScopes = [];
  47. } else {
  48. $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
  49. }
  50. foreach ($propertyScopes as $key => [$scope, $name, $writeScope, $access]) {
  51. $propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
  52. if ($k !== $key || "\0$class\0lazyObjectState" === $k) {
  53. continue;
  54. }
  55. if ($access & Hydrator::PROPERTY_HAS_HOOKS) {
  56. $hookedProperties[$k] = true;
  57. } else {
  58. $classProperties[$writeScope ?? $scope][$name] = $key;
  59. }
  60. }
  61. $resetters = [];
  62. foreach ($classProperties as $scope => $properties) {
  63. $resetters[] = \Closure::bind(static function ($instance, $skippedProperties) use ($properties) {
  64. foreach ($properties as $name => $key) {
  65. if (!\array_key_exists($key, $skippedProperties)) {
  66. unset($instance->$name);
  67. }
  68. }
  69. }, null, $scope);
  70. }
  71. return $resetters;
  72. }
  73. public static function getClassAccessors($class)
  74. {
  75. return \Closure::bind(static fn () => [
  76. 'get' => static function &($instance, $name, $notByRef) {
  77. if (!$notByRef) {
  78. return $instance->$name;
  79. }
  80. $value = $instance->$name;
  81. return $value;
  82. },
  83. 'set' => static function ($instance, $name, $value) {
  84. $instance->$name = $value;
  85. },
  86. 'isset' => static fn ($instance, $name) => isset($instance->$name),
  87. 'unset' => static function ($instance, $name) {
  88. unset($instance->$name);
  89. },
  90. ], null, \Closure::class === $class ? null : $class)();
  91. }
  92. public static function getParentMethods($class)
  93. {
  94. $parent = get_parent_class($class);
  95. $methods = [];
  96. foreach (['set', 'isset', 'unset', 'clone', 'serialize', 'unserialize', 'sleep', 'wakeup', 'destruct', 'get'] as $method) {
  97. if (!$parent || !method_exists($parent, '__'.$method)) {
  98. $methods[$method] = false;
  99. } else {
  100. $m = new \ReflectionMethod($parent, '__'.$method);
  101. $methods[$method] = !$m->isAbstract() && !$m->isPrivate();
  102. }
  103. }
  104. $methods['get'] = $methods['get'] ? ($m->returnsReference() ? 2 : 1) : 0;
  105. return $methods;
  106. }
  107. public static function getScopeForRead($propertyScopes, $class, $property)
  108. {
  109. if (!isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
  110. return null;
  111. }
  112. $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
  113. if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
  114. $scope = $frame['object']->class;
  115. }
  116. if ('*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
  117. return null;
  118. }
  119. return $scope;
  120. }
  121. public static function getScopeForWrite($propertyScopes, $class, $property, $flags)
  122. {
  123. if (!($flags & (\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_READONLY | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET | \ReflectionProperty::IS_PROTECTED_SET : 0)))) {
  124. return null;
  125. }
  126. $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
  127. if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
  128. $scope = $frame['object']->class;
  129. }
  130. if ($flags & (\ReflectionProperty::IS_PRIVATE | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY))) {
  131. return $scope;
  132. }
  133. if ($flags & (\ReflectionProperty::IS_PROTECTED | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PROTECTED_SET : 0)) && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
  134. return null;
  135. }
  136. return $scope;
  137. }
  138. }