forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangedFilesDetector.php
More file actions
156 lines (128 loc) · 4.92 KB
/
Copy pathChangedFilesDetector.php
File metadata and controls
156 lines (128 loc) · 4.92 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\Detector;
use Rector\Caching\Cache;
use Rector\Caching\Config\FileHashComputer;
use Rector\Caching\Enum\CacheKey;
use Rector\Caching\FileDependencyCollector;
use Rector\Util\FileHasher;
/**
* Inspired by https://github.com/symplify/symplify/pull/90/files#diff-72041b2e1029a08930e13d79d298ef11
*
* @see \Rector\Tests\Caching\Detector\ChangedFilesDetectorTest
*/
final class ChangedFilesDetector
{
/**
* @var array<string, true>
*/
private array $cacheableFiles = [];
public function __construct(
private readonly FileHashComputer $fileHashComputer,
private readonly Cache $cache,
private readonly FileHasher $fileHasher,
private readonly FileDependencyCollector $fileDependencyCollector
) {
}
public function cacheFile(string $filePath): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
if (! isset($this->cacheableFiles[$filePathCacheKey])) {
return;
}
// a failed capture means a possibly incomplete set, skip caching so the file is reprocessed
$dependencyHashes = $this->fileDependencyCollector->getDependencyFileHashes($filePath);
if ($dependencyHashes === null) {
return;
}
// the file may have just been written, recompute its hash fresh
// rather than trusting a memo entry from the pre-write filter pass
$this->fileDependencyCollector->forgetContentHash($filePath);
$ownHash = $this->fileDependencyCollector->contentHash($filePath);
if ($ownHash === null) {
return;
}
// store the own content hash plus one per dependency, so a dependency change
// invalidates this file even when its own content is unchanged
$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, [
'hash' => $ownHash,
'deps' => $dependencyHashes,
]);
}
public function addCacheableFile(string $filePath): void
{
$filePathCacheKey = $this->getFilePathCacheKey($filePath);
$this->cacheableFiles[$filePathCacheKey] = true;
}
public function hasFileChanged(string $filePath): bool
{
$fileInfoCacheKey = $this->getFilePathCacheKey($filePath);
$cachedValue = $this->cache->load($fileInfoCacheKey, CacheKey::FILE_HASH_KEY);
// no value to compare against → be defensive and assume changed
if ($cachedValue === null) {
return true;
}
// legacy string entry → own-hash comparison only, rewritten in the new format on next cacheFile()
if (is_string($cachedValue)) {
return $this->fileDependencyCollector->contentHash($filePath) !== $cachedValue;
}
if (! is_array($cachedValue)) {
return true;
}
// own content changed
if (($cachedValue['hash'] ?? null) !== $this->fileDependencyCollector->contentHash($filePath)) {
return true;
}
// any recorded dependency changed
return $this->fileDependencyCollector->hasAnyChangedDependency($cachedValue['deps'] ?? []);
}
public function invalidateFile(string $filePath): void
{
$fileInfoCacheKey = $this->getFilePathCacheKey($filePath);
$this->cache->clean($fileInfoCacheKey);
unset($this->cacheableFiles[$fileInfoCacheKey]);
}
public function clear(): void
{
$this->cache->clear();
}
/**
* @api
*/
public function setFirstResolvedConfigFileInfo(string $filePath): void
{
// the first config is core to all → if it was changed, just invalidate it
$configHash = $this->fileHashComputer->compute($filePath);
$this->storeConfigurationDataHash($filePath, $configHash);
}
private function resolvePath(string $filePath): string
{
$realPath = realpath($filePath);
if ($realPath === false) {
return $filePath;
}
return $realPath;
}
private function getFilePathCacheKey(string $filePath): string
{
return $this->fileHasher->hash($this->resolvePath($filePath));
}
private function storeConfigurationDataHash(string $filePath, string $configurationHash): void
{
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . $this->getFilePathCacheKey($filePath);
$this->invalidateCacheIfConfigurationChanged($key, $configurationHash);
$this->cache->save($key, CacheKey::CONFIGURATION_HASH_KEY, $configurationHash);
}
private function invalidateCacheIfConfigurationChanged(string $key, string $configurationHash): void
{
$oldCachedValue = $this->cache->load($key, CacheKey::CONFIGURATION_HASH_KEY);
if ($oldCachedValue === null) {
return;
}
if ($oldCachedValue === $configurationHash) {
return;
}
// should be unique per getcwd()
$this->clear();
}
}