Repository navigation
Expand file tree
/
Copy pathFileCacheStorage.php
More file actions
126 lines (103 loc) · 4.28 KB
/
Copy pathFileCacheStorage.php
File metadata and controls
126 lines (103 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Rector\Caching\ValueObject\Storage;
use FilesystemIterator;
use Nette\Utils\FileSystem;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Caching\ValueObject\CacheFilePaths;
use Rector\Caching\ValueObject\CacheItem;
use Rector\Exception\Cache\CachingException;
/**
* Inspired by https://github.com/phpstan/phpstan-src/blob/1e7ceae933f07e5a250b61ed94799e6c2ea8daa2/src/Cache/FileCacheStorage.php
* @see \Rector\Tests\Caching\ValueObject\Storage\FileCacheStorageTest
*/
final readonly class FileCacheStorage implements CacheStorageInterface
{
public function __construct(
private string $directory,
private \Symfony\Component\Filesystem\Filesystem $filesystem
) {
}
public function load(string $key, string $variableKey): mixed
{
return (function (string $key, string $variableKey) {
$cacheFilePaths = $this->getCacheFilePaths($key);
$filePath = $cacheFilePaths->getFilePath();
if (! \is_file($filePath)) {
return null;
}
$cacheItem = (require $filePath);
if (! $cacheItem instanceof CacheItem) {
return null;
}
if (! $cacheItem->isVariableKeyValid($variableKey)) {
return null;
}
return $cacheItem->getData();
})($key, $variableKey);
}
public function save(string $key, string $variableKey, mixed $data): void
{
$cacheFilePaths = $this->getCacheFilePaths($key);
$this->filesystem->mkdir($cacheFilePaths->getFirstDirectory());
$this->filesystem->mkdir($cacheFilePaths->getSecondDirectory());
$filePath = $cacheFilePaths->getFilePath();
$errorBefore = \error_get_last();
$exported = @\var_export(new CacheItem($variableKey, $data), true);
$errorAfter = \error_get_last();
if ($errorAfter !== null && $errorBefore !== $errorAfter) {
throw new CachingException(\sprintf(
'Error occurred while saving item %s (%s) to cache: %s',
$key,
$variableKey,
$errorAfter['message']
));
}
// atomic write via temp file + rename(), so a parallel reader never sees a partially written cache file
// handles the Windows rename() edge case too, see https://github.com/rectorphp/rector/issues/9876
FileSystem::writeAtomic($filePath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported), null);
}
public function clean(string $key): void
{
$cacheFilePaths = $this->getCacheFilePaths($key);
$this->processRemoveCacheFilePath($cacheFilePaths);
$this->processRemoveEmptyDirectory($cacheFilePaths->getSecondDirectory());
$this->processRemoveEmptyDirectory($cacheFilePaths->getFirstDirectory());
}
public function clear(): void
{
FileSystem::delete($this->directory);
}
private function processRemoveCacheFilePath(CacheFilePaths $cacheFilePaths): void
{
$filePath = $cacheFilePaths->getFilePath();
if (! $this->filesystem->exists($filePath)) {
return;
}
FileSystem::delete($filePath);
}
private function processRemoveEmptyDirectory(string $directory): void
{
if (! $this->filesystem->exists($directory)) {
return;
}
if ($this->isNotEmptyDirectory($directory)) {
return;
}
FileSystem::delete($directory);
}
private function isNotEmptyDirectory(string $directory): bool
{
// FilesystemIterator will initially point to the first file in the folder - if there are no files in the folder, valid() will return false
$filesystemIterator = new FilesystemIterator($directory);
return $filesystemIterator->valid();
}
private function getCacheFilePaths(string $key): CacheFilePaths
{
$keyHash = sha1($key);
$firstDirectory = sprintf('%s/%s', $this->directory, substr($keyHash, 0, 2));
$secondDirectory = sprintf('%s/%s', $firstDirectory, substr($keyHash, 2, 2));
$filePath = sprintf('%s/%s.php', $secondDirectory, $keyHash);
return new CacheFilePaths($firstDirectory, $secondDirectory, $filePath);
}
}