Skip to content

Commit f948fed

Browse files
committed
Prepare for the new serialization mechanism
1 parent d7e94f7 commit f948fed

13 files changed

+142
-128
lines changed

Authentication/Token/AbstractToken.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,36 @@ public function eraseCredentials()
142142
}
143143
}
144144

145+
/**
146+
* Returns all the necessary state of the object for serialization purposes.
147+
*
148+
* There is no need to serialize any entry, they should be returned as-is.
149+
* If you extend this method, keep in mind you MUST guarantee parent data is present in the state.
150+
* Here is an example of how to extend this method:
151+
* <code>
152+
* public function __serialize(): array
153+
* {
154+
* return [$this->childAttribute, parent::__serialize()];
155+
* }
156+
* </code>
157+
*
158+
* @see __unserialize()
159+
*/
160+
public function __serialize(): array
161+
{
162+
return [$this->user, $this->authenticated, $this->roles, $this->attributes, $this->roleNames];
163+
}
164+
145165
/**
146166
* {@inheritdoc}
147167
*
148-
* @final since Symfony 4.3, use getState() instead
168+
* @final since Symfony 4.3, use __serialize() instead
149169
*
150-
* @internal since Symfony 4.3, use getState() instead
170+
* @internal since Symfony 4.3, use __serialize() instead
151171
*/
152172
public function serialize()
153173
{
154-
$serialized = $this->getState();
174+
$serialized = $this->__serialize();
155175

156176
if (null === $isCalledFromOverridingMethod = \func_num_args() ? \func_get_arg(0) : null) {
157177
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
@@ -162,56 +182,36 @@ public function serialize()
162182
}
163183

164184
/**
165-
* {@inheritdoc}
166-
*
167-
* @final since Symfony 4.3, use setState() instead
168-
*
169-
* @internal since Symfony 4.3, use setState() instead
170-
*/
171-
public function unserialize($serialized)
172-
{
173-
$this->setState(\is_array($serialized) ? $serialized : unserialize($serialized));
174-
}
175-
176-
/**
177-
* Returns all the necessary state of the object for serialization purposes.
185+
* Restores the object state from an array given by __serialize().
178186
*
179-
* There is no need to serialize any entry, they should be returned as-is.
180-
* If you extend this method, keep in mind you MUST guarantee parent data is present in the state.
187+
* There is no need to unserialize any entry in $data, they are already ready-to-use.
188+
* If you extend this method, keep in mind you MUST pass the parent data to its respective class.
181189
* Here is an example of how to extend this method:
182190
* <code>
183-
* protected function getState(): array
191+
* public function __unserialize(array $data): void
184192
* {
185-
* return [$this->childAttribute, parent::getState()];
193+
* [$this->childAttribute, $parentData] = $data;
194+
* parent::__unserialize($parentData);
186195
* }
187196
* </code>
188197
*
189-
* @see setState()
198+
* @see __serialize()
190199
*/
191-
protected function getState(): array
200+
public function __unserialize(array $data): void
192201
{
193-
return [$this->user, $this->authenticated, $this->roles, $this->attributes, $this->roleNames];
202+
[$this->user, $this->authenticated, $this->roles, $this->attributes, $this->roleNames] = $data;
194203
}
195204

196205
/**
197-
* Restores the object state from an array given by getState().
206+
* {@inheritdoc}
198207
*
199-
* There is no need to unserialize any entry in $data, they are already ready-to-use.
200-
* If you extend this method, keep in mind you MUST pass the parent data to its respective class.
201-
* Here is an example of how to extend this method:
202-
* <code>
203-
* protected function setState(array $data)
204-
* {
205-
* [$this->childAttribute, $parentData] = $data;
206-
* parent::setState($parentData);
207-
* }
208-
* </code>
208+
* @final since Symfony 4.3, use __unserialize() instead
209209
*
210-
* @see getState()
210+
* @internal since Symfony 4.3, use __unserialize() instead
211211
*/
212-
protected function setState(array $data)
212+
public function unserialize($serialized)
213213
{
214-
[$this->user, $this->authenticated, $this->roles, $this->attributes, $this->roleNames] = $data;
214+
$this->__unserialize(\is_array($serialized) ? $serialized : unserialize($serialized));
215215
}
216216

217217
/**

Authentication/Token/AnonymousToken.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ public function getSecret()
5555
/**
5656
* {@inheritdoc}
5757
*/
58-
protected function getState(): array
58+
public function __serialize(): array
5959
{
60-
return [$this->secret, parent::getState()];
60+
return [$this->secret, parent::__serialize()];
6161
}
6262

6363
/**
6464
* {@inheritdoc}
6565
*/
66-
protected function setState(array $data)
66+
public function __unserialize(array $data): void
6767
{
6868
[$this->secret, $parentData] = $data;
69-
parent::setState($parentData);
69+
parent::__unserialize($parentData);
7070
}
7171
}

Authentication/Token/PreAuthenticatedToken.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ public function eraseCredentials()
7575
/**
7676
* {@inheritdoc}
7777
*/
78-
protected function getState(): array
78+
public function __serialize(): array
7979
{
80-
return [$this->credentials, $this->providerKey, parent::getState()];
80+
return [$this->credentials, $this->providerKey, parent::__serialize()];
8181
}
8282

8383
/**
8484
* {@inheritdoc}
8585
*/
86-
protected function setState(array $data)
86+
public function __unserialize(array $data): void
8787
{
8888
[$this->credentials, $this->providerKey, $parentData] = $data;
89-
parent::setState($parentData);
89+
parent::__unserialize($parentData);
9090
}
9191
}

Authentication/Token/RememberMeToken.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ public function getCredentials()
9292
/**
9393
* {@inheritdoc}
9494
*/
95-
protected function getState(): array
95+
public function __serialize(): array
9696
{
97-
return [$this->secret, $this->providerKey, parent::getState()];
97+
return [$this->secret, $this->providerKey, parent::__serialize()];
9898
}
9999

100100
/**
101101
* {@inheritdoc}
102102
*/
103-
protected function setState(array $data)
103+
public function __unserialize(array $data): void
104104
{
105105
[$this->secret, $this->providerKey, $parentData] = $data;
106-
parent::setState($parentData);
106+
parent::__unserialize($parentData);
107107
}
108108
}

Authentication/Token/SwitchUserToken.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ public function getOriginalToken(): TokenInterface
4444
/**
4545
* {@inheritdoc}
4646
*/
47-
protected function getState(): array
47+
public function __serialize(): array
4848
{
49-
return [$this->originalToken, parent::getState()];
49+
return [$this->originalToken, parent::__serialize()];
5050
}
5151

5252
/**
5353
* {@inheritdoc}
5454
*/
55-
protected function setState(array $data)
55+
public function __unserialize(array $data): void
5656
{
5757
[$this->originalToken, $parentData] = $data;
58-
parent::setState($parentData);
58+
parent::__unserialize($parentData);
5959
}
6060
}

Authentication/Token/UsernamePasswordToken.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ public function eraseCredentials()
8787
/**
8888
* {@inheritdoc}
8989
*/
90-
protected function getState(): array
90+
public function __serialize(): array
9191
{
92-
return [$this->credentials, $this->providerKey, parent::getState()];
92+
return [$this->credentials, $this->providerKey, parent::__serialize()];
9393
}
9494

9595
/**
9696
* {@inheritdoc}
9797
*/
98-
protected function setState(array $data)
98+
public function __unserialize(array $data): void
9999
{
100100
[$this->credentials, $this->providerKey, $parentData] = $data;
101-
parent::setState($parentData);
101+
parent::__unserialize($parentData);
102102
}
103103
}

Exception/AccountStatusException.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public function setUser(UserInterface $user)
4242
/**
4343
* {@inheritdoc}
4444
*/
45-
protected function getState(): array
45+
public function __serialize(): array
4646
{
47-
return [$this->user, parent::getState()];
47+
return [$this->user, parent::__serialize()];
4848
}
4949

5050
/**
5151
* {@inheritdoc}
5252
*/
53-
protected function setState(array $data)
53+
public function __unserialize(array $data): void
5454
{
5555
[$this->user, $parentData] = $data;
56-
parent::setState($parentData);
56+
parent::__unserialize($parentData);
5757
}
5858
}

0 commit comments

Comments
 (0)