1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace Symfony\Component\VarExporter;
- use Symfony\Component\VarExporter\Exception\ExceptionInterface;
- use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
- use Symfony\Component\VarExporter\Internal\Hydrator;
- use Symfony\Component\VarExporter\Internal\Registry;
- final class Instantiator
- {
-
- public static function instantiate(string $class, array $properties = [], array $privateProperties = []): object
- {
- $reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class);
- if (Registry::$cloneable[$class]) {
- $wrappedInstance = [clone Registry::$prototypes[$class]];
- } elseif (Registry::$instantiableWithoutConstructor[$class]) {
- $wrappedInstance = [$reflector->newInstanceWithoutConstructor()];
- } elseif (null === Registry::$prototypes[$class]) {
- throw new NotInstantiableTypeException($class);
- } elseif ($reflector->implementsInterface('Serializable') && (\PHP_VERSION_ID < 70400 || !method_exists($class, '__unserialize'))) {
- $wrappedInstance = [unserialize('C:'.\strlen($class).':"'.$class.'":0:{}')];
- } else {
- $wrappedInstance = [unserialize('O:'.\strlen($class).':"'.$class.'":0:{}')];
- }
- if ($properties) {
- $privateProperties[$class] = isset($privateProperties[$class]) ? $properties + $privateProperties[$class] : $properties;
- }
- foreach ($privateProperties as $class => $properties) {
- if (!$properties) {
- continue;
- }
- foreach ($properties as $name => $value) {
-
-
- $properties[$name] = [$value];
- }
- (Hydrator::$hydrators[$class] ?? Hydrator::getHydrator($class))($properties, $wrappedInstance);
- }
- return $wrappedInstance[0];
- }
- }
|