-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCache.php
More file actions
194 lines (159 loc) · 4.26 KB
/
Cache.php
File metadata and controls
194 lines (159 loc) · 4.26 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
namespace Pest\TypeCoverage\Support;
use LogicException;
use PHPStan\Analyser\Error;
/**
* @internal
*/
final class Cache
{
/**
* The cache version.
*/
private const string CACHE_VERSION = 'v4';
/**
* The cache instance.
*/
public static function instance(): self
{
return new self;
}
/**
* Checks if the cache contains the given file.
*/
public function has(string $file): bool
{
$fileHash = md5_file($file);
if ($fileHash === false) {
return false;
}
$items = $this->all();
return array_key_exists($fileHash, $items);
}
/**
* Gets the cached contents for the given file.
*
* @return array{0: string, 1: array<int, Error>, 2: array<int, Error>}
*
* @throws LogicException
*/
public function get(string $file): array
{
$fileHash = md5_file($file);
if ($fileHash === false) {
throw new LogicException('Failed to compute the hash for the file: '.$file);
}
$items = $this->all();
return $items[$fileHash] ?? throw new LogicException('No cache found for the file: '.$file);
}
/**
* Flushes all the cache contents.
*/
public function flush(): void
{
if (is_file($this->file())) {
unlink($this->file());
}
}
/**
* Returns the cache file.
*/
private function file(): string
{
return dirname(__DIR__, 2)
.DIRECTORY_SEPARATOR
.'.temp'
.DIRECTORY_SEPARATOR
.self::CACHE_VERSION
.'.php';
}
/**
* Gets all the cache contents.
*/
private function all(): array
{
return $this->withinLock(function () {
if (! is_file($this->file())) {
return [];
}
$cache = include $this->file();
return is_array($cache) ? $cache : [];
});
}
/**
* Persists the cache contents.
*/
public function persist(string $file, array $values): void
{
$fileHash = md5_file($file);
foreach ($values as $value) {
if (is_array($value)) {
foreach ($value as $item) {
if ($item instanceof Error) {
(fn () => $this->canBeIgnored = null)->call($item);
}
}
}
}
$dirPath = dirname($this->file());
if (! is_dir($dirPath)) {
if (! mkdir($dirPath, 0755, true)) {
return;
}
chmod($dirPath, 0755);
}
$this->withinLock(function () use ($fileHash, $values) {
$filePath = $this->file();
$cache = [];
if (is_file($filePath)) {
$existingCache = include $filePath;
if (is_array($existingCache)) {
$cache = $existingCache;
}
}
$cache[$fileHash] = $values;
$content = '<?php return unserialize('.var_export(serialize($cache), true).');';
if (file_put_contents($filePath, $content) !== false) {
chmod($filePath, 0666);
}
return null;
});
}
/**
* Executes the callback within a lock.
*/
private function withinLock(callable $callback): mixed
{
$filePath = $this->file();
$lockPath = $filePath.'.lock';
$dirPath = dirname($filePath);
if (! is_dir($dirPath)) {
mkdir($dirPath, 0755, true);
chmod($dirPath, 0755);
}
if (! is_file($lockPath)) {
touch($lockPath);
chmod($lockPath, 0666);
}
$lock = fopen($lockPath, 'c+');
if ($lock === false) {
return $callback();
}
$attempts = 0;
while (! flock($lock, LOCK_EX | LOCK_NB) && $attempts < 100) {
usleep(1000);
$attempts++;
}
if ($attempts >= 100) {
fclose($lock);
return $callback();
}
try {
return $callback();
} finally {
flock($lock, LOCK_UN);
fclose($lock);
}
}
}