From c3ea7cd6598b2ec44f50691b1970260e185a2bf3 Mon Sep 17 00:00:00 2001 From: Kyrian Obikwelu Date: Thu, 12 Jun 2025 08:10:03 +0100 Subject: [PATCH] fix(cache): Use serialize/unserialize in FileCache for object persistence This resolves issues where `ClientStateManager` would fail to hydrate `ClientState` objects from `FileCache`, effectively making the cache non-functional for object storage. --- src/Defaults/FileCache.php | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/Defaults/FileCache.php b/src/Defaults/FileCache.php index 8ac19b0..e516e37 100644 --- a/src/Defaults/FileCache.php +++ b/src/Defaults/FileCache.php @@ -193,13 +193,11 @@ private function readCacheFile(): array $handle = @fopen($this->cacheFile, 'rb'); if ($handle === false) { - // TODO: Log error return []; } try { if (! flock($handle, LOCK_SH)) { - // TODO: Log error return []; } $content = stream_get_contents($handle); @@ -209,9 +207,8 @@ private function readCacheFile(): array return []; } - $data = json_decode($content, true); - if (json_last_error() !== JSON_ERROR_NONE || ! is_array($data)) { - // TODO: Log error, potentially unlink corrupt file + $data = unserialize($content); + if ($data === false) { return []; } @@ -225,29 +222,25 @@ private function readCacheFile(): array private function writeCacheFile(array $data): bool { - $jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); - if (json_last_error() !== JSON_ERROR_NONE) { - // TODO: Log error + $jsonData = serialize($data); + + if ($jsonData === false) { return false; } $handle = @fopen($this->cacheFile, 'cb'); if ($handle === false) { - // TODO: Log error return false; } try { if (! flock($handle, LOCK_EX)) { - // TODO: Log error return false; } if (! ftruncate($handle, 0)) { - // TODO: Log error return false; } if (fwrite($handle, $jsonData) === false) { - // TODO: Log error return false; } fflush($handle); @@ -256,7 +249,6 @@ private function writeCacheFile(array $data): bool return true; } catch (Throwable $e) { - // TODO: Log error flock($handle, LOCK_UN); // Ensure lock release on error return false; @@ -271,7 +263,6 @@ private function ensureDirectoryExists(string $directory): void { if (! is_dir($directory)) { if (! @mkdir($directory, $this->dirPermission, true)) { - // TODO: Log error throw new InvalidArgumentException("Cache directory does not exist and could not be created: {$directory}"); } @chmod($directory, $this->dirPermission); @@ -291,11 +282,9 @@ private function calculateExpiry(DateInterval|int|null $ttl): ?int try { return (new DateTimeImmutable())->add($ttl)->getTimestamp(); } catch (Throwable $e) { - // TODO: Log error return null; } } - // TODO: Log warning throw new InvalidArgumentException('Invalid TTL type provided. Must be null, int, or DateInterval.'); } @@ -321,9 +310,9 @@ private function validateKeys(array $keys): void { foreach ($keys as $key) { if (! is_string($key)) { - throw new InvalidArgumentException('Cache key must be a string, got '.gettype($key)); + throw new InvalidArgumentException('Cache key must be a string, got ' . gettype($key)); } - $this->sanitizeKey($key); // Reuse sanitize validation + $this->sanitizeKey($key); } }