Skip to content

Commit 86ca954

Browse files
Merge branch '7.4' into 8.0
* 7.4: [Mime] Deprecate implementing `__sleep/wakeup()` on `AbstractPart` implementations [Validator] Deprecate implementing `__sleep/wakeup()` on GenericMetadata implementations [String] Deprecate implementing `__sleep/wakeup()` on string implementations More cleanups related to internal sleep/wakeup usages [HttpKernel] Deprecate `__sleep/wakeup()` on kernels and data collectors and make `Profile` final [Serializer] Make `AttributeMetadata` and `ClassMetadata` final Replace __sleep/wakeup() by __(un)serialize() when BC isn't a concern chore: PHP CS Fixer - simplify config
2 parents 24b585b + 3ba35cb commit 86ca954

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

AbstractString.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,35 @@ public function wordwrap(int $width = 75, string $break = "\n", bool $cut = fals
706706
return $str;
707707
}
708708

709+
public function __serialize(): array
710+
{
711+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) {
712+
return ['string' => $this->string];
713+
}
714+
715+
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
716+
717+
$data = [];
718+
foreach ($this->__sleep() as $key) {
719+
try {
720+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
721+
$data[$key] = $r->getValue($this);
722+
}
723+
} catch (\ReflectionException) {
724+
$data[$key] = $this->$key;
725+
}
726+
}
727+
728+
return $data;
729+
}
730+
731+
/**
732+
* @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
733+
*/
709734
public function __sleep(): array
710735
{
736+
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
737+
711738
return ['string'];
712739
}
713740

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Deprecate implementing `__sleep/wakeup()` on string implementations
8+
49
7.3
510
---
611

LazyString.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,37 @@ public function __toString(): string
101101
}
102102
}
103103

104+
public function __serialize(): array
105+
{
106+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class || self::class !== (new \ReflectionMethod($this, '__serialize'))->class) {
107+
$this->__toString();
108+
109+
return ['value' => $this->value];
110+
}
111+
112+
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
113+
114+
$data = [];
115+
foreach ($this->__sleep() as $key) {
116+
try {
117+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
118+
$data[$key] = $r->getValue($this);
119+
}
120+
} catch (\ReflectionException) {
121+
$data[$key] = $this->$key;
122+
}
123+
}
124+
125+
return $data;
126+
}
127+
128+
/**
129+
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
130+
*/
104131
public function __sleep(): array
105132
{
133+
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
134+
106135
$this->__toString();
107136

108137
return ['value'];

UnicodeString.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,52 @@ public function startsWith(string|iterable|AbstractString $prefix): bool
366366
return $prefix === $grapheme;
367367
}
368368

369+
public function __unserialize(array $data): void
370+
{
371+
if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class && self::class === (new \ReflectionMethod($this, '__unserialize'))->class) {
372+
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
373+
}
374+
375+
try {
376+
if (\in_array(array_keys($data), [['string'], ["\0*\0string"]], true)) {
377+
$this->string = $data['string'] ?? $data["\0*\0string"];
378+
379+
if ($wakeup) {
380+
$this->__wakeup();
381+
}
382+
383+
return;
384+
}
385+
386+
trigger_deprecation('symfony/string', '7.4', 'Passing more than just key "string" to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));
387+
388+
\Closure::bind(function ($data) use ($wakeup) {
389+
foreach ($data as $key => $value) {
390+
$this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
391+
}
392+
393+
if ($wakeup) {
394+
$this->__wakeup();
395+
}
396+
}, $this, static::class)($data);
397+
} finally {
398+
if (!$wakeup) {
399+
if (!\is_string($this->string)) {
400+
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
401+
}
402+
403+
normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
404+
}
405+
}
406+
}
407+
408+
/**
409+
* @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
410+
*/
369411
public function __wakeup(): void
370412
{
413+
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
414+
371415
if (!\is_string($this->string)) {
372416
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
373417
}

0 commit comments

Comments
 (0)