1 | <?php |
||
20 | final class Instantiator implements InstantiatorInterface |
||
21 | { |
||
22 | /** |
||
23 | * Markers used internally by PHP to define whether {@see \unserialize} should invoke |
||
24 | * the method {@see \Serializable::unserialize()} when dealing with classes implementing |
||
25 | * the {@see \Serializable} interface. |
||
26 | */ |
||
27 | public const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C'; |
||
28 | public const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O'; |
||
29 | |||
30 | /** |
||
31 | * Used to instantiate specific classes, indexed by class name. |
||
32 | * |
||
33 | * @var callable[] |
||
34 | */ |
||
35 | private static $cachedInstantiators = []; |
||
36 | |||
37 | /** |
||
38 | * Array of objects that can directly be cloned, indexed by class name. |
||
39 | * |
||
40 | * @var object[] |
||
41 | */ |
||
42 | private static $cachedCloneables = []; |
||
43 | |||
44 | /** |
||
45 | * {@inheritDoc} |
||
46 | */ |
||
47 | 40 | public function instantiate($className) |
|
61 | |||
62 | /** |
||
63 | * Builds the requested object and caches it in static properties for performance |
||
64 | * |
||
65 | * @return object |
||
66 | */ |
||
67 | 22 | private function buildAndCacheFromFactory(string $className) |
|
78 | |||
79 | /** |
||
80 | * Builds a callable capable of instantiating the given $className without |
||
81 | * invoking its constructor. |
||
82 | * |
||
83 | * @throws InvalidArgumentException |
||
84 | * @throws UnexpectedValueException |
||
85 | * @throws ReflectionException |
||
86 | */ |
||
87 | 22 | private function buildFactory(string $className) : callable |
|
108 | |||
109 | /** |
||
110 | * @param string $className |
||
111 | * |
||
112 | * @throws InvalidArgumentException |
||
113 | * @throws ReflectionException |
||
114 | */ |
||
115 | 22 | private function getReflectionClass($className) : ReflectionClass |
|
129 | |||
130 | /** |
||
131 | * @throws UnexpectedValueException |
||
132 | */ |
||
133 | 2 | private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, string $serializedString) : void |
|
155 | |||
156 | /** |
||
157 | * @throws UnexpectedValueException |
||
158 | */ |
||
159 | 2 | private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, string $serializedString) : void |
|
167 | |||
168 | 18 | private function isInstantiableViaReflection(ReflectionClass $reflectionClass) : bool |
|
172 | |||
173 | /** |
||
174 | * Verifies whether the given class is to be considered internal |
||
175 | */ |
||
176 | 18 | private function hasInternalAncestors(ReflectionClass $reflectionClass) : bool |
|
188 | |||
189 | /** |
||
190 | * Checks if a class is cloneable |
||
191 | * |
||
192 | * Classes implementing `__clone` cannot be safely cloned, as that may cause side-effects. |
||
193 | */ |
||
194 | 17 | private function isSafeToClone(ReflectionClass $reflection) : bool |
|
198 | } |
||
199 |