Lines
100.00%
12 / 12
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| SourceScanner | 100.00% | 12 / 12 | 100.00% | 2 / 2 | 6 | 100.00% | 1 / 1 | |||
| scanFingerprint | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | |||||
| describeFile | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Broker\Reload; | |
| 6 | ||
| 7 | use RuntimeException; | |
| 8 | use SplFileInfo; | |
| 9 | use Symfony\Component\Finder\Finder; | |
| 10 | ||
| 11 | final readonly class SourceScanner | |
| 12 | { | |
| 13 | private const string PHP_FILES = '*.php'; | |
| 14 | ||
| 15 | /** @param list<string> $paths */ | |
| 16 | public function scanFingerprint(array $paths): SourceFingerprint | |
| 17 | { | |
| 18 | clearstatcache(); | |
| 19 | $entries = []; | |
| 20 | ||
| 21 | foreach ($paths as $path) { | |
| 22 | if (!is_dir($path)) { | |
| 23 | $entries[] = $this->describeFile(new SplFileInfo($path)); | |
| 24 | ||
| 25 | continue; | |
| 26 | } | |
| 27 | ||
| 28 | foreach (new Finder()->files()->in($path)->name(self::PHP_FILES)->sortByName() as $file) { | |
| 29 | $entries[] = $this->describeFile($file); | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | return new SourceFingerprint(hash('xxh128', implode("\n", $entries))); | |
| 34 | } | |
| 35 | ||
| 36 | // The inode catches an editor that saves by renaming a new file over the old one. | |
| 37 | private function describeFile(SplFileInfo $file): string | |
| 38 | { | |
| 39 | try { | |
| 40 | return \sprintf('%s|%d|%d|%d', $file->getPathname(), $file->getMTime(), $file->getSize(), $file->getInode()); | |
| 41 | } catch (RuntimeException) { | |
| 42 | return $file->getPathname() . '|absent'; | |
| 43 | } | |
| 44 | } | |
| 45 | } |