10000 [VarExporter] Fix exporting classes with __unserialize() but not __se… · symfony/symfony@16d0b26 · GitHub
[go: up one dir, main page]

Skip to content

Commit 16d0b26

Browse files
committed
[VarExporter] Fix exporting classes with __unserialize() but not __serialize()
1 parent 3ee3ddc commit 16d0b26

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Symfony/Component/VarExporter/Internal/Exporter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
114114
$objectsPool[$value] = [$id = \count($objectsPool), serialize($value), [], 0];
115115
$value = new Reference($id);
116116
goto handle_value;
117+
} elseif ($reflector->hasMethod('__unserialize')) {
118+
$properties = (array) $value;
119+
goto prepare_value;
117120
} else {
118121
if (method_exists($class, '__sleep')) {
119122
if (!\is_array($sleep = $value->__sleep())) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
4+
$o = [
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\__UnserializeButNo__Serialize'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\__UnserializeButNo__Serialize')),
6+
],
7+
null,
8+
[],
9+
$o[0],
10+
[
11+
[
12+
'foo' => 'ccc',
13+
],
14+
]
15+
);

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ public function provideExport()
245245

246246
yield ['php74-serializable', new Php74Serializable()];
247247

248+
yield ['__unserialize-but-no-__serialize', new __UnserializeButNo__Serialize()];
249+
248250
if (\PHP_VERSION_ID < 80100) {
249251
return;
250252
}
@@ -453,3 +455,18 @@ public function unserialize($ser)
453455
class ArrayObject extends \ArrayObject
454456
{
455457
}
458+
459+
class __UnserializeButNo__Serialize
460+
{
461+
public $foo;
462+
463+
public function __construct()
464+
{
465+
$this->foo = 'ccc';
466+
}
467+
468+
public function __unserialize(array $data): void
469+
{
470+
$this->foo = $data['foo'];
471+
}
472+
}

0 commit comments

Comments
 (0)
0