Skip to content

Commit 5e95b63

Browse files
committed
Replace __sleep/__wakeup with __serialize/__unserialize
https://wiki.php.net/rfc/soft-deprecate-sleep-wakeup
1 parent a1d6e5e commit 5e95b63

File tree

3 files changed

+81
-60
lines changed

3 files changed

+81
-60
lines changed

src/Mapping/ClassMetadata.php

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,110 +2460,114 @@ public function mapField(array $mapping): array
24602460
* - reflClass (ReflectionClass)
24612461
* - propertyAccessors (ReflectionProperty array)
24622462
*
2463-
* @return array The names of all the fields that should be serialized.
2464-
*/
2465-
public function __sleep(): array
2466-
{
2467-
// This metadata is always serialized/cached.
2468-
$serialized = [
2469-
'fieldMappings',
2470-
'associationMappings',
2471-
'identifier',
2472-
'name',
2473-
'db',
2474-
'collection',
2475-
'readPreference',
2476-
'readPreferenceTags',
2477-
'writeConcern',
2478-
'rootDocumentName',
2479-
'generatorType',
2480-
'generatorOptions',
2481-
'idGenerator',
2482-
'indexes',
2483-
'shardKey',
2484-
'timeSeriesOptions',
2463+
* @return array<string, mixed>
2464+
*/
2465+
public function __serialize(): array
2466+
{
2467+
$data = [
2468+
'fieldMappings' => $this->fieldMappings,
2469+
'associationMappings' => $this->associationMappings,
2470+
'identifier' => $this->identifier,
2471+
'name' => $this->name,
2472+
'db' => $this->db,
2473+
'collection' => $this->collection,
2474+
'readPreference' => $this->readPreference,
2475+
'readPreferenceTags' => $this->readPreferenceTags,
2476+
'writeConcern' => $this->writeConcern,
2477+
'rootDocumentName' => $this->rootDocumentName,
2478+
'generatorType' => $this->generatorType,
2479+
'generatorOptions' => $this->generatorOptions,
2480+
'idGenerator' => $this->idGenerator,
2481+
'indexes' => $this->indexes,
2482+
'shardKey' => $this->shardKey,
2483+
'timeSeriesOptions' => $this->timeSeriesOptions,
24852484
];
24862485

2487-
// The rest of the metadata is only serialized if necessary.
24882486
if ($this->changeTrackingPolicy !== self::CHANGETRACKING_DEFERRED_IMPLICIT) {
2489-
$serialized[] = 'changeTrackingPolicy';
2487+
$data['changeTrackingPolicy'] = $this->changeTrackingPolicy;
24902488
}
24912489

24922490
if ($this->customRepositoryClassName) {
2493-
$serialized[] = 'customRepositoryClassName';
2491+
$data['customRepositoryClassName'] = $this->customRepositoryClassName;
24942492
}
24952493

24962494
if ($this->inheritanceType !== self::INHERITANCE_TYPE_NONE || $this->discriminatorField !== null) {
2497-
$serialized[] = 'inheritanceType';
2498-
$serialized[] = 'discriminatorField';
2499-
$serialized[] = 'discriminatorValue';
2500-
$serialized[] = 'discriminatorMap';
2501-
$serialized[] = 'defaultDiscriminatorValue';
2502-
$serialized[] = 'parentClasses';
2503-
$serialized[] = 'subClasses';
2495+
$data['inheritanceType'] = $this->inheritanceType;
2496+
$data['discriminatorField'] = $this->discriminatorField;
2497+
$data['discriminatorValue'] = $this->discriminatorValue;
2498+
$data['discriminatorMap'] = $this->discriminatorMap;
2499+
$data['defaultDiscriminatorValue'] = $this->defaultDiscriminatorValue;
2500+
$data['parentClasses'] = $this->parentClasses;
2501+
$data['subClasses'] = $this->subClasses;
25042502
}
25052503

25062504
if ($this->isMappedSuperclass) {
2507-
$serialized[] = 'isMappedSuperclass';
2505+
$data['isMappedSuperclass'] = $this->isMappedSuperclass;
25082506
}
25092507

25102508
if ($this->isEmbeddedDocument) {
2511-
$serialized[] = 'isEmbeddedDocument';
2509+
$data['isEmbeddedDocument'] = $this->isEmbeddedDocument;
25122510
}
25132511

25142512
if ($this->isQueryResultDocument) {
2515-
$serialized[] = 'isQueryResultDocument';
2513+
$data['isQueryResultDocument'] = $this->isQueryResultDocument;
25162514
}
25172515

2518-
if ($this->isView()) {
2519-
$serialized[] = 'isView';
2520-
$serialized[] = 'rootClass';
2516+
if ($this->isView) {
2517+
$data['isView'] = $this->isView;
2518+
$data['rootClass'] = $this->rootClass;
25212519
}
25222520

25232521
if ($this->isFile) {
2524-
$serialized[] = 'isFile';
2525-
$serialized[] = 'bucketName';
2526-
$serialized[] = 'chunkSizeBytes';
2522+
$data['isFile'] = $this->isFile;
2523+
$data['bucketName'] = $this->bucketName;
2524+
$data['chunkSizeBytes'] = $this->chunkSizeBytes;
25272525
}
25282526

25292527
if ($this->isVersioned) {
2530-
$serialized[] = 'isVersioned';
2531-
$serialized[] = 'versionField';
2528+
$data['isVersioned'] = $this->isVersioned;
2529+
$data['versionField'] = $this->versionField;
25322530
}
25332531

25342532
if ($this->isLockable) {
2535-
$serialized[] = 'isLockable';
2536-
$serialized[] = 'lockField';
2533+
$data['isLockable'] = $this->isLockable;
2534+
$data['lockField'] = $this->lockField;
25372535
}
25382536

25392537
if ($this->lifecycleCallbacks) {
2540-
$serialized[] = 'lifecycleCallbacks';
2538+
$data['lifecycleCallbacks'] = $this->lifecycleCallbacks;
25412539
}
25422540

25432541
if ($this->collectionCapped) {
2544-
$serialized[] = 'collectionCapped';
2545-
$serialized[] = 'collectionSize';
2546-
$serialized[] = 'collectionMax';
2542+
$data['collectionCapped'] = $this->collectionCapped;
2543+
$data['collectionSize'] = $this->collectionSize;
2544+
$data['collectionMax'] = $this->collectionMax;
25472545
}
25482546

25492547
if ($this->isReadOnly) {
2550-
$serialized[] = 'isReadOnly';
2548+
$data['isReadOnly'] = $this->isReadOnly;
25512549
}
25522550

25532551
if ($this->validator !== null) {
2554-
$serialized[] = 'validator';
2555-
$serialized[] = 'validationAction';
2556-
$serialized[] = 'validationLevel';
2552+
$data['validator'] = $this->validator;
2553+
$data['validationAction'] = $this->validationAction;
2554+
$data['validationLevel'] = $this->validationLevel;
25572555
}
25582556

2559-
return $serialized;
2557+
return $data;
25602558
}
25612559

25622560
/**
2563-
* Restores some state that cannot be serialized/unserialized.
2561+
* Restores state after unserialization.
2562+
*
2563+
* @param array<string, mixed> $data
25642564
*/
2565-
public function __wakeup(): void
2565+
public function __unserialize(array $data): void
25662566
{
2567+
foreach ($data as $property => $value) {
2568+
$this->$property = $value;
2569+
}
2570+
25672571
// Restore ReflectionClass and properties
25682572
$this->reflClass = new ReflectionClass($this->name);
25692573
$this->instantiator = new Instantiator();

src/PersistentCollection/PersistentCollectionTrait.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,18 @@ public function slice(int $offset, int|null $length = null): array
540540
* Called by PHP when this collection is serialized. Ensures that the
541541
* internal state of the collection can be reproduced after serialization
542542
*
543-
* @return string[]
543+
* @return array<string, mixed>
544544
*/
545-
public function __sleep(): array
546-
{
547-
return ['coll', 'initialized', 'mongoData', 'snapshot', 'isDirty', 'hints'];
545+
public function __serialize(): array
546+
{
547+
return [
548+
'coll' => $this->coll,
549+
'initialized' => $this->initialized,
550+
'mongoData' => $this->mongoData,
551+
'snapshot' => $this->snapshot,
552+
'isDirty' => $this->isDirty,
553+
'hints' => $this->hints,
554+
];
548555
}
549556

550557
/* ArrayAccess implementation */

src/Tools/Console/Command/GeneratePersistentCollectionsCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter;
88
use InvalidArgumentException;
9+
use LogicException;
910
use Symfony\Component\Console\Attribute\AsCommand;
1011
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\Console\Input\InputArgument;
@@ -19,6 +20,7 @@
1920
use function is_array;
2021
use function is_dir;
2122
use function is_writable;
23+
use function method_exists;
2224
use function mkdir;
2325
use function realpath;
2426
use function sprintf;
@@ -103,6 +105,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
103105
continue;
104106
}
105107

108+
if (method_exists($mapping['collectionClass'], '__sleep')) {
109+
throw new LogicException(sprintf(
110+
'Persistent collection class "%s" cannot implement the __sleep() method ' .
111+
'because it is deprecated in PHP. Implement __serialize() method instead.',
112+
$mapping['collectionClass']
113+
));
114+
}
115+
106116
$generated[$mapping['collectionClass']] = true;
107117
$output->write(
108118
sprintf('Generating class for "<info>%s</info>"', $mapping['collectionClass']) . PHP_EOL

0 commit comments

Comments
 (0)