-
Notifications
You must be signed in to change notification settings - Fork 6
/
json-check.php
85 lines (69 loc) · 2.28 KB
/
json-check.php
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
<?php
declare(strict_types=1);
if ($argc == 1) {
$mode = '';
$dir = '.';
} elseif ($argc == 2) {
$mode = $argv[1];
$dir = '.';
} elseif ($argc == 3) {
$mode = $argv[1];
$dir = $argv[2];
} else {
die('JSON Checker cannot be run with this set of parameters!');
}
if (!in_array($mode, ['', 'fix'])) {
die('Unsupported mode "' . $mode . '"!');
}
$start = microtime(true);
$invalidFiles = jsonStyleCheck($dir, $mode);
$duration = microtime(true) - $start;
foreach ($invalidFiles as $invalidFile) {
echo $invalidFile . PHP_EOL;
}
if (!empty($invalidFiles)) {
echo PHP_EOL;
}
echo 'Checked all files in ' . number_format($duration, 3) . ' seconds, ' . number_format(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB memory used' . PHP_EOL;
if (!empty($invalidFiles)) {
exit(1);
}
function jsonStyleCheck(string $dir, string $mode)
{
$ignore = ['./.vscode', './.idea', './.git', './libs/vendor'];
$invalidFiles = [];
$files = scandir($dir);
foreach ($files as $file) {
if ($file != '.' && $file != '..' && !in_array($dir, $ignore)) {
if (is_dir($dir . '/' . $file)) {
$invalidFiles = array_merge($invalidFiles, jsonStyleCheck($dir . '/' . $file, $mode));
} else {
if (fnmatch('*.json', $dir . '/' . $file)) {
$invalidFile = checkContentInFile($dir . '/' . $file, $mode);
if ($invalidFile !== false) {
$invalidFiles[] = $invalidFile;
}
}
}
}
}
return $invalidFiles;
}
function checkContentInFile(string $dir, string $mode)
{
$fileOriginal = file_get_contents($dir);
// Normalize line endings
$fileOriginal = str_replace("\r\n", "\n", $fileOriginal);
$fileOriginal = str_replace("\r", "\n", $fileOriginal);
// Ignore line break at the end of the file
$fileOriginal = rtrim($fileOriginal, "\n");
// Reformat JSON using PHP
$fileCompare = json_encode(json_decode($fileOriginal), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION);
if ($fileOriginal == $fileCompare) {
return false;
}
if ($mode == 'fix') {
file_put_contents($dir, $fileCompare);
}
return $dir;
}