Skip to content

Commit fc51414

Browse files
committed
[VarExporter] Fix exporting classes with __serialize() but not __unserialize()
1 parent 0c1e73e commit fc51414

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
8383
throw new \TypeError($class.'::__serialize() must return an array');
8484
}
8585

86+
if (!$reflector->hasMethod('__unserialize')) {
87+
$serializeProperties = $properties;
88+
$properties = [];
89+
foreach ($serializeProperties as $n => $v) {
90+
$c = \PHP_VERSION_ID >= 80100 && $reflector->hasProperty($n) && ($p = $reflector->getProperty($n))->isReadOnly() ? $p->class : 'stdClass';
91+
$properties[$c][$n] = $v;
92+
}
93+
}
94+
8695
goto prepare_value;
8796
}
8897

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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\\__SerializeButNo__Unserialize'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\__SerializeButNo__Unserialize')),
6+
],
7+
null,
8+
[
9+
'stdClass' => [
10+
'foo' => [
11+
'ccc',
12+
],
13+
],
14+
],
15+
$o[0],
16+
[]
17+
);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ public static function provideExport()
247247

248248
yield ['__unserialize-but-no-__serialize', new __UnserializeButNo__Serialize()];
249249

250+
yield ['__serialize-but-no-__unserialize', new __SerializeButNo__Unserialize()];
251+
250252
if (\PHP_VERSION_ID < 80100) {
251253
return;
252254
}
@@ -470,3 +472,20 @@ public function __unserialize(array $data): void
470472
$this->foo = $data['foo'];
471473
}
472474
}
475+
476+
class __SerializeButNo__Unserialize
477+
{
478+
public $foo;
479+
480+
public function __construct()
481+
{
482+
$this->foo = 'ccc';
483+
}
484+
485+
public function __serialize(): array
486+
{
487+
return [
488+
'foo' => $this->foo,
489+
];
490+
}
491+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy