Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
FileCacheService
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 getFilecache
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getChecksums
100.00% covered (success)
100.00%
5 / 5
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
 unixsum
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 unixsumFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Hyde\Framework\Services;
4
5use Hyde\Framework\Hyde;
6
7/**
8 * Helper methods to interact with the filecache.
9 *
10 * @see \Tests\Feature\Services\FileCacheServiceTest
11 * @see \Tests\Unit\FileCacheServiceUnixsumMethodTest
12 */
13class FileCacheService
14{
15    public static function getFilecache(): array
16    {
17        $filecache = [];
18
19        $files = glob(Hyde::vendorPath('resources/views/**/*.blade.php'));
20
21        foreach ($files as $file) {
22            $filecache[str_replace(Hyde::vendorPath(), '', $file)] = [
23                'unixsum' => static::unixsumFile($file),
24            ];
25        }
26
27        return $filecache;
28    }
29
30    public static function getChecksums(): array
31    {
32        $cache = static::getFilecache();
33
34        $checksums = [];
35
36        foreach ($cache as $file) {
37            $checksums[] = $file['unixsum'];
38        }
39
40        return $checksums;
41    }
42
43    public static function checksumMatchesAny(string $checksum): bool
44    {
45        return in_array($checksum, static::getChecksums());
46    }
47
48    /**
49     * A EOL agnostic wrapper for calculating MD5 checksums.
50     *
51     * @internal This function is not cryptographically secure.
52     *
53     * @see https://github.com/hydephp/framework/issues/85
54     */
55    public static function unixsum(string $string): string
56    {
57        $string = str_replace(["\r\n", "\r"], "\n", $string);
58
59        return md5($string);
60    }
61
62    /* Shorthand for @see static::unixsum() but loads a file */
63    public static function unixsumFile(string $file): string
64    {
65        return static::unixsum(file_get_contents($file));
66    }
67}