Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b157f1d

Browse files
[VarExporter] add Instantiator::instantiate() to create+populate objects without calling their constructor nor any other methods
1 parent 3b931fe commit b157f1d

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\Internal\Hydrator;
15+
use Symfony\Component\VarExporter\Internal\Registry;
16+
17+
/**
18+
* A utility class to create objects without calling their constructor.
19+
*
20+
* @author Nicolas Grekas <[email protected]>
21+
*/
22+
final class Instantiator
23+
{
24+
/**
25+
* Creates an object and sets its properties without calling its constructor nor any other methods.
26+
*
27+
* For example:
28+
*
29+
* // creates an empty instance of Foo
30+
* Instantiator::instantiate([Foo::class => []]);
31+
*
32+
* // creates a Foo instance and sets one of its public, protected or private properties
33+
* Instantiator::instantiate([Foo::class => ['propertyName' => $propertyValue]]);
34+
*
35+
* // creates a Foo instance and sets a private property defined on its parent Bar class
36+
* Instantiator::instantiate([
37+
* Foo::class => [],
38+
* Bar::class => ['privatePropertyName' => $propertyValue],
39+
* ]);
40+
*
41+
* Instances of ArrayObject, ArrayIterator and SplObjectHash can be created
42+
* by using the special "\0" property name to define their internal value:
43+
*
44+
* // creates an SplObjectHash where $info1 is attached to $obj1, etc.
45+
* Instantiator::instantiate([SplObjectStorage::class => ["\0" => [$obj1, $info1, $obj2, $info2...]]]);
46+
*
47+
* // creates an ArrayObject pupolated with $inputArray
48+
* Instantiator::instantiate([ArrayObject::class => ["\0" => $inputArray]]);
49+
*
50+
* @param array $propertiesByClass The properties to set on the object, keyed by the class to bind to
51+
* when setting them. The first key of the array defines the class
52+
* of the returned instance.
53+
*
54+
* @return object An instance of the class defined by the first key of $propertiesByClass
55+
*
56+
* @throws \ReflectionException When the instance cannot be created
57+
*/
58+
public static function instantiate(array $propertiesByClass)
59+
{
60+
if (!$propertiesByClass) {
61+
throw new \ReflectionException('No class provided.');
62+
}
63+
64+
foreach ($propertiesByClass as $class => $properties) {
65+
break;
66+
}
67+
68+
try {
69+
$reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class);
70+
} catch (\Exception $e) {
71+
if (0 === strpos($e->getMessage(), 'Serialization of ')) {
72+
$e = new \ReflectionException(substr_replace($e->getMessage(), 'Instanti', 0, 8));
73+
}
74+
75+
throw $e;
76+
}
77+
78+
if (Registry::$cloneable[$class]) {
79+
$instance = clone Registry::$prototypes[$class];
80+
} elseif (Registry::$instantiableWithoutConstructor[$class]) {
81+
$instance = $reflector->newInstanceWithoutConstructor();
82+
} elseif (null === Registry::$prototypes[$class]) {
83+
throw new \ReflectionException(sprintf("Instantiation of '%s' is not allowed", $class));
84+
} elseif ($reflector->implementsInterface('Serializable')) {
85+
$instance = unserialize('C:'.\strlen($class).':"'.$class.'":0:{}');
86+
} else {
87+
$instance = unserialize('O:'.\strlen($class).':"'.$class.'":0:{}');
88+
}
89+
90+
$instance = array($instance);
91+
92+
foreach ($propertiesByClass as $class => $properties) {
93+
if (!$properties) {
94+
continue;
95+
}
96+
foreach ($properties as $name => $value) {
97+
$properties[$name] = array($value);
98+
}
99+
(Hydrator::$hydrators[$class] ?? Hydrator::getHydrator($class))($properties, $instance);
100+
}
101+
102+
return $instance[0];
103+
}
104+
}

src/Symfony/Component/VarExporter/Internal/Hydrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static function getHydrator($class)
7878
}
7979
}
8080
}
81-
foreach ($properties["\0"] as $i => $v) {
81+
foreach ($properties["\0"] ?? array() as $i => $v) {
8282
$constructor->invokeArgs($objects[$i], $v);
8383
}
8484
};

src/Symfony/Component/VarExporter/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The VarExporter component allows exporting any serializable PHP data structure t
55
plain PHP code. While doing so, it preserves all the semantics associated with
66
the serialization mechanism of PHP (`__wakeup`, `__sleep`, `Serializable`).
77

8+
It also provides an instantiator that allows creating and populating objects
9+
without calling their constructor nor any other methods.
10+
811
The reason to use this component *vs* `serialize()` or
912
[igbinary](https://github.com/igbinary/igbinary) is performance: thanks to
1013
OPcache, the resulting code is significantly faster and more memory efficient

0 commit comments

Comments
 (0)