Permalink
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()
  • Loading branch information...
nicolas-grekas committed Nov 14, 2018
2 parents bc03c1b + 46e2ecd commit 100f2056b7f49937ef716a33afd2330f8687c028
@@ -119,11 +119,10 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
$proto = (array) $proto;
foreach ($arrayValue as $name => $v) {
$i = 0;
$n = (string) $name;
if ('' === $n || "\0" !== $n[0]) {
$c = 'stdClass';
$properties[$c][$n] = $v;
unset($sleep[$n]);
} elseif ('*' === $n[1]) {
$n = substr($n, 3);
$c = $reflector->getProperty($n)->class;
@@ -132,26 +131,26 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
} elseif ('Exception' === $c) {
$c = 'ErrorException';
}
$properties[$c][$n] = $v;
unset($sleep[$n]);
} else {
$i = strpos($n, "\0", 2);
$c = substr($n, 1, $i - 1);
$n = substr($n, 1 + $i);
if (null === $sleep) {
$properties[$c][$n] = $v;
} elseif (isset($sleep[$n]) && $c === $class) {
$properties[$c][$n] = $v;
unset($sleep[$n]);
}
if (null !== $sleep) {
if (!isset($sleep[$n]) || ($i && $c !== $class)) {
continue;
}
$sleep[$n] = false;
}
if (\array_key_exists($name, $proto) && $proto[$name] === $v) {
unset($properties[$c][$n]);
if (!\array_key_exists($name, $proto) || $proto[$name] !== $v) {
$properties[$c][$n] = $v;
}
}
if ($sleep) {
foreach ($sleep as $n => $v) {
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), E_USER_NOTICE);
if (false !== $v) {
trigger_error(sprintf('serialize(): "%s" returned as member variable from __sleep() but does not exist', $n), E_USER_NOTICE);
}
}
}
@@ -0,0 +1,13 @@
<?php
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
$o = [
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\MyWakeup'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\MyWakeup')),
],
null,
[],
$o[0],
[
1 => 0,
]
);
@@ -12,9 +12,6 @@
$o[1],
123,
],
'bis' => [
1 => 123,
],
'baz' => [
1 => 123,
],
@@ -97,6 +97,9 @@ public function testExport(string $testName, $value, bool $staticValueExpected =
$marshalledValue = include $fixtureFile;
if (!$isStaticValue) {
if ($value instanceof MyWakeup) {
$value->bis = null;
}
$this->assertDumpEquals($value, $marshalledValue);
} else {
$this->assertSame($value, $marshalledValue);
@@ -184,6 +187,11 @@ public function provideExport()
yield array('final-array-iterator', new FinalArrayIterator());
yield array('final-stdclass', new FinalStdClass());
$value = new MyWakeup();
$value->bis = new \ReflectionClass($value);
yield array('wakeup-refl', $value);
}
}

0 comments on commit 100f205

Please sign in to comment.