-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathFilesSnapshot.php
More file actions
91 lines (70 loc) · 2.19 KB
/
FilesSnapshot.php
File metadata and controls
91 lines (70 loc) · 2.19 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
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Cache;
use ReflectionClass;
use function array_unique;
use function is_file;
use function Safe\filemtime;
class FilesSnapshot
{
/** @param array<string, int> $dependencies */
private function __construct(
private readonly array $dependencies,
)
{
}
/** @param list<string> $files */
public static function for(array $files): self
{
$dependencies = [];
foreach (array_unique($files) as $file) {
if (! is_file($file)) {
continue;
}
$dependencies[$file] = filemtime($file);
}
return new self($dependencies);
}
public static function forClass(ReflectionClass $class, bool $withInheritance = false): self
{
return self::for(
self::dependencies($class, $withInheritance),
);
}
public static function alwaysUnchanged(): self
{
return new self([]);
}
/** @return list<string> */
private static function dependencies(ReflectionClass $class, bool $withInheritance = false): array
{
$filename = $class->getFileName();
// Internal classes are treated as always the same, e.g. you'll have to drop the cache between PHP versions.
if ($filename === false) {
return [];
}
$files = [$filename];
if (! $withInheritance) {
return $files;
}
if ($class->getParentClass() !== false) {
$files = [...$files, ...self::dependencies($class->getParentClass(), $withInheritance)];
}
foreach ($class->getTraits() as $trait) {
$files = [...$files, ...self::dependencies($trait, $withInheritance)];
}
foreach ($class->getInterfaces() as $interface) {
$files = [...$files, ...self::dependencies($interface, $withInheritance)];
}
return $files;
}
public function changed(): bool
{
foreach ($this->dependencies as $filename => $modificationTime) {
if ($modificationTime !== filemtime($filename)) {
return true;
}
}
return false;
}
}