LazyObjectRegistry.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 $classReflectors = [];
  24. /**
  25. * @var array<class-string, array<string, mixed>>
  26. */
  27. public static $defaultProperties = [];
  28. /**
  29. * @var array<class-string, list<\Closure>>
  30. */
  31. public static $classResetters = [];
  32. /**
  33. * @var array<class-string, array{get: \Closure, set: \Closure, isset: \Closure, unset: \Closure}>
  34. */
  35. public static $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 $parentMethods = [];
  40. public static ?\Closure $noInitializerState = null;
  41. public static function getClassResetters($class)
  42. {
  43. $classProperties = [];
  44. if ((self::$classReflectors[$class] ??= new \ReflectionClass($class))->isInternal()) {
  45. $propertyScopes = [];
  46. } else {
  47. $propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
  48. }
  49. foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) {
  50. $propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
  51. if ($k === $key && "\0$class\0lazyObjectState" !== $k) {
  52. $classProperties[$readonlyScope ?? $scope][$name] = $key;
  53. }
  54. }
  55. $resetters = [];
  56. foreach ($classProperties as $scope => $properties) {
  57. $resetters[] = \Closure::bind(static function ($instance, $skippedProperties, $onlyProperties = null) use ($properties) {
  58. foreach ($properties as $name => $key) {
  59. if (!\array_key_exists($key, $skippedProperties) && (null === $onlyProperties || \array_key_exists($key, $onlyProperties))) {
  60. unset($instance->$name);
  61. }
  62. }
  63. }, null, $scope);
  64. }
  65. $resetters[] = static function ($instance, $skippedProperties, $onlyProperties = null) {
  66. foreach ((array) $instance as $name => $value) {
  67. if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties) && (null === $onlyProperties || \array_key_exists($name, $onlyProperties))) {
  68. unset($instance->$name);
  69. }
  70. }
  71. };
  72. return $resetters;
  73. }
  74. public static function getClassAccessors($class)
  75. {
  76. return \Closure::bind(static fn () => [
  77. 'get' => static function &($instance, $name, $readonly) {
  78. if (!$readonly) {
  79. return $instance->$name;
  80. }
  81. $value = $instance->$name;
  82. return $value;
  83. },
  84. 'set' => static function ($instance, $name, $value) {
  85. $instance->$name = $value;
  86. },
  87. 'isset' => static fn ($instance, $name) => isset($instance->$name),
  88. 'unset' => static function ($instance, $name) {
  89. unset($instance->$name);
  90. },
  91. ], null, \Closure::class === $class ? null : $class)();
  92. }
  93. public static function getParentMethods($class)
  94. {
  95. $parent = get_parent_class($class);
  96. $methods = [];
  97. foreach (['set', 'isset', 'unset', 'clone', 'serialize', 'unserialize', 'sleep', 'wakeup', 'destruct', 'get'] as $method) {
  98. if (!$parent || !method_exists($parent, '__'.$method)) {
  99. $methods[$method] = false;
  100. } else {
  101. $m = new \ReflectionMethod($parent, '__'.$method);
  102. $methods[$method] = !$m->isAbstract() && !$m->isPrivate();
  103. }
  104. }
  105. $methods['get'] = $methods['get'] ? ($m->returnsReference() ? 2 : 1) : 0;
  106. return $methods;
  107. }
  108. public static function getScope($propertyScopes, $class, $property, $readonlyScope = null)
  109. {
  110. if (null === $readonlyScope && !isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
  111. return null;
  112. }
  113. $frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];
  114. if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
  115. $scope = $frame['object']->class;
  116. }
  117. if (null === $readonlyScope && '*' === $k[1] && ($class === $scope || is_subclass_of($class, $scope))) {
  118. return null;
  119. }
  120. return $scope;
  121. }
  122. }