Skip to content

Commit 2aedd16

Browse files
bug #48724 [VarExporter] Fix exporting classes with __unserialize() but not __serialize() (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [VarExporter] Fix exporting classes with __unserialize() but not __serialize() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #48706 indirectly | License | MIT | Doc PR | - This is an edge case not supported yet, highlighted by our own symfony/expression-language GetAttrNode class. Commits ------- 6ccb85e [VarExporter] Fix exporting classes with __unserialize() but not __serialize()
2 parents f3b892d + 6ccb85e commit 2aedd16

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
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)];
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)
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