bug #31020 [VarExporter] fix exporting classes with private constructors (nicolas-grekas)
This PR was merged into the 4.2 branch.
Discussion
----------
[VarExporter] fix exporting classes with private constructors
| Q | A
| ------------- | ---
| Branch? | 4.2
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Reported by @theofidry
Commits
-------
e354d54e7e
[VarExporter] fix exporting classes with private constructors
This commit is contained in:
commit
75b1157633
@ -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);
|
||||
|
@ -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],
|
||||
[]
|
||||
);
|
@ -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;
|
||||
|
Reference in New Issue
Block a user