Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions src/Defaults/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 [];
}

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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.');
}

Expand All @@ -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);
}
}

Expand Down