Skip to content
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
5 changes: 5 additions & 0 deletions UPGRADE-7.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Serializer

* Make `AttributeMetadata` and `ClassMetadata` final

String
------

* Deprecate implementing `__sleep/wakeup()` on string implementations

Translation
-----------

Expand Down
15 changes: 12 additions & 3 deletions src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,30 @@ public function __serialize(): array

public function __unserialize(array $data): void
{
if (self::class !== (new \ReflectionMethod($this, '__wakeup'))->class) {
if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class) {
trigger_deprecation('symfony/http-kernel', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
}

if (\in_array(array_keys($data), [['data'], ["\0*\0data"]], true)) {
$this->data = $data['data'] ?? $data["\0*\0data"];

if ($wakeup) {
$this->__wakeup();
}

return;
}

trigger_deprecation('symfony/http-kernel', '7.4', 'Passing more than just key "data" to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));

\Closure::bind(function ($data) {
\Closure::bind(function ($data) use ($wakeup) {
foreach ($data as $key => $value) {
$this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
}

$this->__wakeup();
if ($wakeup) {
$this->__wakeup();
}
}, $this, static::class)($data);
}

Expand All @@ -136,6 +142,8 @@ public function __unserialize(array $data): void
*/
public function __sleep(): array
{
trigger_deprecation('symfony/http-kernel', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));

return ['data'];
}

Expand All @@ -146,6 +154,7 @@ public function __sleep(): array
*/
public function __wakeup(): void
{
trigger_deprecation('symfony/http-kernel', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
}

/**
Expand Down
35 changes: 30 additions & 5 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,25 +802,46 @@ public function __serialize(): array

public function __unserialize(array $data): void
{
if (self::class !== (new \ReflectionMethod($this, '__wakeup'))->class) {
if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class) {
trigger_deprecation('symfony/http-kernel', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
}

if (\in_array(array_keys($data), [['environment', 'debug'], ["\0*\0environment", "\0*\0debug"]], true)) {
$this->environment = $data['environment'] ?? $data["\0*\0environment"];
$this->debug = $data['debug'] ?? $data["\0*\0debug"];
$environment = $data['environment'] ?? $data["\0*\0environment"];
$debug = $data['debug'] ?? $data["\0*\0debug"];

if (\is_object($environment) || \is_object($debug)) {
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}

$this->environment = $environment;
$this->debug = $debug;

if ($wakeup) {
$this->__wakeup();
} else {
$this->__construct($environment, $debug);
}

return;
}

trigger_deprecation('symfony/http-kernel', '7.4', 'Passing more than just key "environment" and "debug" to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));

\Closure::bind(function ($data) {
\Closure::bind(function ($data) use ($wakeup) {
foreach ($data as $key => $value) {
$this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
}

$this->__wakeup();
if ($wakeup) {
$this->__wakeup();
} else {
if (\is_object($this->environment) || \is_object($this->debug)) {
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}

$this->__construct($this->environment, $this->debug);
}
}, $this, static::class)($data);
}

Expand All @@ -831,6 +852,8 @@ public function __unserialize(array $data): void
*/
public function __sleep(): array
{
trigger_deprecation('symfony/http-kernel', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));

return ['environment', 'debug'];
}

Expand All @@ -841,6 +864,8 @@ public function __sleep(): array
*/
public function __wakeup(): void
{
trigger_deprecation('symfony/http-kernel', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));

if (\is_object($this->environment) || \is_object($this->debug)) {
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function merge(AttributeMetadataInterface $attributeMetadata): void
/**
* @internal since Symfony 7.4, will be replaced by `__serialize()` in 8.0
*
* @final since Symfony 7.4
* @final since Symfony 7.4, will be replaced by `__serialize()` in 8.0
*/
public function __sleep(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function setClassDiscriminatorMapping(?ClassDiscriminatorMapping $mapping
/**
* @internal since Symfony 7.4, will be replaced by `__serialize()` in 8.0
*
* @final since Symfony 7.4
* @final since Symfony 7.4, will be replaced by `__serialize()` in 8.0
*/
public function __sleep(): array
{
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,37 @@ public function wordwrap(int $width = 75, string $break = "\n", bool $cut = fals
return $str;
}

public function __serialize(): array
{
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
return ['string' => $this->string];
}

trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));

$data = [];
foreach ($this->__sleep() as $key) {
try {
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
$data[$key] = $r->getValue($this);
}
} catch (\ReflectionException) {
$data[$key] = $this->$key;
}
}

return $data;
}

/**
* @internal since Symfony 7.4, will be removed in 8.0
*
* @final since Symfony 7.4, will be removed in 8.0
*/
public function __sleep(): array
{
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));

return ['string'];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Deprecate implementing `__sleep/wakeup()` on string implementations

7.3
---

Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/String/LazyString.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ public function __toString(): string
}
}

public function __serialize(): array
{
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
$this->__toString();

return ['value' => $this->value];
}

trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));

$data = [];
foreach ($this->__sleep() as $key) {
try {
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
$data[$key] = $r->getValue($this);
}
} catch (\ReflectionException) {
$data[$key] = $this->$key;
}
}

return $data;
}

/**
* @internal since Symfony 7.4, will be removed in 8.0
*
* @final since Symfony 7.4, will be removed in 8.0
*/
public function __sleep(): array
{
$this->__toString();
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/String/UnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,54 @@
return $prefix === $grapheme;
}

public function __unserialize(array $data): void
{
if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class) {
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
}

try {
if (\in_array(array_keys($data), [['string'], ["\0*\0string"]], true)) {
$this->string = $data['string'] ?? $data["\0*\0string"];

if ($wakeup) {
$this->__wakeup();
}

return;
}

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));

\Closure::bind(function ($data) use ($wakeup) {
foreach ($data as $key => $value) {
$this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;

Check failure on line 390 in src/Symfony/Component/String/UnicodeString.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/String/UnicodeString.php:390:30: InvalidArgument: Isset only works with variables and array elements (see https://psalm.dev/004)

Check failure on line 390 in src/Symfony/Component/String/UnicodeString.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/String/UnicodeString.php:390:30: InvalidArgument: Isset only works with variables and array elements (see https://psalm.dev/004)
}

if ($wakeup) {
$this->__wakeup();
}
}, $this, static::class)($data);
} finally {
if (!$wakeup) {
if (!\is_string($this->string)) {
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}

normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
}
}
}

/**
* @internal since Symfony 7.4, will be removed in 8.0
*
* @final since Symfony 7.4, will be removed in 8.0
*/
public function __wakeup(): void
{
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));

if (!\is_string($this->string)) {
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/String/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"require": {
"php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3.0",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-intl-grapheme": "~1.33",
"symfony/polyfill-intl-normalizer": "~1.0",
Expand Down
Loading