|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xmly; |
|
4
|
|
|
|
|
5
|
|
|
final class Util |
|
6
|
|
|
{ |
|
7
|
12 |
|
public static function jsonDecode($json, $assoc = false, $depth = 512) |
|
8
|
|
|
{ |
|
9
|
12 |
|
static $jsonErrors = array( |
|
10
|
|
|
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - Maximum stack depth exceeded', |
|
11
|
|
|
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch', |
|
12
|
|
|
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Unexpected control character found', |
|
13
|
|
|
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON', |
|
14
|
|
|
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded' |
|
15
|
|
|
); |
|
16
|
|
|
|
|
17
|
12 |
|
if (empty($json)) { |
|
18
|
|
|
return null; |
|
19
|
|
|
} |
|
20
|
12 |
|
$data = \json_decode($json, $assoc, $depth); |
|
21
|
|
|
|
|
22
|
12 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
23
|
|
|
$last = json_last_error(); |
|
24
|
|
|
throw new \InvalidArgumentException( |
|
25
|
|
|
'Unable to parse JSON data: ' |
|
26
|
|
|
. (isset($jsonErrors[$last]) |
|
27
|
|
|
? $jsonErrors[$last] |
|
28
|
|
|
: 'Unknown error') |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
12 |
|
return $data; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
public static function randomString($length = 11) |
|
36
|
|
|
{ |
|
37
|
6 |
|
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; |
|
38
|
6 |
|
$randStr = str_shuffle(str_repeat($str, $length));//打乱字符串 |
|
39
|
6 |
|
return substr($randStr, 0, $length); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
public static function msecTime() |
|
43
|
|
|
{ |
|
44
|
6 |
|
return floor(microtime(true) * 1000); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
6 |
|
public static function mkdir() |
|
48
|
|
|
{ |
|
49
|
6 |
|
if (!is_dir(Config::LOG_PATH)) { |
|
50
|
1 |
|
mkdir(Config::LOG_PATH, 0777, true); |
|
51
|
|
|
} |
|
52
|
6 |
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
public static function writeLog($logLevel, $url, $body, $statusCode = null, $duration = null, $error = null) |
|
55
|
|
|
{ |
|
56
|
6 |
|
self::mkdir(); |
|
57
|
6 |
|
date_default_timezone_set("Asia/Shanghai"); |
|
58
|
6 |
|
$logFileName = Config::LOG_PATH . $logLevel . '-' . date('Y-m-d-H') . '.log'; |
|
59
|
6 |
|
if ($logLevel === 'INFO') { |
|
60
|
6 |
|
$content = date('Y-m-d H:i:s') . ' [' . $logLevel . '] '; |
|
61
|
6 |
|
$content .= $statusCode . ' ' . $duration . ' ' . $url . ' ' . $body . "\n"; |
|
62
|
|
|
} else { |
|
63
|
1 |
|
$content = date('Y-m-d H:i:s') . ' [' . $logLevel . '] '; |
|
64
|
1 |
|
$content .= $statusCode . ' ' . $duration . ' ' . $url . ' ' . $body . ' ' . $error . "\n"; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
6 |
|
file_put_contents($logFileName, $content, FILE_APPEND); |
|
68
|
6 |
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
public static function writeInfoLog($url, $body, $statusCode, $duration) |
|
71
|
|
|
{ |
|
72
|
6 |
|
self::writeLog('INFO', $url, $body, $statusCode, $duration); |
|
73
|
6 |
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public static function writeErrLog($url, $body, $statusCode, $duration, $error) |
|
76
|
|
|
{ |
|
77
|
1 |
|
self::writeLog('ERROR', $url, $body, $statusCode, $duration, $error); |
|
78
|
1 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|