Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 100f205

Browse files
bug #29209 [VarExporter] fix handling of __sleep() (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [VarExporter] fix handling of __sleep() | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29118 | License | MIT | Doc PR | - Commits ------- 46e2ecd [VarExporter] fix handling of __sleep()
2 parents bc03c1b + 46e2ecd commit 100f205

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
119119
$proto = (array) $proto;
120120

121121
foreach ($arrayValue as $name => $v) {
122+
$i = 0;
122123
$n = (string) $name;
123124
if ('' === $n || "\0" !== $n[0]) {
124125
$c = 'stdClass';
125-
$properties[$c][$n] = $v;
126-
unset($sleep[$n]);
127126
} elseif ('*' === $n[1]) {
128127
$n = substr($n, 3);
129128
$c = $reflector->getProperty($n)->class;
@@ -132,26 +131,26 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
132131
} elseif ('Exception' === $c) {
133132
$c = 'ErrorException';
134133
}
135-
$properties[$c][$n] = $v;
136-
unset($sleep[$n]);
137134
} else {
138135
$i = strpos($n, "\0", 2);
139136
$c = substr($n, 1, $i - 1);
140137
$n = substr($n, 1 + $i);
141-
if (null === $sleep) {
142-
$properties[$c][$n] = $v;
143-
} elseif (isset($sleep[$n]) && $c === $class) {
144-
$properties[$c][$n] = $v;
145-
unset($sleep[$n]);
138+
}
139+
if (null !== $sleep) {
140+
if (!isset($sleep[$n]) || ($i && $c !== $class)) {
141+
continue;
146142
}
143+
$sleep[$n] = false;
147144
}
148-
if (\array_key_exists($name, $proto) && $proto[$name] === $v) {
149-
unset($properties[$c][$n]);
145+
if (!\array_key_exists($name, $proto) || $proto[$name] !== $v) {
146+
$properties[$c][$n] = $v;
150147
}
151148
}
152149
if ($sleep) {
153150
foreach ($sleep as $n => $v) {
154-
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), E_USER_NOTICE);
151+
if (false !== $v) {
152+
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), E_USER_NOTICE);
153+
}
155154
}
156155
}
157156

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

src/Symfony/Component/VarExporter/Tests/Fixtures/wakeup.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
$o[1],
1313
123,
1414
],
15-
'bis' => [
16-
1 => 123,
17-
],
1815
'baz' => [
1916
1 => 123,
2017
],

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function testExport(string $testName, $value, bool $staticValueExpected =
9797
$marshalledValue = include $fixtureFile;
9898

9999
if (!$isStaticValue) {
100+
if ($value instanceof MyWakeup) {
101+
$value->bis = null;
102+
}
100103
$this->assertDumpEquals($value, $marshalledValue);
101104
} else {
102105
$this->assertSame($value, $marshalledValue);
@@ -184,6 +187,11 @@ public function provideExport()
184187
yield array('final-array-iterator', new FinalArrayIterator());
185188

186189
yield array('final-stdclass', new FinalStdClass());
190+
191+
$value = new MyWakeup();
192+
$value->bis = new \ReflectionClass($value);
193+
194+
yield array('wakeup-refl', $value);
187195
}
188196
}
189197

0 commit comments

Comments
 (0)