-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathFileHashComputer.php
More file actions
57 lines (47 loc) · 1.69 KB
/
FileHashComputer.php
File metadata and controls
57 lines (47 loc) · 1.69 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
<?php
declare(strict_types=1);
namespace Rector\Caching\Config;
use Rector\Application\VersionResolver;
use Rector\Caching\Contract\CacheMetaExtensionInterface;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
/**
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
*/
final class FileHashComputer
{
/**
* @param CacheMetaExtensionInterface[] $cacheMetaExtensions
*/
public function __construct(
private readonly array $cacheMetaExtensions = []
) {
}
public function compute(string $filePath): string
{
$this->ensureIsPhp($filePath);
$parametersHash = SimpleParameterProvider::hash();
$extensionHash = $this->computeExtensionHash();
return sha1($filePath . $parametersHash . $extensionHash . VersionResolver::PACKAGE_VERSION);
}
private function computeExtensionHash(): string
{
$extensionHash = '';
foreach ($this->cacheMetaExtensions as $cacheMetaExtension) {
$extensionHash .= $cacheMetaExtension->getKey() . ':' . $cacheMetaExtension->getHash();
}
return $extensionHash;
}
private function ensureIsPhp(string $filePath): void
{
$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
if ($fileExtension === 'php') {
return;
}
throw new ShouldNotHappenException(sprintf(
// getRealPath() cannot be used, as it breaks in phar
'Provide only PHP file, ready for Dependency Injection. "%s" given',
$filePath
));
}
}