diff options
Diffstat (limited to 'vendor/symfony/var-exporter/Instantiator.php')
-rw-r--r-- | vendor/symfony/var-exporter/Instantiator.php | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/Instantiator.php b/vendor/symfony/var-exporter/Instantiator.php new file mode 100644 index 0000000..10200c0 --- /dev/null +++ b/vendor/symfony/var-exporter/Instantiator.php | |||
@@ -0,0 +1,59 @@ | |||
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 | |||
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\Registry; | ||
17 | |||
18 | /** | ||
19 | * A utility class to create objects without calling their constructor. | ||
20 | * | ||
21 | * @author Nicolas Grekas <p@tchwork.com> | ||
22 | */ | ||
23 | final class Instantiator | ||
24 | { | ||
25 | /** | ||
26 | * Creates an object and sets its properties without calling its constructor nor any other methods. | ||
27 | * | ||
28 | * @see Hydrator::hydrate() for examples | ||
29 | * | ||
30 | * @template T of object | ||
31 | * | ||
32 | * @param class-string<T> $class The class of the instance to create | ||
33 | * @param array<string, mixed> $properties The properties to set on the instance | ||
34 | * @param array<class-string, array<string, mixed>> $scopedProperties The properties to set on the instance, | ||
35 | * keyed by their declaring class | ||
36 | * | ||
37 | * @return T | ||
38 | * | ||
39 | * @throws ExceptionInterface When the instance cannot be created | ||
40 | */ | ||
41 | public static function instantiate(string $class, array $properties = [], array $scopedProperties = []): object | ||
42 | { | ||
43 | $reflector = Registry::$reflectors[$class] ??= Registry::getClassReflector($class); | ||
44 | |||
45 | if (Registry::$cloneable[$class]) { | ||
46 | $instance = clone Registry::$prototypes[$class]; | ||
47 | } elseif (Registry::$instantiableWithoutConstructor[$class]) { | ||
48 | $instance = $reflector->newInstanceWithoutConstructor(); | ||
49 | } elseif (null === Registry::$prototypes[$class]) { | ||
50 | throw new NotInstantiableTypeException($class); | ||
51 | } elseif ($reflector->implementsInterface('Serializable') && !method_exists($class, '__unserialize')) { | ||
52 | $instance = unserialize('C:'.\strlen($class).':"'.$class.'":0:{}'); | ||
53 | } else { | ||
54 | $instance = unserialize('O:'.\strlen($class).':"'.$class.'":0:{}'); | ||
55 | } | ||
56 | |||
57 | return $properties || $scopedProperties ? Hydrator::hydrate($instance, $properties, $scopedProperties) : $instance; | ||
58 | } | ||
59 | } | ||