Skip to content

Commit 12182ee

Browse files
committed
Replace __sleep/__wakeup with __serialize/__unserialize
https://wiki.php.net/rfc/soft-deprecate-sleep-wakeup
1 parent 310f9bc commit 12182ee

File tree

2 files changed

+72
-61
lines changed

2 files changed

+72
-61
lines changed

src/Mapping/ClassMetadata.php

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,111 +2538,115 @@ public function mapField(array $mapping): array
25382538
* - reflFields (ReflectionProperty array)
25392539
* - propertyAccessors (ReflectionProperty array)
25402540
*
2541-
* @return array The names of all the fields that should be serialized.
2542-
*/
2543-
public function __sleep(): array
2544-
{
2545-
// This metadata is always serialized/cached.
2546-
$serialized = [
2547-
'fieldMappings',
2548-
'associationMappings',
2549-
'identifier',
2550-
'name',
2551-
'db',
2552-
'collection',
2553-
'readPreference',
2554-
'readPreferenceTags',
2555-
'writeConcern',
2556-
'rootDocumentName',
2557-
'generatorType',
2558-
'generatorOptions',
2559-
'idGenerator',
2560-
'indexes',
2561-
'shardKey',
2562-
'timeSeriesOptions',
2541+
* @return array<string, mixed>
2542+
*/
2543+
public function __serialize(): array
2544+
{
2545+
$data = [
2546+
'fieldMappings' => $this->fieldMappings,
2547+
'associationMappings' => $this->associationMappings,
2548+
'identifier' => $this->identifier,
2549+
'name' => $this->name,
2550+
'db' => $this->db,
2551+
'collection' => $this->collection,
2552+
'readPreference' => $this->readPreference,
2553+
'readPreferenceTags' => $this->readPreferenceTags,
2554+
'writeConcern' => $this->writeConcern,
2555+
'rootDocumentName' => $this->rootDocumentName,
2556+
'generatorType' => $this->generatorType,
2557+
'generatorOptions' => $this->generatorOptions,
2558+
'idGenerator' => $this->idGenerator,
2559+
'indexes' => $this->indexes,
2560+
'shardKey' => $this->shardKey,
2561+
'timeSeriesOptions' => $this->timeSeriesOptions,
25632562
];
25642563

2565-
// The rest of the metadata is only serialized if necessary.
25662564
if ($this->changeTrackingPolicy !== self::CHANGETRACKING_DEFERRED_IMPLICIT) {
2567-
$serialized[] = 'changeTrackingPolicy';
2565+
$data['changeTrackingPolicy'] = $this->changeTrackingPolicy;
25682566
}
25692567

25702568
if ($this->customRepositoryClassName) {
2571-
$serialized[] = 'customRepositoryClassName';
2569+
$data['customRepositoryClassName'] = $this->customRepositoryClassName;
25722570
}
25732571

25742572
if ($this->inheritanceType !== self::INHERITANCE_TYPE_NONE || $this->discriminatorField !== null) {
2575-
$serialized[] = 'inheritanceType';
2576-
$serialized[] = 'discriminatorField';
2577-
$serialized[] = 'discriminatorValue';
2578-
$serialized[] = 'discriminatorMap';
2579-
$serialized[] = 'defaultDiscriminatorValue';
2580-
$serialized[] = 'parentClasses';
2581-
$serialized[] = 'subClasses';
2573+
$data['inheritanceType'] = $this->inheritanceType;
2574+
$data['discriminatorField'] = $this->discriminatorField;
2575+
$data['discriminatorValue'] = $this->discriminatorValue;
2576+
$data['discriminatorMap'] = $this->discriminatorMap;
2577+
$data['defaultDiscriminatorValue'] = $this->defaultDiscriminatorValue;
2578+
$data['parentClasses'] = $this->parentClasses;
2579+
$data['subClasses'] = $this->subClasses;
25822580
}
25832581

25842582
if ($this->isMappedSuperclass) {
2585-
$serialized[] = 'isMappedSuperclass';
2583+
$data['isMappedSuperclass'] = $this->isMappedSuperclass;
25862584
}
25872585

25882586
if ($this->isEmbeddedDocument) {
2589-
$serialized[] = 'isEmbeddedDocument';
2587+
$data['isEmbeddedDocument'] = $this->isEmbeddedDocument;
25902588
}
25912589

25922590
if ($this->isQueryResultDocument) {
2593-
$serialized[] = 'isQueryResultDocument';
2591+
$data['isQueryResultDocument'] = $this->isQueryResultDocument;
25942592
}
25952593

2596-
if ($this->isView()) {
2597-
$serialized[] = 'isView';
2598-
$serialized[] = 'rootClass';
2594+
if ($this->isView) {
2595+
$data['isView'] = $this->isView;
2596+
$data['rootClass'] = $this->rootClass;
25992597
}
26002598

26012599
if ($this->isFile) {
2602-
$serialized[] = 'isFile';
2603-
$serialized[] = 'bucketName';
2604-
$serialized[] = 'chunkSizeBytes';
2600+
$data['isFile'] = $this->isFile;
2601+
$data['bucketName'] = $this->bucketName;
2602+
$data['chunkSizeBytes'] = $this->chunkSizeBytes;
26052603
}
26062604

26072605
if ($this->isVersioned) {
2608-
$serialized[] = 'isVersioned';
2609-
$serialized[] = 'versionField';
2606+
$data['isVersioned'] = $this->isVersioned;
2607+
$data['versionField'] = $this->versionField;
26102608
}
26112609

26122610
if ($this->isLockable) {
2613-
$serialized[] = 'isLockable';
2614-
$serialized[] = 'lockField';
2611+
$data['isLockable'] = $this->isLockable;
2612+
$data['lockField'] = $this->lockField;
26152613
}
26162614

26172615
if ($this->lifecycleCallbacks) {
2618-
$serialized[] = 'lifecycleCallbacks';
2616+
$data['lifecycleCallbacks'] = $this->lifecycleCallbacks;
26192617
}
26202618

26212619
if ($this->collectionCapped) {
2622-
$serialized[] = 'collectionCapped';
2623-
$serialized[] = 'collectionSize';
2624-
$serialized[] = 'collectionMax';
2620+
$data['collectionCapped'] = $this->collectionCapped;
2621+
$data['collectionSize'] = $this->collectionSize;
2622+
$data['collectionMax'] = $this->collectionMax;
26252623
}
26262624

26272625
if ($this->isReadOnly) {
2628-
$serialized[] = 'isReadOnly';
2626+
$data['isReadOnly'] = $this->isReadOnly;
26292627
}
26302628

26312629
if ($this->validator !== null) {
2632-
$serialized[] = 'validator';
2633-
$serialized[] = 'validationAction';
2634-
$serialized[] = 'validationLevel';
2630+
$data['validator'] = $this->validator;
2631+
$data['validationAction'] = $this->validationAction;
2632+
$data['validationLevel'] = $this->validationLevel;
26352633
}
26362634

2637-
return $serialized;
2635+
return $data;
26382636
}
26392637

26402638
/**
2641-
* Restores some state that cannot be serialized/unserialized.
2639+
* Restores state after unserialization.
2640+
*
2641+
* @param array<string, mixed> $data
26422642
*/
2643-
public function __wakeup(): void
2643+
public function __unserialize(array $data): void
26442644
{
2645-
// Restore ReflectionClass and properties
2645+
foreach ($data as $property => $value) {
2646+
$this->$property = $value;
2647+
}
2648+
2649+
// Restore ReflectionClass and properties that are not part of the serialized payload
26462650
$this->reflClass = new ReflectionClass($this->name);
26472651
$this->instantiator = new Instantiator();
26482652
$this->reflectionService = new RuntimeReflectionService();

src/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,18 @@ public function slice(int $offset, int|null $length = null): array
547547
* Called by PHP when this collection is serialized. Ensures that the
548548
* internal state of the collection can be reproduced after serialization
549549
*
550-
* @return string[]
550+
* @return array<string, mixed>
551551
*/
552-
public function __sleep(): array
553-
{
554-
return ['coll', 'initialized', 'mongoData', 'snapshot', 'isDirty', 'hints'];
552+
public function __serialize(): array
553+
{
554+
return [
555+
'coll' => $this->coll,
556+
'initialized' => $this->initialized,
557+
'mongoData' => $this->mongoData,
558+
'snapshot' => $this->snapshot,
559+
'isDirty' => $this->isDirty,
560+
'hints' => $this->hints,
561+
];
555562
}
556563

557564
/* ArrayAccess implementation */

0 commit comments

Comments
 (0)