8000 [VarExporter] add Instantiator::instantiate() to create+populate obje… · symfony/symfony@77376e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77376e4

Browse files
[VarExporter] add Instantiator::instantiate() to create+populate objects without calling their constructor nor any other methods
1 parent 2879baf commit 77376e4

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 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+
}

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
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
16+
use Symfony\Component\VarExporter\Instantiator;
17+
18+
class InstantiatorTest extends TestCase
19+
{
20+
use VarDumperTestTrait;
21+
22+
/**
23+
* @expectedException \Symfony\Component\VarExporter\Exception\ClassNotFoundException
24+
* @expectedExceptionMessage Class "SomeNotExistingClass" not found.
25+
*/
26+
public function testNotFoundClass()
27+
{
28+
Instantiator::instantiate('SomeNotExistingClass');
29+
}
30+
31+
/**
32+
* @dataProvider provideFailingInstantiation
33+
* @expectedException \Symfony\Component\VarExporter\Exception\NotInstantiableTypeException
34+
* @expectedExceptionMessageRegexp Type ".*" is not instantiable.
35+
*/
36+
public function testFailingInstantiation(string $class)
37+
{
38+
Instantiator::instantiate($class);
39+
}
40+
41+
public function provideFailingInstantiation()
42+
{
43+
yield array('ReflectionClass');
44+
yield array('SplHeap');
45+
yield array('Throwable');
46+
yield array('Closure');
47+
yield array('SplFileInfo');
48+
}
49+
50+
public function testInstantiate()
51+
{
52+
$this->assertEquals((object) array('p' => 123), Instantiator::instantiate('stdClass', array('p' => 123)));
53+
$this->assertEquals((object) array('p' => 123), Instantiator::instantiate('STDcLASS', array('p' => 123)));
54+
$this->assertEquals(new \ArrayObject(array(123)), Instantiator::instantiate(\ArrayObject::class, array("\0" => array(array(123)))));
55+
56+
$expected = array(
57+
"\0".__NAMESPACE__."\Bar\0priv" => 123,
58+
"\0".__NAMESPACE__."\Foo\0priv" => 234,
59+
);
60+
61+
$this->assertSame($expected, (array) Instantiator::instantiate(Bar::class, array('priv' => 123), array(Foo::class => array('priv' => 234))));
62+
63+
$e = Instantiator::instantiate('Exception', array('foo' => 123, 'trace' => array(234)));
64+
65+
$this->assertSame(123, $e->foo);
66+
$this->assertSame(array(234), $e->getTrace());
67+
}
68+
}
69+
70+
class Foo
71+
{
72+
private $priv;
73+
}
74+
75+
class Bar extends Foo
76+
{
77+
private $priv;
78+
}

0 commit comments

Comments
 (0)
0