Repository navigation
Expand file tree
/
Copy pathFileCacheStorage.php
More file actions
156 lines (128 loc) · 5.18 KB
/
Copy pathFileCacheStorage.php
File metadata and controls
156 lines (128 loc) · 5.18 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Rector\Caching\ValueObject\Storage;
use FilesystemIterator;
use Nette\Utils\FileSystem;
use Nette\Utils\Random;
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();
$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 CachingException(\sprintf(
'Error occurred while saving item %s (%s) to cache: %s',
$key,
$variableKey,
$errorAfter['message']
));
}
// for performance reasons we don't use SmartFileSystem
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported), null);
/**
* @see https://github.com/rectorphp/rector/issues/9876
*
* Try the atomic write first, as rename() also works on unaffected Windows PHP versions
*/
$renameSuccess = @\rename($tmpPath, $filePath);
if ($renameSuccess) {
return;
}
if (\DIRECTORY_SEPARATOR === '/' || ! \file_exists($filePath)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}
/**
* @see https://github.com/rectorphp/rector/issues/8432
* @see https://github.com/php/php-src/issues/7910
*
* Fall back only on affected Windows PHP versions where rename() failed
*/
$copySuccess = @\copy($tmpPath, $filePath);
@\unlink($tmpPath);
if ($copySuccess) {
return;
}
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}
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);
}
}