mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 07:35:34 +08:00
update to 1.2 version
Generate systemd script Default info_level set to 2 Modify & add some comment for Example module Brand new Console Add daemon command argument Add #OnTick annotation Add ZMRobot API class
This commit is contained in:
@@ -14,8 +14,7 @@ use Exception;
|
||||
|
||||
class Console
|
||||
{
|
||||
static function setColor($string, $color = "")
|
||||
{
|
||||
static function setColor($string, $color = "") {
|
||||
switch ($color) {
|
||||
case "red":
|
||||
return "\x1b[38;5;203m" . $string . "\x1b[m";
|
||||
@@ -25,6 +24,7 @@ class Console
|
||||
return "\x1b[38;5;227m" . $string . "\x1b[m";
|
||||
case "blue":
|
||||
return "\033[34m" . $string . "\033[0m";
|
||||
case "pink": // I really don't know what stupid color it is.
|
||||
case "lightpurple":
|
||||
return "\x1b[38;5;207m" . $string . "\x1b[m";
|
||||
case "lightblue":
|
||||
@@ -33,8 +33,6 @@ class Console
|
||||
return "\x1b[38;5;214m" . $string . "\x1b[m";
|
||||
case "gray":
|
||||
return "\x1b[38;5;59m" . $string . "\x1b[m";
|
||||
case "pink":
|
||||
return "\x1b[38;5;207m" . $string . "\x1b[m";
|
||||
case "lightlightblue":
|
||||
return "\x1b[38;5;63m" . $string . "\x1b[m";
|
||||
default:
|
||||
@@ -42,9 +40,8 @@ class Console
|
||||
}
|
||||
}
|
||||
|
||||
static function error($obj, $head = null)
|
||||
{
|
||||
if ($head === null) $head = date("[H:i:s ") . "ERROR] ";
|
||||
static function error($obj, $head = null) {
|
||||
if ($head === null) $head = date("[H:i:s] ") . "[E] ";
|
||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||
$trace = "[" . basename($trace["file"], ".php") . ":" . $trace["function"] . "] ";
|
||||
@@ -58,90 +55,80 @@ class Console
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "red") . "\n");
|
||||
}
|
||||
|
||||
static function warning($obj, $head = null)
|
||||
{
|
||||
if ($head === null) $head = date("[H:i:s") . " WARN] ";
|
||||
static function warning($obj, $head = null) {
|
||||
if ($head === null) $head = date("[H:i:s]") . " [W] ";
|
||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||
$trace = "[" . basename($trace["file"], ".php") . ":" . $trace["function"] . "] ";
|
||||
}
|
||||
if (!is_string($obj)) {
|
||||
if (isset($trace)) {
|
||||
var_dump($obj);
|
||||
return;
|
||||
} else $obj = "{Object}";
|
||||
if(ZMBuf::$atomics["info_level"]->get() >= 1) {
|
||||
if (!is_string($obj)) {
|
||||
if (isset($trace)) {
|
||||
var_dump($obj);
|
||||
return;
|
||||
} else $obj = "{Object}";
|
||||
}
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "yellow") . "\n");
|
||||
}
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "yellow") . "\n");
|
||||
}
|
||||
|
||||
static function info($obj, $head = null)
|
||||
{
|
||||
if ($head === null) $head = date("[H:i:s ") . "INFO] ";
|
||||
static function info($obj, $head = null) {
|
||||
if ($head === null) $head = date("[H:i:s] ") . "[I] ";
|
||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||
$trace = "[" . basename($trace["file"], ".php") . ":" . $trace["function"] . "] ";
|
||||
}
|
||||
if (!is_string($obj)) {
|
||||
if (isset($trace)) {
|
||||
var_dump($obj);
|
||||
return;
|
||||
} else $obj = "{Object}";
|
||||
if(ZMBuf::$atomics["info_level"]->get() >= 2) {
|
||||
if (!is_string($obj)) {
|
||||
if (isset($trace)) {
|
||||
var_dump($obj);
|
||||
return;
|
||||
} else $obj = "{Object}";
|
||||
}
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "lightblue") . "\n");
|
||||
}
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "lightblue") . "\n");
|
||||
}
|
||||
|
||||
static function log($obj, $color = "")
|
||||
{
|
||||
static function success($obj, $head = null) {
|
||||
if ($head === null) $head = date("[H:i:s] ") . "[S] ";
|
||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||
$trace = "[" . basename($trace["file"], ".php") . ":" . $trace["function"] . "] ";
|
||||
}
|
||||
if(ZMBuf::$atomics["info_level"]->get() >= 2) {
|
||||
if (!is_string($obj)) {
|
||||
if (isset($trace)) {
|
||||
var_dump($obj);
|
||||
return;
|
||||
} else $obj = "{Object}";
|
||||
}
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "green") . "\n");
|
||||
}
|
||||
}
|
||||
|
||||
static function verbose($obj, $head = null) {
|
||||
if($head === null) $head = date("[H:i:s] ") . "[V] ";
|
||||
if(ZMBuf::$atomics["info_level"]->get() >= 3) {
|
||||
if (!is_string($obj)) {
|
||||
if (isset($trace)) {
|
||||
var_dump($obj);
|
||||
return;
|
||||
} else $obj = "{Object}";
|
||||
}
|
||||
echo(self::setColor($head . ($trace ?? "") . $obj, "blue") . "\n");
|
||||
}
|
||||
}
|
||||
|
||||
static function debug($obj) {
|
||||
debug($obj);
|
||||
}
|
||||
|
||||
static function log($obj, $color = "") {
|
||||
if (!is_string($obj)) var_dump($obj);
|
||||
else echo(self::setColor($obj, $color) . "\n");
|
||||
}
|
||||
|
||||
static function msg($obj, $self_id = "")
|
||||
{
|
||||
if (ZMBuf::$info_level !== null && ZMBuf::$info_level->get() == 3) {
|
||||
if (!isset($obj["post_type"])) {
|
||||
switch ($obj["action"]) {
|
||||
case "send_private_msg":
|
||||
$msg = Console::setColor(date("H:i:s ") . "[" . (ZMBuf::globals("robot_alias")[$self_id] ?? "Null") . "] ", "lightlightblue");
|
||||
$msg .= Console::setColor("私聊↑(" . $obj["params"]["user_id"] . ")", "lightlightblue");
|
||||
$msg .= Console::setColor(" > ", "gray");
|
||||
$msg .= $obj["params"]["message"];
|
||||
Console::log($msg);
|
||||
break;
|
||||
case "send_group_msg":
|
||||
//TODO: 写新的控制台消息(API消息处理)
|
||||
Console::log(Console::setColor("[" . date("H:i:s") . " GROUP:" . $obj["params"]["group_id"] . "] ", "blue") . Console::setColor($obj["params"]["user_id"] ?? "", "yellow") . Console::setColor(" > ", "gray") . ($obj["params"]["message"] ?? ""));
|
||||
break;
|
||||
case "send_discuss_msg":
|
||||
Console::log(Console::setColor("[" . date("H:i:s") . " DISCUSS:" . $obj["params"]["discuss_id"] . "] ", "blue") . Console::setColor($obj["params"]["user_id"] ?? "", "yellow") . Console::setColor(" > ", "gray") . ($obj["params"]["message"] ?? ""));
|
||||
break;
|
||||
case "send_msg":
|
||||
$obj["action"] = "send_" . $obj["message_type"] . "_msg";
|
||||
self::msg($obj);
|
||||
break;
|
||||
case "send_wechat_msg":
|
||||
Console::log(Console::setColor("[" . date("H:i:s") . " WECHAT] ", "blue") . Console::setColor($obj["params"]["user_id"] ?? "", "yellow") . Console::setColor(" > ", "gray") . ($obj["params"]["message"] ?? ""));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if ($obj["post_type"] == "message") {
|
||||
switch ($obj["message_type"]) {
|
||||
case "group":
|
||||
//
|
||||
//TODO: 写新的控制台消息(event处理)
|
||||
case "private":
|
||||
case "discuss":
|
||||
case "wechat":
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static function stackTrace()
|
||||
{
|
||||
static function stackTrace() {
|
||||
$log = "Stack trace:\n";
|
||||
$trace = debug_backtrace();
|
||||
//array_shift($trace);
|
||||
@@ -165,8 +152,7 @@ class Console
|
||||
echo $log;
|
||||
}
|
||||
|
||||
static function listenConsole()
|
||||
{
|
||||
static function listenConsole() {
|
||||
if (in_array('--disable-console-input', FrameworkLoader::$argv)) {
|
||||
self::info("ConsoleCommand disabled.");
|
||||
return;
|
||||
@@ -183,10 +169,18 @@ class Console
|
||||
* @param string $cmd
|
||||
* @return bool
|
||||
*/
|
||||
private static function executeCommand(string $cmd)
|
||||
{
|
||||
private static function executeCommand(string $cmd) {
|
||||
$it = explodeMsg($cmd);
|
||||
switch ($it[0] ?? '') {
|
||||
case 'logtest':
|
||||
Console::log(date("[H:i:s]"). " [L] This is normal msg. (0)");
|
||||
Console::error("This is error msg. (0)");
|
||||
Console::warning("This is warning msg. (1)");
|
||||
Console::info("This is info msg. (2)");
|
||||
Console::success("This is success msg. (2)");
|
||||
Console::verbose("This is verbose msg. (3)");
|
||||
Console::debug("This is debug msg. (4)");
|
||||
return true;
|
||||
case 'call':
|
||||
$class_name = $it[1];
|
||||
$function_name = $it[2];
|
||||
@@ -203,6 +197,9 @@ class Console
|
||||
case 'echo':
|
||||
Console::info($it[1]);
|
||||
return true;
|
||||
case 'color':
|
||||
Console::log($it[2], $it[1]);
|
||||
return true;
|
||||
case 'stop':
|
||||
ZMUtil::stop();
|
||||
return false;
|
||||
@@ -218,9 +215,8 @@ class Console
|
||||
}
|
||||
}
|
||||
|
||||
public static function withSleep(string $string, int $int)
|
||||
{
|
||||
public static function withSleep(string $string, int $int) {
|
||||
self::info($string);
|
||||
sleep($int);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
|
||||
namespace Framework;
|
||||
|
||||
use Co;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Runtime;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use ZM\Event\EventHandler;
|
||||
use Exception;
|
||||
use Swoole\WebSocket\Server;
|
||||
@@ -32,7 +35,8 @@ class FrameworkLoader
|
||||
/** @var Server */
|
||||
private $server;
|
||||
|
||||
public function __construct($args = []) {
|
||||
public function __construct($args = [])
|
||||
{
|
||||
if (self::$instance !== null) die("Cannot run two FrameworkLoader in one process!");
|
||||
self::$instance = $this;
|
||||
self::$argv = $args;
|
||||
@@ -52,17 +56,38 @@ class FrameworkLoader
|
||||
try {
|
||||
$this->server = new Server(self::$settings->get("host"), self::$settings->get("port"));
|
||||
if (in_array("--remote-shell", $args)) RemoteShell::listen($this->server, "127.0.0.1");
|
||||
$this->server->set(self::$settings->get("swoole"));
|
||||
$settings = self::$settings->get("swoole");
|
||||
if (in_array("--daemon", $args)) {
|
||||
$settings["daemonize"] = 1;
|
||||
Console::log("已启用守护进程,输出重定向到 " . $settings["log_file"]);
|
||||
self::$argv[] = "--disable-console-input";
|
||||
}
|
||||
$this->server->set($settings);
|
||||
$this->server->on("WorkerStart", [$this, "onWorkerStart"]);
|
||||
$this->server->on("message", function ($server, $frame) { EventHandler::callSwooleEvent("message", $server, $frame); });
|
||||
$this->server->on("message", function ($server, Frame $frame) {
|
||||
Console::debug("Calling Swoole \"message\" event from fd=" . $frame->fd);
|
||||
EventHandler::callSwooleEvent("message", $server, $frame);
|
||||
});
|
||||
$this->server->on("request", function ($request, $response) {
|
||||
$response = new Response($response);
|
||||
Console::debug("Receiving Http request event, cid=" . Co::getCid());
|
||||
EventHandler::callSwooleEvent("request", $request, $response);
|
||||
});
|
||||
$this->server->on("open", function ($server, $request) { EventHandler::callSwooleEvent("open", $server, $request); });
|
||||
$this->server->on("close", function ($server, $fd) { EventHandler::callSwooleEvent("close", $server, $fd); });
|
||||
$this->server->on("open", function ($server, Request $request) {
|
||||
Console::debug("Calling Swoole \"open\" event from fd=" . $request->fd);
|
||||
EventHandler::callSwooleEvent("open", $server, $request);
|
||||
});
|
||||
$this->server->on("close", function ($server, $fd) {
|
||||
Console::debug("Calling Swoole \"close\" event from fd=" . $fd);
|
||||
EventHandler::callSwooleEvent("close", $server, $fd);
|
||||
});
|
||||
ZMBuf::initAtomic();
|
||||
Console::info("host: ".self::$settings->get("host").", port: ".self::$settings->get("port"));
|
||||
if (in_array("--log-error", $args)) ZMBuf::$atomics["info_level"]->set(0);
|
||||
if (in_array("--log-warning", $args)) ZMBuf::$atomics["info_level"]->set(1);
|
||||
if (in_array("--log-info", $args)) ZMBuf::$atomics["info_level"]->set(2);
|
||||
if (in_array("--log-verbose", $args)) ZMBuf::$atomics["info_level"]->set(3);
|
||||
if (in_array("--log-debug", $args)) ZMBuf::$atomics["info_level"]->set(4);
|
||||
Console::log("host: " . self::$settings->get("host") . ", port: " . self::$settings->get("port") . ", log_level: " . ZMBuf::$atomics["info_level"]->get());
|
||||
$this->server->start();
|
||||
} catch (Exception $e) {
|
||||
Console::error("Framework初始化出现错误,请检查!");
|
||||
@@ -71,15 +96,18 @@ class FrameworkLoader
|
||||
}
|
||||
}
|
||||
|
||||
private function requireGlobalFunctions() {
|
||||
private function requireGlobalFunctions()
|
||||
{
|
||||
require __DIR__ . '/global_functions.php';
|
||||
}
|
||||
|
||||
private function registerAutoloader(string $string) {
|
||||
private function registerAutoloader(string $string)
|
||||
{
|
||||
if (!spl_autoload_register($string)) die("Failed to register autoloader named \"$string\" !");
|
||||
}
|
||||
|
||||
private function defineProperties() {
|
||||
private function defineProperties()
|
||||
{
|
||||
define("ZM_START_TIME", microtime(true));
|
||||
define("ZM_DATA", self::$settings->get("zm_data"));
|
||||
define("CONFIG_DIR", self::$settings->get("config_dir"));
|
||||
@@ -93,7 +121,8 @@ class FrameworkLoader
|
||||
define("ZM_MATCH_SECOND", 3);
|
||||
}
|
||||
|
||||
private function selfCheck() {
|
||||
private function selfCheck()
|
||||
{
|
||||
if (!extension_loaded("swoole")) die("Can not find swoole extension.\n");
|
||||
//if (!extension_loaded("gd")) die("Can not find gd extension.\n");
|
||||
if (!extension_loaded("sockets")) die("Can not find sockets extension.\n");
|
||||
@@ -105,7 +134,8 @@ class FrameworkLoader
|
||||
return true;
|
||||
}
|
||||
|
||||
public function onWorkerStart(\Swoole\Server $server, $worker_id) {
|
||||
public function onWorkerStart(\Swoole\Server $server, $worker_id)
|
||||
{
|
||||
self::$instance = $this;
|
||||
self::$run_time = microtime(true);
|
||||
EventHandler::callSwooleEvent("WorkerStart", $server, $worker_id);
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Framework;
|
||||
|
||||
|
||||
class InfoLevel
|
||||
{
|
||||
const INFO = 0;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Framework;
|
||||
|
||||
|
||||
use Swoole\Coroutine\System;
|
||||
|
||||
class Logger
|
||||
{
|
||||
private static function getTimeFormat($type = "I") {
|
||||
return "[" . date("Y-m-d H:i:s") . "]\t[" . $type . "]\t";
|
||||
}
|
||||
|
||||
public static function writeSwooleLog($log) {
|
||||
System::writeFile(CRASH_DIR . "swoole_error.log", "\n" . self::getTimeFormat() . $log, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ namespace Framework;
|
||||
use Swoole\Atomic;
|
||||
use swoole_atomic;
|
||||
use ZM\connection\WSConnection;
|
||||
use ZM\Utils\Scheduler;
|
||||
use ZM\Utils\SQLPool;
|
||||
|
||||
class ZMBuf
|
||||
@@ -22,8 +21,7 @@ class ZMBuf
|
||||
/** @var WSConnection[] */
|
||||
static $connect = [];//储存连接实例的数组
|
||||
//Scheduler计划任务连接实例,只可以在单worker_num时使用
|
||||
/** @var Scheduler|null */
|
||||
static $scheduler = null;
|
||||
static $scheduler = null; //This is stupid warning...
|
||||
|
||||
//Swoole SQL连接池,多进程下每个进程一个连接池
|
||||
/** @var SQLPool */
|
||||
@@ -52,6 +50,7 @@ class ZMBuf
|
||||
public static $req_mapping = [];
|
||||
public static $config = [];
|
||||
public static $context = [];
|
||||
public static $instance = [];
|
||||
|
||||
static function get($name, $default = null) {
|
||||
return self::$cache[$name] ?? $default;
|
||||
@@ -106,6 +105,7 @@ class ZMBuf
|
||||
self::$cache = [];
|
||||
self::$connect = [];
|
||||
self::$time_nlp = null;
|
||||
self::$instance = [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -176,6 +176,6 @@ function context() {
|
||||
}
|
||||
|
||||
function debug($msg) {
|
||||
if (ZMBuf::$atomics["info_level"]->get() == 1)
|
||||
Console::log(date("[H:i:s ") . "DEBUG] " . $msg, 'gray');
|
||||
if (ZMBuf::$atomics["info_level"]->get() >= 4)
|
||||
Console::log(date("[H:i:s] ") . "[D] " . $msg, 'gray');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user