Skip to content

Commit

Permalink
Detect cache saving error
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 22, 2020
1 parent 262694d commit 542238a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Cache/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ public function save(string $key, string $variableKey, $data): void
$this->makeDir($secondDirectory);

$tmpPath = sprintf('%s/%s.tmp', $this->directory, Random::generate());
$errorBefore = error_get_last();
$exported = @var_export(new CacheItem($variableKey, $data), true);
$errorAfter = error_get_last();
if ($errorAfter !== null && $errorBefore !== $errorAfter) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Error occurred while saving item %s (%s) to cache: %s', $key, $variableKey, $errorAfter['message']));
}
FileWriter::write(
$tmpPath,
sprintf(
"<?php declare(strict_types = 1);\n\nreturn %s;",
var_export(new CacheItem($variableKey, $data), true)
$exported
)
);

Expand Down

0 comments on commit 542238a

Please sign in to comment.