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