Skip to content

Commit 016f09d

Browse files
committed
Deprecate overring the __sleep method of ClassMetadata or PersistentCollection
1 parent bf2e243 commit 016f09d

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

src/Mapping/ClassMetadata.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use ProxyManager\Proxy\GhostObjectInterface;
3535
use ReflectionClass;
3636
use ReflectionEnum;
37+
use ReflectionMethod;
3738
use ReflectionNamedType;
3839
use ReflectionProperty;
3940
use Symfony\Component\Uid\UuidV1;
@@ -2640,12 +2641,36 @@ public function mapField(array $mapping): array
26402641
* That means any metadata properties that are not set or empty or simply have
26412642
* their default value are NOT serialized.
26422643
*
2643-
* Parts that are also NOT serialized because they cannot be properly unserialized:
2644-
* - reflClass (ReflectionClass)
2645-
* - reflFields (ReflectionProperty array)
2646-
* - propertyAccessors (ReflectionProperty array)
2644+
* Parts that are also NOT serialized because they cannot be properly unserialized:
2645+
* - reflClass (ReflectionClass)
2646+
* - reflFields (ReflectionProperty array)
2647+
* - propertyAccessors (ReflectionProperty array)
26472648
*
2648-
* @return array The names of all the fields that should be serialized.
2649+
* @return array<string, mixed> The serialized data.
2650+
*/
2651+
public function __serialize(): array
2652+
{
2653+
if (new ReflectionMethod($this, '__sleep')->getDeclaringClass() !== self::class) {
2654+
trigger_deprecation(
2655+
'doctrine/mongodb-odm',
2656+
'2.16',
2657+
'The method __sleep() is deprecated. Implement and use %s() instead.',
2658+
__METHOD__,
2659+
);
2660+
}
2661+
2662+
$data = [];
2663+
foreach ($this->__sleep() as $field) {
2664+
$data[$field] = $this->$field;
2665+
}
2666+
2667+
return $data;
2668+
}
2669+
2670+
/**
2671+
* @deprecated
2672+
*
2673+
* @return list<string> The names of all the fields that should be serialized.
26492674
*/
26502675
public function __sleep()
26512676
{
@@ -2700,7 +2725,7 @@ public function __sleep()
27002725
$serialized[] = 'isQueryResultDocument';
27012726
}
27022727

2703-
if ($this->isView()) {
2728+
if ($this->isView) {
27042729
$serialized[] = 'isView';
27052730
$serialized[] = 'rootClass';
27062731
}
@@ -2745,8 +2770,20 @@ public function __sleep()
27452770
}
27462771

27472772
/**
2748-
* Restores some state that cannot be serialized/unserialized.
2773+
* Restores the serialized values and some state that cannot be serialized/unserialized.
2774+
*
2775+
* @param array<string, mixed> $data The serialized data.
27492776
*/
2777+
public function __unserialize(array $data): void
2778+
{
2779+
foreach ($data as $field => $value) {
2780+
$this->$field = $value;
2781+
}
2782+
2783+
$this->__wakeup();
2784+
}
2785+
2786+
/** @deprecated */
27502787
public function __wakeup(): void
27512788
{
27522789
// Restore ReflectionClass and properties

src/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,23 @@ public function slice($offset, $length = null)
509509
* Called by PHP when this collection is serialized. Ensures that the
510510
* internal state of the collection can be reproduced after serialization
511511
*
512+
* @return array<string, mixed>
513+
*/
514+
public function __serialize(): array
515+
{
516+
return [
517+
'coll' => $this->coll,
518+
'initialized' => $this->initialized,
519+
'mongoData' => $this->mongoData,
520+
'snapshot' => $this->snapshot,
521+
'isDirty' => $this->isDirty,
522+
'hints' => $this->hints,
523+
];
524+
}
525+
526+
/**
527+
* @deprecated Implement and use __serialize() instead.
528+
*
512529
* @return string[]
513530
*/
514531
public function __sleep()

0 commit comments

Comments
 (0)