1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Services; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Hyde; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Helper methods to interact with the filecache. The filecache is used to compare |
9
|
|
|
* published Blade views with the original Blade views in the Hyde Framework |
10
|
|
|
* so the user can be warned before overwriting their customizations. |
11
|
|
|
* |
12
|
|
|
* @see \Hyde\Framework\Testing\Feature\Services\ChecksumServiceTest |
13
|
|
|
*/ |
14
|
|
|
class ChecksumService |
15
|
|
|
{ |
16
|
|
|
public static function getFilecache(): array |
17
|
|
|
{ |
18
|
|
|
$filecache = []; |
19
|
|
|
|
20
|
|
|
$files = glob(Hyde::vendorPath('resources/views/**/*.blade.php')); |
21
|
|
|
|
22
|
|
|
foreach ($files as $file) { |
23
|
|
|
$filecache[str_replace(Hyde::vendorPath(), '', $file)] = [ |
24
|
|
|
'unixsum' => static::unixsumFile($file), |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $filecache; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getChecksums(): array |
32
|
|
|
{ |
33
|
|
|
$cache = static::getFilecache(); |
34
|
|
|
|
35
|
|
|
$checksums = []; |
36
|
|
|
|
37
|
|
|
foreach ($cache as $file) { |
38
|
|
|
$checksums[] = $file['unixsum']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $checksums; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function checksumMatchesAny(string $checksum): bool |
45
|
|
|
{ |
46
|
|
|
return in_array($checksum, static::getChecksums()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* A EOL agnostic wrapper for calculating MD5 checksums. |
51
|
|
|
* |
52
|
|
|
* @internal This function is not cryptographically secure. |
53
|
|
|
* |
54
|
|
|
* @see https://github.com/hydephp/framework/issues/85 |
55
|
|
|
*/ |
56
|
|
|
public static function unixsum(string $string): string |
57
|
|
|
{ |
58
|
|
|
$string = str_replace(["\r\n", "\r"], "\n", $string); |
59
|
|
|
|
60
|
|
|
return md5($string); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/* Shorthand for @see static::unixsum() but loads a file */ |
64
|
|
|
public static function unixsumFile(string $file): string |
65
|
|
|
{ |
66
|
|
|
return static::unixsum(file_get_contents($file)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|