-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.php
83 lines (70 loc) · 3.74 KB
/
test.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
<?php
function recursiveFindFiles($dir, $ext) {
if (!file_exists($dir)) { return; }
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
foreach($it as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == $ext) {
yield $file;
}
}
}
function getJSONLastError() {
switch (json_last_error()) {
case JSON_ERROR_NONE:
return 'No error has occurred';
case JSON_ERROR_DEPTH:
return 'The maximum stack depth has been exceeded';
case JSON_ERROR_STATE_MISMATCH:
return 'Invalid or malformed JSON';
case JSON_ERROR_CTRL_CHAR:
return 'Control character error, possibly incorrectly encoded';
case JSON_ERROR_SYNTAX:
return 'Syntax error';
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
case JSON_ERROR_RECURSION:
return 'One or more recursive references in the value to be encoded';
case JSON_ERROR_INF_OR_NAN:
return 'One or more NAN or INF values in the value to be encoded';
case JSON_ERROR_UNSUPPORTED_TYPE:
return 'A value of a type that cannot be encoded was given';
case JSON_ERROR_INVALID_PROPERTY_NAME:
return 'A property name that cannot be encoded was given';
case JSON_ERROR_UTF16:
return 'Malformed UTF-16 characters, possibly incorrectly encoded';
default:
return 'Unknown error';
}
}
function loadSetups($fromLocation) {
echo 'Loading setups...', "\n";
$hasError = false;
$newSetups = [];
$setupFiles = [];
if (!empty($fromLocation) && file_exists($fromLocation)) {
if (is_dir($fromLocation)) {
foreach (recursiveFindFiles($fromLocation, 'json') as $file) { $setupFiles[] = (string)$file; }
} else {
$setupFiles[] = $fromLocation;
}
}
sort($setupFiles);
foreach ($setupFiles as $file) {
echo "\t", 'Using file: ', $file, "\n";
$contents = file_get_contents($file);
// Remove comments.
$contents = preg_replace("#/\*.*?\*/#s", '', $contents);
$data = json_decode($contents, true);
if (is_array($data)) {
echo "\t\t", 'Found ', count($data), ' setup', (count($data) != 1 ? 's' : ''), '.', "\n";
$newSetups = array_merge($newSetups, $data);
} else {
echo "\t\t", 'Error reading setups - ', getJSONLastError(), "\n";
$hasError = true;
}
echo "", "\n";
}
echo 'Loaded ', count($newSetups), ' setup', (count($newSetups) != 1 ? 's' : ''), ' from ', $fromLocation, '.', "\n";
exit($hasError ? 1 : 0);
}
loadSetups(__DIR__);