Repository navigation
Expand file tree
/
Copy pathFileHashComputer.php
More file actions
47 lines (37 loc) · 1.46 KB
/
Copy pathFileHashComputer.php
File metadata and controls
47 lines (37 loc) · 1.46 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
<?php
declare(strict_types=1);
namespace Rector\Caching\Config;
use Rector\Application\VersionResolver;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
use Rector\FileSystem\FilePathHelper;
/**
* Inspired by https://github.com/symplify/easy-coding-standard/blob/e598ab54686e416788f28fcfe007fd08e0f371d9/packages/changed-files-detector/src/FileHashComputer.php
*/
final readonly class FileHashComputer
{
public function __construct(
private FilePathHelper $filePathHelper
) {
}
public function compute(string $filePath): string
{
$this->ensureIsPhp($filePath);
$parametersHash = SimpleParameterProvider::hashForCacheInvalidation();
// relative config path, so the hash (and the whole cache) is not tied to one directory
$relativeFilePath = $this->filePathHelper->relativePath($this->filePathHelper->resolveRealPath($filePath));
return sha1($relativeFilePath . $parametersHash . VersionResolver::PACKAGE_VERSION);
}
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
));
}
}