vendor/symfony/var-exporter/Internal/Registry.php line 50

Open in your IDE?
  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. use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
  12. use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. *
  16. * @internal
  17. */
  18. class Registry
  19. {
  20. public static array $reflectors = [];
  21. public static array $prototypes = [];
  22. public static array $factories = [];
  23. public static array $cloneable = [];
  24. public static array $instantiableWithoutConstructor = [];
  25. public function __construct(
  26. public readonly array $classes,
  27. ) {
  28. }
  29. public static function unserialize($objects, $serializables)
  30. {
  31. $unserializeCallback = ini_set('unserialize_callback_func', __CLASS__.'::getClassReflector');
  32. try {
  33. foreach ($serializables as $k => $v) {
  34. $objects[$k] = unserialize($v, ['allowed_classes' => true]);
  35. }
  36. } finally {
  37. ini_set('unserialize_callback_func', $unserializeCallback);
  38. }
  39. return $objects;
  40. }
  41. public static function p($class)
  42. {
  43. self::getClassReflector($class, true, true);
  44. return self::$prototypes[$class];
  45. }
  46. public static function f($class)
  47. {
  48. $reflector = self::$reflectors[$class] ??= self::getClassReflector($class, true, false);
  49. return self::$factories[$class] = $reflector->newInstanceWithoutConstructor(...);
  50. }
  51. public static function getClassReflector($class, $instantiableWithoutConstructor = false, $cloneable = null)
  52. {
  53. if (!($isClass = class_exists($class)) && !interface_exists($class, false) && !trait_exists($class, false)) {
  54. throw new ClassNotFoundException($class);
  55. }
  56. $reflector = new \ReflectionClass($class);
  57. if ($instantiableWithoutConstructor) {
  58. $proto = $reflector->newInstanceWithoutConstructor();
  59. } elseif (!$isClass || $reflector->isAbstract()) {
  60. throw new NotInstantiableTypeException($class);
  61. } elseif ($reflector->name !== $class) {
  62. $reflector = self::$reflectors[$name = $reflector->name] ??= self::getClassReflector($name, false, $cloneable);
  63. self::$cloneable[$class] = self::$cloneable[$name];
  64. self::$instantiableWithoutConstructor[$class] = self::$instantiableWithoutConstructor[$name];
  65. self::$prototypes[$class] = self::$prototypes[$name];
  66. return $reflector;
  67. } else {
  68. try {
  69. $proto = $reflector->newInstanceWithoutConstructor();
  70. $instantiableWithoutConstructor = true;
  71. } catch (\ReflectionException) {
  72. $proto = $reflector->implementsInterface('Serializable') && !method_exists($class, '__unserialize') ? 'C:' : 'O:';
  73. if ('C:' === $proto && !$reflector->getMethod('unserialize')->isInternal()) {
  74. $proto = null;
  75. } else {
  76. try {
  77. $proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}', ['allowed_classes' => true]);
  78. } catch (\Exception $e) {
  79. if (method_exists($class, '__unserialize')) {
  80. // The class cannot be instantiated empty but defines __serialize()/__unserialize();
  81. // it'll be reconstructed by serializing the whole value.
  82. $proto = null;
  83. } elseif (__FILE__ !== $e->getFile()) {
  84. throw $e;
  85. } else {
  86. throw new NotInstantiableTypeException($class, $e);
  87. }
  88. }
  89. if (false === $proto) {
  90. if (!method_exists($class, '__unserialize')) {
  91. throw new NotInstantiableTypeException($class);
  92. }
  93. $proto = null;
  94. }
  95. }
  96. }
  97. if (null !== $proto && !$proto instanceof \Throwable && !$proto instanceof \Serializable && !method_exists($class, '__sleep') && !method_exists($class, '__serialize')) {
  98. try {
  99. serialize($proto);
  100. } catch (\Exception $e) {
  101. throw new NotInstantiableTypeException($class, $e);
  102. }
  103. }
  104. }
  105. if (null === $cloneable) {
  106. if (($proto instanceof \Reflector || $proto instanceof \ReflectionGenerator || $proto instanceof \ReflectionType || $proto instanceof \IteratorIterator || $proto instanceof \RecursiveIteratorIterator) && (!$proto instanceof \Serializable && !method_exists($proto, '__wakeup') && !method_exists($class, '__unserialize'))) {
  107. throw new NotInstantiableTypeException($class);
  108. }
  109. $cloneable = $reflector->isCloneable() && !$reflector->hasMethod('__clone');
  110. }
  111. self::$cloneable[$class] = $cloneable;
  112. self::$instantiableWithoutConstructor[$class] = $instantiableWithoutConstructor;
  113. self::$prototypes[$class] = $proto;
  114. if ($proto instanceof \Throwable) {
  115. static $setTrace;
  116. if (null === $setTrace) {
  117. $setTrace = [
  118. new \ReflectionProperty(\Error::class, 'trace'),
  119. new \ReflectionProperty(\Exception::class, 'trace'),
  120. ];
  121. $setTrace[0] = $setTrace[0]->setValue(...);
  122. $setTrace[1] = $setTrace[1]->setValue(...);
  123. }
  124. $setTrace[$proto instanceof \Exception]($proto, []);
  125. }
  126. return $reflector;
  127. }
  128. }