Skip to content

Commit 5dc562a

Browse files
committed
Use __sleep/__wakeup instead of Serializable
Fixes symfony#38338
1 parent 127724d commit 5dc562a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/Symfony/Component/RateLimiter/LimiterStateInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
namespace Symfony\Component\RateLimiter;
1313

1414
/**
15+
* Representing the stored state of the limiter.
16+
*
17+
* Classes implementing this interface must be serializable,
18+
* which is used by the storage implementations to store the
19+
* object.
20+
*
1521
* @author Wouter de Jong <[email protected]>
1622
*
1723
* @experimental in 5.2
1824
*/
19-
interface LimiterStateInterface extends \Serializable
25+
interface LimiterStateInterface
2026
{
2127
public function getId(): string;
2228

src/Symfony/Component/RateLimiter/TokenBucket.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,22 @@ public function getExpirationTime(): int
7070
return $this->rate->calculateTimeForTokens($this->burstSize);
7171
}
7272

73-
public function serialize(): string
73+
/**
74+
* @internal
75+
*/
76+
public function __sleep(): array
7477
{
75-
return serialize([$this->id, $this->tokens, $this->timer, $this->burstSize, (string) $this->rate]);
78+
$this->stringRate = (string) $this->rate;
79+
80+
return ['id', 'tokens', 'timer', 'burstSize', 'stringRate'];
7681
}
7782

78-
public function unserialize($serialized): void
83+
/**
84+
* @internal
85+
*/
86+
public function __wakeup(): void
7987
{
80-
[$this->id, $this->tokens, $this->timer, $this->burstSize, $rate] = unserialize($serialized);
81-
82-
$this->rate = Rate::fromString($rate);
88+
$this->rate = Rate::fromString($this->stringRate);
89+
unset($this->stringRate);
8390
}
8491
}

src/Symfony/Component/RateLimiter/Window.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ public function getHitCount(): int
4848
return $this->hitCount;
4949
}
5050

51-
public function serialize(): string
51+
/**
52+
* @internal
53+
*/
54+
public function __sleep(): array
5255
{
5356
// $intervalInSeconds is not serialized, it should only be set
5457
// upon first creation of the Window.
55-
return serialize([$this->id, $this->hitCount]);
56-
}
57-
58-
public function unserialize($serialized): void
59-
{
60-
[$this->id, $this->hitCount] = unserialize($serialized);
58+
return ['id', 'hitCount'];
6159
}
6260
}

0 commit comments

Comments
 (0)