mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 08:35:35 +08:00
Fit for docker.
This commit is contained in:
@@ -14,7 +14,8 @@ use Exception;
|
|||||||
|
|
||||||
class Console
|
class Console
|
||||||
{
|
{
|
||||||
static function setColor($string, $color = "") {
|
static function setColor($string, $color = "")
|
||||||
|
{
|
||||||
switch ($color) {
|
switch ($color) {
|
||||||
case "red":
|
case "red":
|
||||||
return "\x1b[38;5;203m" . $string . "\x1b[m";
|
return "\x1b[38;5;203m" . $string . "\x1b[m";
|
||||||
@@ -41,7 +42,8 @@ class Console
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static function error($obj, $head = null) {
|
static function error($obj, $head = null)
|
||||||
|
{
|
||||||
if ($head === null) $head = date("[H:i:s ") . "ERROR] ";
|
if ($head === null) $head = date("[H:i:s ") . "ERROR] ";
|
||||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||||
@@ -56,7 +58,8 @@ class Console
|
|||||||
echo(self::setColor($head . ($trace ?? "") . $obj, "red") . "\n");
|
echo(self::setColor($head . ($trace ?? "") . $obj, "red") . "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static function warning($obj, $head = null) {
|
static function warning($obj, $head = null)
|
||||||
|
{
|
||||||
if ($head === null) $head = date("[H:i:s") . " WARN] ";
|
if ($head === null) $head = date("[H:i:s") . " WARN] ";
|
||||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||||
@@ -71,7 +74,8 @@ class Console
|
|||||||
echo(self::setColor($head . ($trace ?? "") . $obj, "yellow") . "\n");
|
echo(self::setColor($head . ($trace ?? "") . $obj, "yellow") . "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static function info($obj, $head = null) {
|
static function info($obj, $head = null)
|
||||||
|
{
|
||||||
if ($head === null) $head = date("[H:i:s ") . "INFO] ";
|
if ($head === null) $head = date("[H:i:s ") . "INFO] ";
|
||||||
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
if (ZMBuf::$info_level !== null && in_array(ZMBuf::$info_level->get(), [1, 2])) {
|
||||||
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
$trace = debug_backtrace()[1] ?? ['file' => '', 'function' => ''];
|
||||||
@@ -86,12 +90,14 @@ class Console
|
|||||||
echo(self::setColor($head . ($trace ?? "") . $obj, "lightblue") . "\n");
|
echo(self::setColor($head . ($trace ?? "") . $obj, "lightblue") . "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static function log($obj, $color = "") {
|
static function log($obj, $color = "")
|
||||||
|
{
|
||||||
if (!is_string($obj)) var_dump($obj);
|
if (!is_string($obj)) var_dump($obj);
|
||||||
else echo(self::setColor($obj, $color) . "\n");
|
else echo(self::setColor($obj, $color) . "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static function msg($obj, $self_id = "") {
|
static function msg($obj, $self_id = "")
|
||||||
|
{
|
||||||
if (ZMBuf::$info_level !== null && ZMBuf::$info_level->get() == 3) {
|
if (ZMBuf::$info_level !== null && ZMBuf::$info_level->get() == 3) {
|
||||||
if (!isset($obj["post_type"])) {
|
if (!isset($obj["post_type"])) {
|
||||||
switch ($obj["action"]) {
|
switch ($obj["action"]) {
|
||||||
@@ -134,7 +140,8 @@ class Console
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static function stackTrace(){
|
static function stackTrace()
|
||||||
|
{
|
||||||
$log = "Stack trace:\n";
|
$log = "Stack trace:\n";
|
||||||
$trace = debug_backtrace();
|
$trace = debug_backtrace();
|
||||||
//array_shift($trace);
|
//array_shift($trace);
|
||||||
@@ -158,7 +165,12 @@ class Console
|
|||||||
echo $log;
|
echo $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function listenConsole(){
|
static function listenConsole()
|
||||||
|
{
|
||||||
|
if (in_array('--disable-console-input', FrameworkLoader::$argv)) {
|
||||||
|
self::info("ConsoleCommand disabled.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
go(function () {
|
go(function () {
|
||||||
while (true) {
|
while (true) {
|
||||||
$cmd = trim(co::fread(STDIN));
|
$cmd = trim(co::fread(STDIN));
|
||||||
@@ -171,14 +183,15 @@ class Console
|
|||||||
* @param string $cmd
|
* @param string $cmd
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private static function executeCommand(string $cmd) {
|
private static function executeCommand(string $cmd)
|
||||||
|
{
|
||||||
$it = explodeMsg($cmd);
|
$it = explodeMsg($cmd);
|
||||||
switch ($it[0] ?? '') {
|
switch ($it[0] ?? '') {
|
||||||
case 'call':
|
case 'call':
|
||||||
$class_name = $it[1];
|
$class_name = $it[1];
|
||||||
$function_name = $it[2];
|
$function_name = $it[2];
|
||||||
$class = new $class_name([]);
|
$class = new $class_name([]);
|
||||||
call_user_func_array([$class, $function_name],[]);
|
call_user_func_array([$class, $function_name], []);
|
||||||
return true;
|
return true;
|
||||||
case 'bc':
|
case 'bc':
|
||||||
$code = base64_decode($it[1] ?? '', true);
|
$code = base64_decode($it[1] ?? '', true);
|
||||||
@@ -205,7 +218,8 @@ class Console
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function withSleep(string $string, int $int) {
|
public static function withSleep(string $string, int $int)
|
||||||
|
{
|
||||||
self::info($string);
|
self::info($string);
|
||||||
sleep($int);
|
sleep($int);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ class FrameworkLoader
|
|||||||
|
|
||||||
/** @var float|string */
|
/** @var float|string */
|
||||||
public static $run_time;
|
public static $run_time;
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $argv;
|
||||||
|
|
||||||
/** @var Server */
|
/** @var Server */
|
||||||
private $server;
|
private $server;
|
||||||
@@ -30,6 +34,7 @@ class FrameworkLoader
|
|||||||
public function __construct($args = []) {
|
public function __construct($args = []) {
|
||||||
if (self::$instance !== null) die("Cannot run two FrameworkLoader in one process!");
|
if (self::$instance !== null) die("Cannot run two FrameworkLoader in one process!");
|
||||||
self::$instance = $this;
|
self::$instance = $this;
|
||||||
|
self::$argv = $args;
|
||||||
|
|
||||||
chdir(__DIR__ . '/../..');
|
chdir(__DIR__ . '/../..');
|
||||||
define('WORKING_DIR', getcwd());
|
define('WORKING_DIR', getcwd());
|
||||||
|
|||||||
Reference in New Issue
Block a user