LazyObjectRegistry.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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, $onlyProperties = null) use ($properties) {
  64. foreach ($properties as $name => $key) {
  65. if (!\array_key_exists($key, $skippedProperties) && (null === $onlyProperties || \array_key_exists($key, $onlyProperties))) {
  66. unset($instance->$name);
  67. }
  68. }
  69. }, null, $scope);
  70. }
  71. $resetters[] = static function ($instance, $skippedProperties, $onlyProperties = null) use ($hookedProperties) {
  72. foreach ((array) $instance as $name => $value) {
  73. if ("\0" !== ($name[0] ?? '')
  74. && !\array_key_exists($name, $skippedProperties)
  75. && (null === $onlyProperties || \array_key_exists($name, $onlyProperties))
  76. && !isset($hookedProperties[$name])
  77. ) {
  78. unset($instance->$name);
  79. }
  80. }
  81. };
  82. return $resetters;
  83. }
  84. public static function getClassAccessors($class)
  85. {
  86. return \Closure::bind(static fn () => [
  87. 'get' => static function &($instance, $name, $notByRef) {
  88. if (!$notByRef) {
  89. return $instance->$name;
  90. }
  91. $value = $instance->$name;
  92. return $value;
  93. },
  94. 'set' => static function ($instance, $name, $value) {
  95. $instance->$name = $value;
  96. },
  97. 'isset' => static fn ($instance, $name) => isset($instance->$name),
  98. 'unset' => static function ($instance, $name) {
  99. unset($instance->$name);
  100. },
  101. ], null, \Closure::class === $class ? null : $class)();
  102. }
  103. public static function getParentMethods($class)
  104. {
  105. $parent = get_parent_class($class);
  106. $methods = [];
  107. foreach (['set', 'isset', 'unset', 'clone', 'serialize', 'unserialize', 'sleep', 'wakeup', 'destruct', 'get'] as $method) {
  108. if (!$parent || !method_exists($parent, '__'.$method)) {
  109. $methods[$method] = false;
  110. } else {
  111. $m = new \ReflectionMethod($parent, '__'.$method);
  112. $methods[$method] = !$m->isAbstract() && !$m->isPrivate();
  113. }
  114. }
  115. $methods['get'] = $methods['get'] ? ($m->returnsReference() ? 2 : 1) : 0;
  116. return $methods;
  117. }
  118. public static function getScopeForRead($propertyScopes, $class, $property)
  119. {
  120. if (!isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
  121. return null;
  122. }
  123. $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
  124. if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
  125. $scope = $frame['object']->class;
  126. }
  127. if ('*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
  128. return null;
  129. }
  130. return $scope;
  131. }
  132. public static function getScopeForWrite($propertyScopes, $class, $property, $flags)
  133. {
  134. if (!($flags & (\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_READONLY | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET | \ReflectionProperty::IS_PROTECTED_SET : 0)))) {
  135. return null;
  136. }
  137. $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
  138. if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
  139. $scope = $frame['object']->class;
  140. }
  141. if ($flags & (\ReflectionProperty::IS_PRIVATE | (\PHP_VERSION_ID >= 80400 ? \ReflectionProperty::IS_PRIVATE_SET : \ReflectionProperty::IS_READONLY))) {
  142. return $scope;
  143. }
  144. 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"])))) {
  145. return null;
  146. }
  147. return $scope;
  148. }
  149. }