8000 Prepare for the new serialization mechanism · symfony/symfony@c4ce96e · GitHub
[go: up one dir, main page]

Skip to content

Commit c4ce96e

Browse files
committed
Prepare for the new serialization mechanism
1 parent 65b46a5 commit c4ce96e

23 files changed

+238
-167
lines changed

UPGRADE-4.3.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Security
115115
* The `Firewall::handleRequest()` method is deprecated, use `Firewall::callListeners()` instead.
116116
* The `AbstractToken::serialize()`, `AbstractToken::unserialize()`,
117117
`AuthenticationException::serialize()` and `AuthenticationException::unserialize()`
118-
methods are now final, use `getState()` and `setState()` instead.
118+
methods are now final, use `__serialize()` and `__unserialize()` instead.
119119

120120
Before:
121121
```php
@@ -133,15 +133,15 @@ Security
133133

134134
After:
135135
```php
136-
protected function getState(): array
136+
protected function __serialize(): array
137137
{
138-
return [$this->myLocalVar, parent::getState()];
138+
return [$this->myLocalVar, parent::__serialize()];
139139
}
140140
141-
protected function setState(array $data)
141+
protected function __unserialize(array $data): void
142142
{
143143
[$this->myLocalVar, $parentData] = $data;
144-
parent::setState($parentData);
144+
parent::__unserialize($parentData);
145145
}
146146
```
147147

UPGRADE-5.0.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ Security
293293
* The `Firewall::handleRequest()` method has been removed, use `Firewall::callListeners()` instead.
294294
* `\Serializable` interface has been removed from `AbstractToken` and `AuthenticationException`,
295295
thus `serialize()` and `unserialize()` aren't available.
296-
Use `getState()` and `setState()` instead.
296+
Use `__serialize()` and `__unserialize()` instead.
297297

298298
Before:
299299
```php
@@ -311,15 +311,15 @@ Security
311311

312312
After:
313313
```php
314-
protected function getState(): array
314+
protected function __serialize(): array
315315
{
316-
return [$this->myLocalVar, parent::getState()];
316+
return [$this->myLocalVar, parent::__serialize()];
317317
}
318318
319-
protected function setState(array $data)
319+
protected function __unserialize(array $data): void
320320
{
321321
[$this->myLocalVar, $parentData] = $data;
322-
parent::setState($parentData);
322+
parent::__unserialize($parentData);
323323
}
324324
```
325325

src/Symfony/Component/Form/Tests/Fixtures/CustomArrayObject.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,23 @@ public function count()
5858
return \count($this->array);
5959
}
6060

61+
public function __serialize(): array
62+
{
63+
return $this->array;
64+
}
65+
6166
public function serialize()
6267
{
63-
return serialize($this->array);
68+
return serialize($this->__serialize());
69+
}
70+
71+
public function __unserialize(array $data): void
72+
{
73+
$this->array = $data;
6474
}
6575

6676
public function unserialize($serialized)
6777
{
68-
$this->array = (array) unserialize((string) $serialized);
78+
$this->__unserialize((array) unserialize((string) $serialized));
6979
}
7080
}

src/Symfony/Component/PropertyAccess/Tests/Fixtures/NonTraversableArrayObject.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,23 @@ public function count()
5353
return \count($this->array);
5454
}
5555

56+
public function __serialize(): array
57+
{
58+
return $this->array;
59+
}
60+
5661
public function serialize()
5762
{
58-
return serialize($this->array);
63+
return serialize($this->__serialize());
64+
}
65+
66+
public function __unserialize(array $data): void
67+
{
68+
$this->array = $data;
5969
}
6070

6171
public function unserialize($serialized)
6272
{
63-
$this->array = (array) unserialize((string) $serialized);
73+
$this->__unserialize((array) unserialize((string) $serialized));
6474
}
6575
}

src/Symfony/Component/PropertyAccess/Tests/Fixtures/TraversableArrayObject.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,23 @@ public function count()
5858
return \count($this->array);
5959
}
6060

61+
public function __serialize(): array
62+
{
63+
return $this->array;
64+
}
65+
6166
public function serialize()
6267
{
63-
return serialize($this->array);
68+
return serialize($this->__serialize());
69+
}
70+
71+
public function __unserialize(array $data): void
72+
{
73+
$this->array = $data;
6474
}
6575

6676
public function unserialize($serialized)
6777
{
68-
$this->array = (array) unserialize((string) $serialized);
78+
$this->__unserialize((array) unserialize((string) $serialized));
6979
}
7080
}

src/Symfony/Component/Routing/CompiledRoute.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ public function __construct(string $staticPrefix, string $regex, array $tokens,
4949
$this->variables = $variables;
5050
}
5151

52-
/**
53-
* @internal since Symfony 4.3, will be removed in Symfony 5 as the class won't implement Serializable anymore
54-
*/
55-
public function serialize()
52+
public function __serialize(): array
5653
{
57-
return serialize([
54+
return [
5855
'vars' => $this->variables,
5956
'path_prefix' => $this->staticPrefix,
6057
'path_regex' => $this->regex,
@@ -63,16 +60,19 @@ public function serialize()
6360
'host_regex' => $this->hostRegex,
6461
'host_tokens' => $this->hostTokens,
6562
'host_vars' => $this->hostVariables,
66-
]);
63+
];
6764
}
6865

6966
/**
7067
* @internal since Symfony 4.3, will be removed in Symfony 5 as the class won't implement Serializable anymore
7168
*/
72-
public function unserialize($serialized)
69+
public function serialize()
7370
{
74-
$data = unserialize($serialized, ['allowed_classes' => false]);
71+
return serialize($this->__serialize());
72+
}
7573

74+
public function __unserialize(array $data): void
75+
{
7676
$this->variables = $data['vars'];
7777
$this->staticPrefix = $data['path_prefix'];
7878
$this->regex = $data['path_regex'];
@@ -83,6 +83,14 @@ public function unserialize($serialized)
8383
$this->hostVariables = $data['host_vars'];
8484
}
8585

86+
/**
87+
* @internal since Symfony 4.3, will be removed in Symfony 5 as the class won't implement Serializable anymore
88+
*/
89+
public function unserialize($serialized)
90+
{
91+
$this->__unserialize(unserialize($serialized, ['allowed_classes' => false]));
92+
}
93+
8694
/**
8795
* Returns the static prefix.
8896
*

src/Symfony/Component/Routing/Route.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,9 @@ public function __construct(string $path, array $defaults = [], array $requireme
6262
$this->setCondition($condition);
6363
}
6464

65-
/**
66-
* @internal since Symfony 4.3, will be removed in Symfony 5 as the class won't implement Serializable anymore
67-
*/
68-
public function serialize()
65+
public function __serialize(): array
6966
{
70-
return serialize([
67+
return [
7168
'path' => $this->path,
7269
'host' => $this->host,
7370
'defaults' => $this->defaults,
@@ -77,15 +74,19 @@ public function serialize()
7774
'methods' => $this->methods,
7875
'condition' => $this->condition,
7976
'compiled' => $this->compiled,
80-
]);
77+
];
8178
}
8279

8380
/**
8481
* @internal since Symfony 4.3, will be removed in Symfony 5 as the class won't implement Serializable anymore
8582
*/
86-
public function unserialize($serialized)
83+
public function serialize()
84+
{
85+
return serialize($this->__serialize());
86+
}
87+
88+
public function __unserialize(array $data): void
8789
{
88-
$data = unserialize($serialized);
8990
$this->path = $data['path'];
9091
$this->host = $data['host'];
9192
$this->defaults = $data['defaults'];
@@ -102,6 +103,14 @@ public function unserialize($serialized)
102103
}
103104
}
104105

106+
/**
107+
* @internal since Symfony 4.3, will be removed in Symfony 5 as the class won't implement Serializable anymore
108+
*/
109+
public function unserialize($serialized)
110+
{
111+
$this->__unserialize(unserialize($serialized));
112+
}
113+
105114
/**
106115
* Returns the pattern for the path.
107116
*

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CHANGELOG
1111
* The `getRoles()` method of the `TokenInterface` is deprecated. Tokens must implement the `getRoleNames()`
1212
method instead and return roles as strings.
1313
* Made the `serialize()` and `unserialize()` methods of `AbstractToken` and
14-
`AuthenticationException` final, use `getState()`/`setState()` instead
14+
`AuthenticationException` final, use `__serialize()`/`__unserialize()` instead
1515
* `AuthenticationException` doesn't implement `Serializable` anymore
1616
* Deprecated the `ListenerInterface`, turn your listeners into callables instead
1717
* Deprecated `Firewall::handleRequest()`, use `Firewall::callListeners()` instead

src/Symfony/Component/Security/Core/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+
* protected 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
/**

src/Symfony/Component/Security/Core/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
}

0 commit comments

Comments
 (0)
0