Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ViewDiffService
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 getViewFileHashIndex
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getChecksums
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 checksumMatchesAny
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Services;
6
7use Hyde\Hyde;
8
9use function Hyde\unixsum_file;
10use function str_replace;
11use function in_array;
12use function Hyde\unslash;
13use function glob;
14
15/**
16 * @internal This class may be refactored to better suit its intended purpose.
17 *
18 * Helper methods to interact with the virtual filecache that is used to compare
19 * published Blade views with the original Blade views in the Hyde Framework
20 * so the user can be warned before overwriting their customizations.
21 *
22 * Since we currently never use this class more than one in the request cycle,
23 * there is no reason to cache the results of the file index in the instance.
24 */
25class ViewDiffService
26{
27    /** @return array<string, array{unixsum: string}> */
28    public static function getViewFileHashIndex(): array
29    {
30        $filecache = [];
31
32        foreach (glob(Hyde::vendorPath('resources/views/**/*.blade.php')) as $file) {
33            $filecache[unslash(str_replace(Hyde::vendorPath(), '', (string) $file))] = [
34                'unixsum' => unixsum_file($file),
35            ];
36        }
37
38        return $filecache;
39    }
40
41    /** @return array<string> */
42    public static function getChecksums(): array
43    {
44        $checksums = [];
45
46        foreach (static::getViewFileHashIndex() as $file) {
47            $checksums[] = $file['unixsum'];
48        }
49
50        return $checksums;
51    }
52
53    public static function checksumMatchesAny(string $checksum): bool
54    {
55        return in_array($checksum, static::getChecksums());
56    }
57}