forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileStorageCacheProvider.php
170 lines (137 loc) · 5.32 KB
/
FileStorageCacheProvider.php
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
<?php
namespace Psalm\Internal\Provider;
use Psalm\Config;
use Psalm\Internal\Cache;
use Psalm\Storage\FileStorage;
use RuntimeException;
use UnexpectedValueException;
use function array_merge;
use function dirname;
use function file_exists;
use function filemtime;
use function get_class;
use function hash;
use function is_dir;
use function mkdir;
use function strtolower;
use const DIRECTORY_SEPARATOR;
use const PHP_VERSION_ID;
/**
* @internal
*/
class FileStorageCacheProvider
{
private string $modified_timestamps = '';
private Cache $cache;
private const FILE_STORAGE_CACHE_DIRECTORY = 'file_cache';
public function __construct(Config $config)
{
$this->cache = new Cache($config);
$storage_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'Storage' . DIRECTORY_SEPARATOR;
$dependent_files = [
$storage_dir . 'FileStorage.php',
$storage_dir . 'FunctionLikeStorage.php',
$storage_dir . 'ClassLikeStorage.php',
$storage_dir . 'MethodStorage.php',
$storage_dir . 'FunctionLikeParameter.php',
];
if ($config->eventDispatcher->hasAfterClassLikeVisitHandlers()) {
$dependent_files = array_merge($dependent_files, $config->plugin_paths);
}
foreach ($dependent_files as $dependent_file_path) {
if (!file_exists($dependent_file_path)) {
throw new UnexpectedValueException($dependent_file_path . ' must exist');
}
$this->modified_timestamps .= ' ' . filemtime($dependent_file_path);
}
$this->modified_timestamps .= $config->computeHash();
}
public function writeToCache(FileStorage $storage, string $file_contents): void
{
$file_path = strtolower($storage->file_path);
$storage->hash = $this->getCacheHash($file_path, $file_contents);
$this->storeInCache($file_path, $storage);
}
/**
* @param lowercase-string $file_path
*/
protected function storeInCache(string $file_path, FileStorage $storage): void
{
$cache_location = $this->getCacheLocationForPath($file_path, true);
$this->cache->saveItem($cache_location, $storage);
}
public function getLatestFromCache(string $file_path, string $file_contents): ?FileStorage
{
$file_path = strtolower($file_path);
$cached_value = $this->loadFromCache($file_path);
if (!$cached_value) {
return null;
}
$cache_hash = $this->getCacheHash($file_path, $file_contents);
/** @psalm-suppress TypeDoesNotContainType */
if (@get_class($cached_value) === '__PHP_Incomplete_Class'
|| $cache_hash !== $cached_value->hash
) {
$this->removeCacheForFile($file_path);
return null;
}
return $cached_value;
}
public function removeCacheForFile(string $file_path): void
{
$this->cache->deleteItem($this->getCacheLocationForPath($file_path));
}
private function getCacheHash(string $_unused_file_path, string $file_contents): string
{
// do not concatenate, as $file_contents can be big and performance will be bad
// the timestamp is only needed if we don't have file contents
// as same contents should give same results, independent of when file was modified
$data = $file_contents ? $file_contents : $this->modified_timestamps;
return PHP_VERSION_ID >= 8_01_00 ? hash('xxh128', $data) : hash('md4', $data);
}
/**
* @param lowercase-string $file_path
*/
protected function loadFromCache(string $file_path): ?FileStorage
{
$storage = $this->cache->getItem($this->getCacheLocationForPath($file_path));
if ($storage instanceof FileStorage) {
return $storage;
}
return null;
}
private function getCacheLocationForPath(string $file_path, bool $create_directory = false): string
{
$root_cache_directory = $this->cache->getCacheDirectory();
if (!$root_cache_directory) {
throw new UnexpectedValueException('No cache directory defined');
}
$parser_cache_directory = $root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_STORAGE_CACHE_DIRECTORY;
if ($create_directory && !is_dir($parser_cache_directory)) {
try {
if (mkdir($parser_cache_directory, 0777, true) === false) {
// any other error than directory already exists/permissions issue
throw new RuntimeException(
'Failed to create ' . $parser_cache_directory . ' cache directory for unknown reasons',
);
}
} catch (RuntimeException $e) {
// Race condition (#4483)
if (!is_dir($parser_cache_directory)) {
// rethrow the error with default message
// it contains the reason why creation failed
throw $e;
}
}
}
if (PHP_VERSION_ID >= 8_01_00) {
$hash = hash('xxh128', $file_path);
} else {
$hash = hash('md4', $file_path);
}
return $parser_cache_directory
. DIRECTORY_SEPARATOR
. $hash
. ($this->cache->use_igbinary ? '-igbinary' : '');
}
}