summaryrefslogtreecommitdiff
path: root/vendor/symfony/var-exporter/Instantiator.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/var-exporter/Instantiator.php
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/var-exporter/Instantiator.php')
-rw-r--r--vendor/symfony/var-exporter/Instantiator.php59
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
12namespace Symfony\Component\VarExporter;
13
14use Symfony\Component\VarExporter\Exception\ExceptionInterface;
15use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;
16use 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 */
23final 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}