1 | <?php |
||
33 | final class Instantiator implements InstantiatorInterface |
||
34 | { |
||
35 | /** |
||
36 | * Markers used internally by PHP to define whether {@see \unserialize} should invoke |
||
37 | * the method {@see \Serializable::unserialize()} when dealing with classes implementing |
||
38 | * the {@see \Serializable} interface. |
||
39 | */ |
||
40 | const SERIALIZATION_FORMAT_USE_UNSERIALIZER = 'C'; |
||
41 | const SERIALIZATION_FORMAT_AVOID_UNSERIALIZER = 'O'; |
||
42 | |||
43 | /** |
||
44 | * @var \callable[] used to instantiate specific classes, indexed by class name |
||
45 | */ |
||
46 | private static $cachedInstantiators = []; |
||
47 | |||
48 | /** |
||
49 | * @var object[] of objects that can directly be cloned, indexed by class name |
||
50 | */ |
||
51 | private static $cachedCloneables = []; |
||
52 | |||
53 | /** |
||
54 | * {@inheritDoc} |
||
55 | */ |
||
56 | 40 | public function instantiate($className) |
|
70 | |||
71 | /** |
||
72 | * Builds the requested object and caches it in static properties for performance |
||
73 | * |
||
74 | * @param string $className |
||
75 | * |
||
76 | * @return object |
||
77 | */ |
||
78 | 22 | private function buildAndCacheFromFactory($className) |
|
89 | |||
90 | /** |
||
91 | * Builds a callable capable of instantiating the given $className without |
||
92 | * invoking its constructor. |
||
93 | * |
||
94 | * @param string $className |
||
95 | * |
||
96 | * @return callable |
||
97 | */ |
||
98 | 22 | private function buildFactory($className) |
|
119 | |||
120 | /** |
||
121 | * @param string $className |
||
122 | * |
||
123 | * @return ReflectionClass |
||
124 | * |
||
125 | * @throws InvalidArgumentException |
||
126 | */ |
||
127 | 22 | private function getReflectionClass($className) |
|
141 | |||
142 | /** |
||
143 | * @param ReflectionClass $reflectionClass |
||
144 | * @param string $serializedString |
||
145 | * |
||
146 | * @throws UnexpectedValueException |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | private function checkIfUnSerializationIsSupported(ReflectionClass $reflectionClass, $serializedString) |
||
170 | |||
171 | /** |
||
172 | * @param ReflectionClass $reflectionClass |
||
173 | * @param string $serializedString |
||
174 | * |
||
175 | * @throws UnexpectedValueException |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | 2 | private function attemptInstantiationViaUnSerialization(ReflectionClass $reflectionClass, $serializedString) |
|
189 | |||
190 | /** |
||
191 | * @param ReflectionClass $reflectionClass |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 18 | private function isInstantiableViaReflection(ReflectionClass $reflectionClass) |
|
199 | |||
200 | /** |
||
201 | * Verifies whether the given class is to be considered internal |
||
202 | * |
||
203 | * @param ReflectionClass $reflectionClass |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | 18 | private function hasInternalAncestors(ReflectionClass $reflectionClass) |
|
217 | |||
218 | /** |
||
219 | * Checks if a class is cloneable |
||
220 | * |
||
221 | * Classes implementing `__clone` cannot be safely cloned, as that may cause side-effects. |
||
222 | * |
||
223 | * @param ReflectionClass $reflection |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 17 | private function isSafeToClone(ReflectionClass $reflection) |
|
231 | } |
||
232 |