Skip to content

Commit 6ccb85e

Browse files
fancywebnicolas-grekas
authored andcommitted
[VarExporter] Fix exporting classes with __unserialize() but not __serialize()
1 parent 3ee3ddc commit 6ccb85e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
149149
}
150150
if (null !== $sleep) {
151151
if (!isset($sleep[$n]) || ($i && $c !== $class)) {
152+
unset($arrayValue[$name]);
152153
continue;
153154
}
154155
$sleep[$n] = false;
@@ -164,6 +165,9 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
164165
}
165166
}
166167
}
168+
if (method_exists($class, '__unserialize')) {
169+
$properties = $arrayValue;
170+
}
167171

168172
prepare_value:
169173
$objectsPool[$value] = [$id = \count($objectsPool)];
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

+17
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)