-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsole.php
More file actions
219 lines (189 loc) · 5.59 KB
/
Copy pathConsole.php
File metadata and controls
219 lines (189 loc) · 5.59 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* @Author: ohmyga
* @Date: 2021-10-22 11:03:01
* @LastEditTime: 2022-04-17 15:52:00
*/
namespace OAPI\Console;
use OAPI\LogCat\LogCat;
class Console {
/**
* 默认颜色配置
*
* @access private
*/
private static $_default_use_color = [
"error" => "red",
"warning" => "yellow",
"info" => "lightblue",
"success" => "green",
"debug" => "gray"
];
/**
* 用户自定义主题配置
*
* @access private
*/
private static $_user_theme_config = [];
/**
* 颜色值列表
*
* @access private
*/
private static $_color_list = [];
/**
* 颜色配置
*
* @access private
*/
private static $_color = [];
/**
* 初始化控制台
*/
public static function init($level, $theme = "default", array $theme_config = [])
{
self::$_user_theme_config = $theme_config;
Theme::init(self::$_default_use_color, self::$_user_theme_config);
self::$_color = Theme::get($theme);
self::$_color_list = Theme::getColorList();
}
/**
* 设置字体颜色
*
* @param string $text
* @param string $color
* @return string
*/
public static function color($text, $color = "")
{
$color = count((array)self::$_color_list) > 0 && !empty(self::$_color_list[$color]) ? self::$_color_list[$color] : TermColor::color8(37);
return $color . $text . TermColor::RESET;
}
/**
* 替换字体颜色模板字符串
*
* @param string $text
* @return mixed
*/
public static function strRpColor($text)
{
if (!preg_match('/(?!{r}){.*?}/i', $text)) return $text;
foreach (self::$_color_list as $key => $item) {
$text = str_replace('{' . $key . '}', $item, $text);
}
$text = str_replace("{r}", TermColor::RESET, $text);
return $text;
}
/**
* 抛出一条错误信息
*
* @param string $message 错误信息
* @param string $module 触发模块
* @return mixed
*/
public static function error($message, $module = null)
{
echo self::__logFunc("E", ($module !== null) ? $module : null, $message, "error", true);
}
/**
* 输出一条警告信息
*
* @param string $message 警告信息
* @param string $module 触发模块
* @return mixed
*/
public static function warning($message, $module = null)
{
echo self::__logFunc("W", ($module !== null) ? $module : null, $message, "warning", true);
}
/**
* 输出普通信息
*
* @param string $message 信息
* @param string $module 触发模块
* @return mixed
*/
public static function info($message, $module = null)
{
echo self::__logFunc("I", ($module !== null) ? $module : null, $message, "info", true);
}
/**
* 输出一条操作成功的信息
*
* @param string $message 成功信息
* @param string $module 触发模块
* @return mixed
*/
public static function success($message, $module = null)
{
echo self::__logFunc("S", ($module !== null) ? $module : null, $message, "success", true);
}
/**
* 输出一条 DEBUG INFO
*
* @param string $message DEBUG
* @param string $module 触发模块
* @return mixed
*/
public static function debug($message, $module = null)
{
if (__OAPI_DEBUG__ === false) return false;
echo self::__logFunc("D", ($module !== null) ? $module : null, $message, "debug", true);
}
/**
* 指定类型日志输出函数
*/
private static function __logFunc($head, $module = null, $message = "", $color = "", $saveLog = true) {
$text = self::getHead($head);
$text .= ($module !== null) ? "[{$module}] " : " ";
$text .= $message;
if ($saveLog === true) LogCat::run($text . PHP_EOL);
$text = self::color($text, Theme::get()[$color]);
$text = self::strRpColor($text);
$text .= PHP_EOL;
return $text;
}
/**
* 输出一条自定义消息
*
* @param mixed $body 输出内容
* @param bool $time 是否包含时间
* @param string|null $module 是否包含模块名称
* @param bool $eol 输出当前自定义消息吼是否换行
* @param bool $ret 直接输出或是返回
* @param bool $saveLog 是否将控制台输出保存在临时日志文件中
* @return void
*/
public static function diy($body, $time = false, $module = null, $eol = false, $ret = false, $saveLog = true)
{
$text = ($time === true) ? date("[Y-m-d H:i:s]") : "";
$text .= ($module !== null) ? "[{$module}] " : " ";
$text .= $body;
$text .= ($eol === true) ? PHP_EOL : "";
if ($saveLog === true) LogCat::run($text . ($eol === true) ? "" : PHP_EOL);
$text = self::strRpColor($text);
if ($ret === true) return $text;
else echo $text;
}
/**
* 获取控制台的头 (?)
*
* @param string $head
* @return mixed
*/
private static function getHead($head)
{
$text = date("[Y-m-d H:i:s]") . "[{$head}]";
return $text;
}
/**
* 获取颜色值列表
*
* @return array
*/
public static function getColorList(): array
{
if (count((array)self::$_color_list) > 0) return self::$_color_list;
return [];
}
}