[VarExporter] fix exporting classes with private constructors

This commit is contained in:
Nicolas Grekas 2019-04-08 20:04:40 +02:00
parent ec41d76624
commit e354d54e7e
3 changed files with 36 additions and 2 deletions

View File

@ -65,14 +65,14 @@ class Registry
public static function getClassReflector($class, $instantiableWithoutConstructor = false, $cloneable = null)
{
if (!\class_exists($class) && !\interface_exists($class, false) && !\trait_exists($class, false)) {
if (!($isClass = \class_exists($class)) && !\interface_exists($class, false) && !\trait_exists($class, false)) {
throw new ClassNotFoundException($class);
}
$reflector = new \ReflectionClass($class);
if ($instantiableWithoutConstructor) {
$proto = $reflector->newInstanceWithoutConstructor();
} elseif (!$reflector->isInstantiable()) {
} elseif (!$isClass || $reflector->isAbstract()) {
throw new NotInstantiableTypeException($class);
} elseif ($reflector->name !== $class) {
$reflector = self::$reflectors[$name = $reflector->name] ?? self::getClassReflector($name, $instantiableWithoutConstructor, $cloneable);

View File

@ -0,0 +1,17 @@
<?php
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
$o = [
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\PrivateConstructor'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\PrivateConstructor')),
],
null,
[
'stdClass' => [
'prop' => [
'bar',
],
],
],
$o[0],
[]
);

View File

@ -197,6 +197,8 @@ class VarExporterTest extends TestCase
yield ['abstract-parent', new ConcreteClass()];
yield ['foo-serializable', new FooSerializable('bar')];
yield ['private-constructor', PrivateConstructor::create('bar')];
}
}
@ -250,6 +252,21 @@ class MyNotCloneable
}
}
class PrivateConstructor
{
public $prop;
public static function create($prop): self
{
return new self($prop);
}
private function __construct($prop)
{
$this->prop = $prop;
}
}
class MyPrivateValue
{
protected $prot;