Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e5d0442

Browse filesBrowse files
[VarExporter] add Instantiator::instantiate() to create+populate objects without calling their constructor nor any other methods
1 parent deae538 commit e5d0442
Copy full SHA for e5d0442

File tree

Expand file treeCollapse file tree

3 files changed

+104
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+104
-1
lines changed
+100Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\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 <p@tchwork.com>
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 public, protected or private 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([
39+
* Foo::class => [],
40+
* Bar::class => ['privatePropertyName' => $propertyValue],
41+
* ]);
42+
*
43+
* Instances of ArrayObject, ArrayIterator and SplObjectHash can be created
44+
* by using the special "\0" property name to define their internal value:
45+
*
46+
* // creates an SplObjectHash where $info1 is attached to $obj1, etc.
47+
* Instantiator::instantiate([SplObjectStorage::class => ["\0" => [$obj1, $info1, $obj2, $info2...]]]);
48+
*
49+
* // creates an ArrayObject populated with $inputArray
50+
* Instantiator::instantiate([ArrayObject::class => ["\0" => $inputArray]]);
51+
*
52+
* @param array $propertiesByClass The properties to set on the object, keyed by the class to bind to
53+
* when setting them. The first key of the array defines the class
54+
* of the returned instance.
55+
*
56+
* @return object The created instance
57+
*
58+
* @throws ExceptionInterface When the instance cannot be created
59+
*/
60+
public static function instantiate(array $propertiesByClass)
61+
{
62+
if (!$propertiesByClass) {
63+
throw new \ReflectionException('No class provided.');
64+
}
65+
66+
foreach ($propertiesByClass as $class => $properties) {
67+
break;
68+
}
69+
70+
$reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class);
71+
72+
if (Registry::$cloneable[$class]) {
73+
$wrappedInstance = array(clone Registry::$prototypes[$class]);
74+
} elseif (Registry::$instantiableWithoutConstructor[$class]) {
75+
$wrappedInstance = array($reflector->newInstanceWithoutConstructor());
76+
} elseif (null === Registry::$prototypes[$class]) {
77+
throw new NotInstantiableTypeException($class);
78+
} elseif ($reflector->implementsInterface('Serializable')) {
79+
$wrappedInstance = array(unserialize('C:'.\strlen($class).':"'.$class.'":0:{}'));
80+
} else {
81+
$wrappedInstance = array(unserialize('O:'.\strlen($class).':"'.$class.'":0:{}'));
82+
}
83+
84+
$instance = array($instance);
85+
86+
foreach ($propertiesByClass as $class => $properties) {
87+
if (!$properties) {
88+
continue;
89+
}
90+
foreach ($properties as $name => $value) {
91+
// because they're also used for "unserialization", hydrators
92+
// deal with array of instances, so we need to wrap values
93+
$properties[$name] = array($value);
94+
}
95+
(Hydrator::$hydrators[$class] ?? Hydrator::getHydrator($class))($properties, $wrappedInstance);
96+
}
97+
98+
return $wrappedInstance[0];
99+
}
100+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/Internal/Hydrator.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/README.md
+3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.