Skip to content

Commit 77792f3

Browse files
committed
PersistentState serialization magic methods
Could be used to facilitate persistent storage in a non-persistent application, such as a script called by PHP-FPM or PHP-CGI
1 parent 0a66ba0 commit 77792f3

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/VerifierServer/PersistentState.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public function __construct(
4040
private string $storageType = 'filesystem',
4141
private string $json_path = 'json/verify.json'
4242
) {
43-
if ($this->storageType !== 'filesystem') {
44-
$this->initializeDatabase();
45-
}
43+
$this->__wakeup();
4644
}
4745

4846
/**
@@ -273,4 +271,26 @@ public static function writeJson(string $file, array $data): void
273271
{
274272
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));
275273
}
274+
275+
/**
276+
* Prepares the object for serialization by excluding the PDO object.
277+
*
278+
* @return array The list of properties to serialize.
279+
*/
280+
public function __sleep(): array
281+
{
282+
return ['civToken', 'storageType', 'json_path', 'verifyList'];
283+
}
284+
285+
/**
286+
* Reinitializes the PDO object after deserialization.
287+
*
288+
* @return void
289+
*/
290+
public function __wakeup(): void
291+
{
292+
if ($this->storageType !== 'filesystem') {
293+
$this->initializeDatabase();
294+
}
295+
}
276296
}

0 commit comments

Comments
 (0)