10000 [VarExporter] fix exporting objects that mutate on __sleep() by nicolas-grekas · Pull Request #28371 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarExporter] fix exporting objects that mutate on __sleep() #28371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Component/VarExporter/Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
// Might throw Exception("Serialization of '...' is not allowed")
Registry::getClassReflector($class);
serialize(Registry::$prototypes[$class]);
if (\method_exists($class, '__sleep')) {
Registry::getClassReflector($class, Registry::$instantiableWithoutConstructor[$class], Registry::$cloneable[$class]);
}
}
$reflector = Registry::$reflectors[$class];
$proto = Registry::$prototypes[$class];
Expand Down Expand Up @@ -110,6 +113,11 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
$value = null;
goto handle_value;
}
foreach ($sleep as $name) {
if (\property_exists($value, $name) && !$reflector->hasProperty($name)) {
$arrayValue[$name] = $value->$name;
}
}
$sleep = array_flip($sleep);
}

Expand Down
11 changes: 3 additions & 8 deletions src/Symfony/Component/VarExporter/Internal/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,9 @@ public static function getClassReflector($class, $instantiableWithoutConstructor
}
}

if (null !== $cloneable) {
self::$prototypes[$class] = $proto;
self::$cloneable[$class] = $cloneable;

return self::$reflectors[$class] = $reflector;
}

if ($proto instanceof \Reflector || $proto instanceof \ReflectionGenerator || $proto instanceof \ReflectionType || $proto instanceof \IteratorIterator || $proto instanceof \RecursiveIteratorIterator) {
if (null !== self::$cloneable[$class] = $cloneable) {
// no-op
} elseif ($proto instanceof \Reflector || $proto instanceof \ReflectionGenerator || $proto instanceof \ReflectionType || $proto instanceof \IteratorIterator || $proto instanceof \RecursiveIteratorIterator) {
if (!$proto instanceof \Serializable && !\method_exists($proto, '__wakeup')) {
throw new \Exception(sprintf("Serialization of '%s' is not allowed", $class));
}
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/Fixtures/var-on-sleep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
$o = [
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes[\Symfony\Component\VarExporter\Tests\GoodNight::class] ?? \Symfony\Component\VarExporter\Internal\Registry::p(\Symfony\Component\VarExporter\Tests\GoodNight::class, true)),
],
null,
[
'*' => [
'good' => [
'night',
],
],
],
$o[0],
[]
);
18 changes: 16 additions & 2 deletions src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public function provideFailingSerialization()
*/
public function testMarshall(string $testName, $value, bool $staticValueExpected = false)
{
$serializedValue = serialize($value);
$dumpedValue = $this->getDump($value);
$isStaticValue = true;
$marshalledValue = VarExporter::export($value, $isStaticValue);

$this->assertSame($staticValueExpected, $isStaticValue);
$this->assertSame($serializedValue, serialize($value));
if ('var-on-sleep' !== $testName) {
$this->assertDumpEquals($dumpedValue, $value);
}

$dump = "<?php\n\nreturn ".$marshalledValue.";\n";
$dump = str_replace(var_export(__FILE__, true), "\\dirname(__DIR__).\\DIRECTORY_SEPARATOR.'VarExporterTest.php'", $dump);
Expand Down Expand Up @@ -165,6 +167,8 @@ public function provideMarshall()
$r->setValue($value, 234);

yield array('error', $value);

yield array('var-on-sleep', new GoodNight());
}
}

Expand Down Expand Up @@ -248,3 +252,13 @@ public function setFlags($flags)
throw new \BadMethodCallException('Calling MyArrayObject::setFlags() is forbidden');
}
}

class GoodNight
{
public function __sleep()
{
$this->good = 'night';

return array('good');
}
}
0