/
ClassLikeStorageCacheProvider.php
182 lines (143 loc) · 5.51 KB
/
ClassLikeStorageCacheProvider.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
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Psalm\Internal\Provider;
use Psalm\Config;
use Psalm\Storage\ClassLikeStorage;
use UnexpectedValueException;
use function array_merge;
use function dirname;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
use function get_class;
use function hash;
use function igbinary_serialize;
use function igbinary_unserialize;
use function is_dir;
use function is_null;
use function mkdir;
use function serialize;
use function strtolower;
use function unlink;
use function unserialize;
use const DIRECTORY_SEPARATOR;
use const PHP_VERSION_ID;
/**
* @internal
*/
class ClassLikeStorageCacheProvider
{
/**
* @var Config
*/
private $config;
/**
* @var string
*/
private $modified_timestamps = '';
private const CLASS_CACHE_DIRECTORY = 'class_cache';
public function __construct(Config $config)
{
$this->config = $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',
];
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 .= $this->config->computeHash();
}
public function writeToCache(ClassLikeStorage $storage, string $file_path, string $file_contents): void
{
$fq_classlike_name_lc = strtolower($storage->name);
$storage->hash = $this->getCacheHash($file_path, $file_contents);
// check if we have it in cache already
$cached_value = $this->loadFromCache($fq_classlike_name_lc, $file_path);
if (!is_null($cached_value) && $cached_value->hash === $storage->hash) {
return;
}
$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true);
if ($this->config->use_igbinary) {
file_put_contents($cache_location, igbinary_serialize($storage));
} else {
file_put_contents($cache_location, serialize($storage));
}
}
public function getLatestFromCache(
string $fq_classlike_name_lc,
?string $file_path,
?string $file_contents
): ClassLikeStorage {
$cached_value = $this->loadFromCache($fq_classlike_name_lc, $file_path);
if (!$cached_value) {
throw new UnexpectedValueException($fq_classlike_name_lc . ' should be in cache');
}
$cache_hash = $this->getCacheHash($file_path, $file_contents);
/** @psalm-suppress TypeDoesNotContainType */
if (@get_class($cached_value) === '__PHP_Incomplete_Class'
|| $cache_hash !== $cached_value->hash
) {
unlink($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path));
throw new UnexpectedValueException($fq_classlike_name_lc . ' should not be outdated');
}
return $cached_value;
}
private function getCacheHash(?string $file_path, ?string $file_contents): string
{
$data = $file_contents ? $file_contents : $this->modified_timestamps;
return PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
}
/**
* @psalm-suppress MixedAssignment
*/
private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path): ?ClassLikeStorage
{
$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path);
if (file_exists($cache_location)) {
if ($this->config->use_igbinary) {
$storage = igbinary_unserialize((string)file_get_contents($cache_location));
if ($storage instanceof ClassLikeStorage) {
return $storage;
}
return null;
}
$storage = unserialize((string)file_get_contents($cache_location));
if ($storage instanceof ClassLikeStorage) {
return $storage;
}
return null;
}
return null;
}
private function getCacheLocationForClass(
string $fq_classlike_name_lc,
?string $file_path,
bool $create_directory = false
): string {
$root_cache_directory = $this->config->getCacheDirectory();
if (!$root_cache_directory) {
throw new UnexpectedValueException('No cache directory defined');
}
$parser_cache_directory = $root_cache_directory . DIRECTORY_SEPARATOR . self::CLASS_CACHE_DIRECTORY;
if ($create_directory && !is_dir($parser_cache_directory)) {
mkdir($parser_cache_directory, 0777, true);
}
$data = $file_path ? strtolower($file_path) . ' ' : '';
$data .= $fq_classlike_name_lc;
$file_path_sha = PHP_VERSION_ID >= 80100 ? hash('xxh128', $data) : hash('md4', $data);
return $parser_cache_directory
. DIRECTORY_SEPARATOR
. $file_path_sha
. ($this->config->use_igbinary ? '-igbinary' : '');
}
}