|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\VarExporter; |
| 13 | + |
| 14 | +use Symfony\Component\VarExporter\Exception\ExceptionInterface; |
| 15 | +use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException; |
| 16 | +use Symfony\Component\VarExporter\Internal\Hydrator; |
| 17 | +use Symfony\Component\VarExporter\Internal\Registry; |
| 18 | + |
| 19 | +/** |
| 20 | + * A utility class to create objects without calling their constructor. |
| 21 | + * |
| 22 | + * @author Nicolas Grekas <[email protected]> |
| 23 | + */ |
| 24 | +final class Instantiator |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Creates an object and sets its properties without calling its constructor nor any other methods. |
| 28 | + * |
| 29 | + * For example: |
| 30 | + * |
| 31 | + * // creates an empty instance of Foo |
| 32 | + * Instantiator::instantiate(Foo::class); |
| 33 | + * |
| 34 | + * // creates a Foo instance and sets one of its properties |
| 35 | + * Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]); |
| 36 | + * |
| 37 | + * // creates a Foo instance and sets a private property defined on its parent Bar class |
| 38 | + * Instantiator::instantiate(Foo::class, [], [ |
| 39 | + * Bar::class => ['privateBarProperty' => $propertyValue], |
| 40 | + * ]); |
| 41 | + * |
| 42 | + * Instances of ArrayObject, ArrayIterator and SplObjectHash can be created |
| 43 | + * by using the special "\0" property name to define their internal value: |
| 44 | + * |
| 45 | + * // creates an SplObjectHash where $info1 is attached to $obj1, etc. |
| 46 | + * Instantiator::instantiate(SplObjectStorage::class, ["\0" => [$obj1, $info1, $obj2, $info2...]]); |
| 47 | + * |
| 48 | + * // creates an ArrayObject populated with $inputArray |
| 49 | + * Instantiator::instantiate(ArrayObject::class, ["\0" => [$inputArray]]); |
| 50 | + * |
| 51 | + * @param string $class The class of the instance to create |
| 52 | + * @param array $properties The properties to set on the instance |
| 53 | + * @param array $privateProperties The private properties to set on the instance, |
| 54 | + * keyed by their declaring class |
| 55 | + * |
| 56 | + * @return object The created instance |
| 57 | + * |
| 58 | + * @throws ExceptionInterface When the instance cannot be created |
| 59 | + */ |
| 60 | + public static function instantiate(string $class, array $properties = array(), array $privateProperties = array()) |
| 61 | + { |
| 62 | + $reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class); |
| 63 | + |
| 64 | + if (Registry::$cloneable[$class]) { |
| 65 | + $wrappedInstance = array(clone Registry::$prototypes[$class]); |
| 66 | + } elseif (Registry::$instantiableWithoutConstructor[$class]) { |
| 67 | + $wrappedInstance = array($reflector->newInstanceWithoutConstructor()); |
| 68 | + } elseif (null === Registry::$prototypes[$class]) { |
| 69 | + throw new NotInstantiableTypeException($class); |
| 70 | + } elseif ($reflector->implementsInterface('Serializable')) { |
| 71 | + $wrappedInstance = array(unserialize('C:'.\strlen($class).':"'.$class.'":0:{}')); |
| 72 | + } else { |
| 73 | + $wrappedInstance = array(unserialize('O:'.\strlen($class).':"'.$class.'":0:{}')); |
| 74 | + } |
| 75 | + |
| 76 | + if ($properties) { |
| 77 | + $privateProperties[$class] = isset($privateProperties[$class]) ? $properties + $privateProperties[$class] : $properties; |
| 78 | + } |
| 79 | + |
| 80 | + foreach ($privateProperties as $class => $properties) { |
| 81 | + if (!$properties) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + foreach ($properties as $name => $value) { |
| 85 | + // because they're also used for "unserialization", hydrators |
| 86 | + // deal with array of instances, so we need to wrap values |
| 87 | + $properties[$name] = array($value); |
| 88 | + } |
| 89 | + (Hydrator::$hydrators[$class] ?? Hydrator::getHydrator($class))($properties, $wrappedInstance); |
| 90 | + } |
| 91 | + |
| 92 | + return $wrappedInstance[0]; |
| 93 | + } |
| 94 | +} |
0 commit comments