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

Skip to content

Commit b36172c

Browse files
committed
feature #28417 [VarExporter] add Instantiator::instantiate() to create+populate objects without calling their constructor nor any other methods (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [VarExporter] add Instantiator::instantiate() to create+populate objects without calling their constructor nor any other methods | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - A blend of features also provided by https://github.com/doctrine/instantiator and https://github.com/Ocramius/GeneratedHydrator in one simple method. Because it's just a few more lines on top of the existing code infrastructure in the component :) For example, from the docblock: ```php // creates an empty instance of Foo Instantiator::instantiate(Foo::class); // creates a Foo instance and sets one of its public, protected or private properties Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]); // creates a Foo instance and sets a private property defined on its parent Bar class Instantiator::instantiate(Foo::class, [], [ Bar::class => ['privateBarProperty' => $propertyValue], ]); ``` Instances of ArrayObject, ArrayIterator and SplObjectHash can be created by using the special `"\0"` property name to define their internal value: ```php // creates an SplObjectHash where $info1 is attached to $obj1, etc. Instantiator::instantiate(SplObjectStorage::class, ["\0" => [$obj1, $info1, $obj2, $info2...]]); // creates an ArrayObject populated with $inputArray Instantiator::instantiate(ArrayObject::class, ["\0" => [$inputArray, $optionalFlag]]); ``` Misses some tests for now, but reuses the existing code infrastructure used to "unserialize" objects. Commits ------- d9bade0 [VarExporter] add Instantiator::instantiate() to create+populate objects without calling their constructor nor any other methods
2 parents 9db435e + d9bade0 commit b36172c

File tree

3 files changed

+172
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)