diff --git a/composer.json b/composer.json index 809a44e2..7334a405 100644 --- a/composer.json +++ b/composer.json @@ -23,11 +23,19 @@ "swoole/ide-helper": "@dev", "ext-mbstring": "*", "swlib/saber": "^1.0", - "doctrine/annotations": "<1.10.2", + "doctrine/annotations": "~1.10", "ext-json": "*", "ext-posix": "*", "ext-ctype": "*", "ext-pdo": "*", "psy/psysh": "@stable" + }, + "autoload": { + "psr-4": { + "Custom\\": "src/Custom", + "Framework\\": "src/Framework", + "ZM\\": "src/ZM", + "Module\\": "src/Module" + } } } diff --git a/src/Framework/FrameworkLoader.php b/src/Framework/FrameworkLoader.php index 6925792c..f62adb41 100644 --- a/src/Framework/FrameworkLoader.php +++ b/src/Framework/FrameworkLoader.php @@ -43,9 +43,19 @@ class FrameworkLoader $this->requireGlobalFunctions(); if (LOAD_MODE == 0) define("WORKING_DIR", getcwd()); - elseif(LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../")); + elseif (LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../")); elseif (LOAD_MODE == 2) echo "Phar mode: " . WORKING_DIR . PHP_EOL; - $this->registerAutoloader('classLoader'); + //$this->registerAutoloader('classLoader'); + require_once "DataProvider.php"; + if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) { + require_once DataProvider::getWorkingDir() . "/vendor/autoload.php"; + } + if (LOAD_MODE == 2) { + require_once FRAMEWORK_DIR . "/vendor/autoload.php"; + spl_autoload_register('phar_classloader'); + } + + self::$settings = new GlobalConfig(); if (self::$settings->get("debug_mode") === true) { $args[] = "--debug-mode"; @@ -130,7 +140,7 @@ class FrameworkLoader private function defineProperties() { define("ZM_START_TIME", microtime(true)); define("ZM_DATA", self::$settings->get("zm_data")); - define("ZM_VERSION", json_decode(file_get_contents(__DIR__."/../../composer.json"), true)["version"] ?? "unknown"); + define("ZM_VERSION", json_decode(file_get_contents(__DIR__ . "/../../composer.json"), true)["version"] ?? "unknown"); define("CONFIG_DIR", self::$settings->get("config_dir")); define("CRASH_DIR", self::$settings->get("crash_dir")); @mkdir(ZM_DATA); diff --git a/src/Framework/global_functions.php b/src/Framework/global_functions.php index d0af5c3a..daf8315a 100644 --- a/src/Framework/global_functions.php +++ b/src/Framework/global_functions.php @@ -8,11 +8,12 @@ use ZM\Context\ContextInterface; use ZM\Utils\ZMUtil; -function classLoader($p) { +function phar_classloader($p){ $filepath = getClassPath($p); - if ($filepath === null) - echo "F:Warning: get class path wrongs.$p\n"; - //else echo "F:DBG: Found " . $p . "\n"; + if($filepath === null) { + Console::debug("F:Warning: get class path wrongs.$p"); + return; + } try { require_once $filepath; } catch (Exception $e) { diff --git a/src/Module/Example/Hello.php b/src/Module/Example/Hello.php index 8f35a377..ee52080e 100644 --- a/src/Module/Example/Hello.php +++ b/src/Module/Example/Hello.php @@ -30,9 +30,10 @@ class Hello /** * 向机器人发送"你好",即可回复这句话 - * @CQCommand("你好") + * @CQCommand(match="你好",alias={"你好啊","你是谁"}) */ public function hello() { + eval(ZM_BREAKPOINT); return "你好啊,我是由炸毛框架构建的机器人!"; } @@ -66,7 +67,6 @@ class Hello * @Middleware("timer") */ public function timer() { - eval(ZM_BREAKPOINT); return "This page is used as testing TimerMiddleware! Do not use it in production."; } diff --git a/src/ZM/Annotation/CQ/CQCommand.php b/src/ZM/Annotation/CQ/CQCommand.php index 55848d13..2db1693e 100644 --- a/src/ZM/Annotation/CQ/CQCommand.php +++ b/src/ZM/Annotation/CQ/CQCommand.php @@ -19,6 +19,8 @@ class CQCommand extends AnnotationBase implements Level public $match = ""; /** @var string */ public $regexMatch = ""; + /** @var string[] */ + public $alias = []; /** @var int */ public $level = 20; @@ -32,4 +34,4 @@ class CQCommand extends AnnotationBase implements Level */ public function setLevel(int $level) { $this->level = $level; } -} \ No newline at end of file +} diff --git a/src/ZM/Event/CQ/MessageEvent.php b/src/ZM/Event/CQ/MessageEvent.php index b07b150c..8a4d486d 100644 --- a/src/ZM/Event/CQ/MessageEvent.php +++ b/src/ZM/Event/CQ/MessageEvent.php @@ -40,7 +40,7 @@ class MessageEvent public function onBefore() { $obj_list = ZMBuf::$events[CQBefore::class]["message"] ?? []; foreach ($obj_list as $v) { - if($v->level < 200) break; + if ($v->level < 200) break; EventHandler::callWithMiddleware( $v->class, $v->method, @@ -65,7 +65,7 @@ class MessageEvent } } foreach (ZMBuf::$events[CQBefore::class]["message"] ?? [] as $v) { - if($v->level >= 200) continue; + if ($v->level >= 200) continue; $c = $v->class; if (ctx()->getCache("level") != 0) continue; EventHandler::callWithMiddleware( @@ -117,6 +117,13 @@ class MessageEvent return true; }); return; + } elseif (in_array($word[0], $v->alias)) { + Console::debug("Calling $c -> {$v->method}"); + $this->function_call = EventHandler::callWithMiddleware($obj[$c], $v->method, $class_construct, [$word], function ($r) { + if (is_string($r)) context()->reply($r); + return true; + }); + return; } elseif ($v->regexMatch != "" && ($args = matchArgs($v->regexMatch, context()->getMessage())) !== false) { Console::debug("Calling $c -> {$v->method}"); $this->function_call = EventHandler::callWithMiddleware($obj[$c], $v->method, $class_construct, [$args], function ($r) {