/
ProjectCacheProvider.php
143 lines (107 loc) · 3.8 KB
/
ProjectCacheProvider.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
<?php
namespace Psalm\Internal\Provider;
use Psalm\Config;
use Psalm\Internal\Provider\Providers;
use function file_exists;
use function file_put_contents;
use function filemtime;
use function hash;
use function mkdir;
use function touch;
use const DIRECTORY_SEPARATOR;
use const PHP_VERSION_ID;
/**
* Used to determine which files reference other files, necessary for using the --diff
* option from the command line.
*
* @internal
*/
class ProjectCacheProvider
{
private const GOOD_RUN_NAME = 'good_run';
private const COMPOSER_LOCK_HASH = 'composer_lock_hash';
/**
* @var int|null
*/
private $last_run;
/**
* @var string|null
*/
private $composer_lock_hash;
private $composer_lock_location;
public function __construct(string $composer_lock_location)
{
$this->composer_lock_location = $composer_lock_location;
}
public function canDiffFiles(): bool
{
$cache_directory = Config::getInstance()->getCacheDirectory();
return $cache_directory && file_exists($cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME);
}
public function processSuccessfulRun(float $start_time, string $psalm_version): void
{
$cache_directory = Config::getInstance()->getCacheDirectory();
if (!$cache_directory) {
return;
}
$run_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME;
file_put_contents($run_cache_location, $psalm_version);
touch($run_cache_location, (int)$start_time);
}
public function getLastRun(string $psalm_version): int
{
if ($this->last_run === null) {
$cache_directory = Config::getInstance()->getCacheDirectory();
$run_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME;
if (file_exists($run_cache_location) && Providers::safeFileGetContents($run_cache_location) === $psalm_version) {
$this->last_run = filemtime($run_cache_location);
} else {
$this->last_run = 0;
}
}
return $this->last_run;
}
public function hasLockfileChanged(): bool
{
if (!file_exists($this->composer_lock_location)) {
return true;
}
$lockfile_contents = Providers::safeFileGetContents($this->composer_lock_location);
if (!$lockfile_contents) {
return true;
}
if (PHP_VERSION_ID >= 8_01_00) {
$hash = hash('xxh128', $lockfile_contents);
} else {
$hash = hash('md4', $lockfile_contents);
}
$changed = $hash !== $this->getComposerLockHash();
$this->composer_lock_hash = $hash;
return $changed;
}
public function updateComposerLockHash(): void
{
$cache_directory = Config::getInstance()->getCacheDirectory();
if (!$cache_directory || !$this->composer_lock_hash) {
return;
}
if (!file_exists($cache_directory)) {
mkdir($cache_directory, 0777, true);
}
$lock_hash_location = $cache_directory . DIRECTORY_SEPARATOR . self::COMPOSER_LOCK_HASH;
file_put_contents($lock_hash_location, $this->composer_lock_hash);
}
protected function getComposerLockHash(): string
{
if ($this->composer_lock_hash === null) {
$cache_directory = Config::getInstance()->getCacheDirectory();
$lock_hash_location = $cache_directory . DIRECTORY_SEPARATOR . self::COMPOSER_LOCK_HASH;
if (file_exists($lock_hash_location)) {
$this->composer_lock_hash = Providers::safeFileGetContents($lock_hash_location) ?: '';
} else {
$this->composer_lock_hash = '';
}
}
return $this->composer_lock_hash;
}
}