From 7540e688a93206b1f05188765c12724e2b7c5011 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Fri, 7 Jan 2022 14:46:51 +0800 Subject: [PATCH 1/4] delete redundant Console class --- src/OneBot/V12/Console.php | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/OneBot/V12/Console.php diff --git a/src/OneBot/V12/Console.php b/src/OneBot/V12/Console.php deleted file mode 100644 index 58c6642..0000000 --- a/src/OneBot/V12/Console.php +++ /dev/null @@ -1,23 +0,0 @@ - Date: Fri, 7 Jan 2022 14:48:18 +0800 Subject: [PATCH 2/4] move MPUtils to OneBot\Util --- src/OneBot/{V12 => Util}/MPUtils.php | 2 +- src/OneBot/V12/Driver/SwooleDriver.php | 2 +- src/OneBot/V12/Driver/WorkermanDriver.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/OneBot/{V12 => Util}/MPUtils.php (95%) diff --git a/src/OneBot/V12/MPUtils.php b/src/OneBot/Util/MPUtils.php similarity index 95% rename from src/OneBot/V12/MPUtils.php rename to src/OneBot/Util/MPUtils.php index 92ce553..b568211 100644 --- a/src/OneBot/V12/MPUtils.php +++ b/src/OneBot/Util/MPUtils.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace OneBot\V12; +namespace OneBot\Util; class MPUtils { diff --git a/src/OneBot/V12/Driver/SwooleDriver.php b/src/OneBot/V12/Driver/SwooleDriver.php index 3bc782b..885e852 100644 --- a/src/OneBot/V12/Driver/SwooleDriver.php +++ b/src/OneBot/V12/Driver/SwooleDriver.php @@ -6,9 +6,9 @@ use Error; use MessagePack\MessagePack; +use OneBot\Util\MPUtils; use OneBot\V12\Action\ActionResponse; use OneBot\V12\Exception\OneBotFailureException; -use OneBot\V12\MPUtils; use OneBot\V12\Object\Event\OneBotEvent; use OneBot\V12\RetCode; use Swoole\Http\Request; diff --git a/src/OneBot/V12/Driver/WorkermanDriver.php b/src/OneBot/V12/Driver/WorkermanDriver.php index fffa0c2..d17a3b7 100644 --- a/src/OneBot/V12/Driver/WorkermanDriver.php +++ b/src/OneBot/V12/Driver/WorkermanDriver.php @@ -6,10 +6,10 @@ use Error; use MessagePack\MessagePack; +use OneBot\Util\MPUtils; use OneBot\V12\Action\ActionResponse; use OneBot\V12\Driver\Workerman\Worker; use OneBot\V12\Exception\OneBotFailureException; -use OneBot\V12\MPUtils; use OneBot\V12\Object\Event\OneBotEvent; use OneBot\V12\RetCode; use Throwable; From 1cfb3552141f337e91515dc75cc3da731e8604d0 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Fri, 7 Jan 2022 14:52:03 +0800 Subject: [PATCH 3/4] adjust demo Action Handler and interfaces --- .../V12/{ => Action}/ExtendedActionInterface.php | 2 +- src/OneBot/V12/Action/ReplAction.php | 4 ++++ src/OneBot/V12/ActionHandler.php | 12 ------------ 3 files changed, 5 insertions(+), 13 deletions(-) rename src/OneBot/V12/{ => Action}/ExtendedActionInterface.php (71%) delete mode 100644 src/OneBot/V12/ActionHandler.php diff --git a/src/OneBot/V12/ExtendedActionInterface.php b/src/OneBot/V12/Action/ExtendedActionInterface.php similarity index 71% rename from src/OneBot/V12/ExtendedActionInterface.php rename to src/OneBot/V12/Action/ExtendedActionInterface.php index 29a4c0a..3c3d462 100644 --- a/src/OneBot/V12/ExtendedActionInterface.php +++ b/src/OneBot/V12/Action/ExtendedActionInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace OneBot\V12; +namespace OneBot\V12\Action; interface ExtendedActionInterface { diff --git a/src/OneBot/V12/Action/ReplAction.php b/src/OneBot/V12/Action/ReplAction.php index d000794..84737a9 100644 --- a/src/OneBot/V12/Action/ReplAction.php +++ b/src/OneBot/V12/Action/ReplAction.php @@ -7,6 +7,10 @@ use OneBot\V12\Object\ActionObject; use OneBot\V12\Utils; +/** + * Demo REPL Action Handler. + * Just for test. + */ class ReplAction extends ActionBase { public function onSendMessage(ActionObject $action): ActionResponse diff --git a/src/OneBot/V12/ActionHandler.php b/src/OneBot/V12/ActionHandler.php deleted file mode 100644 index 889a455..0000000 --- a/src/OneBot/V12/ActionHandler.php +++ /dev/null @@ -1,12 +0,0 @@ - Date: Fri, 7 Jan 2022 15:48:19 +0800 Subject: [PATCH 4/4] move Utils to OneBot\Util --- src/OneBot/{V12 => Util}/Utils.php | 33 +--------------------- src/OneBot/V12/Action/ActionBase.php | 2 +- src/OneBot/V12/Action/ReplAction.php | 2 +- src/OneBot/V12/Config/Config.php | 2 +- src/OneBot/V12/Driver/Driver.php | 41 ++++++++++++++++++++++++++-- 5 files changed, 43 insertions(+), 37 deletions(-) rename src/OneBot/{V12 => Util}/Utils.php (52%) diff --git a/src/OneBot/V12/Utils.php b/src/OneBot/Util/Utils.php similarity index 52% rename from src/OneBot/V12/Utils.php rename to src/OneBot/Util/Utils.php index b69b962..eb34cbb 100644 --- a/src/OneBot/V12/Utils.php +++ b/src/OneBot/Util/Utils.php @@ -2,10 +2,8 @@ declare(strict_types=1); -namespace OneBot\V12; +namespace OneBot\Util; -use OneBot\V12\Action\ActionBase; -use OneBot\V12\Exception\OneBotFailureException; use PASVL\Validation\ValidatorBuilder; class Utils @@ -43,35 +41,6 @@ public static function msgToString($message): string return $result; } - /** - * @throws OneBotFailureException - */ - public static function getActionFuncName(ActionBase $handler, string $action) - { - if (isset(ActionBase::$core_cache[$action])) { - return ActionBase::$core_cache[$action]; - } - if (isset(ActionBase::$ext_cache[$action])) { - return ActionBase::$ext_cache[$action]; - } - if (substr( - $action, - 0, - strlen(OneBot::getInstance()->getPlatform()) + 1 - ) === (OneBot::getInstance()->getPlatform() . '.')) { - $func = self::separatorToCamel('ext_' . substr($action, strlen(OneBot::getInstance()->getPlatform()) + 1)); - if (method_exists($handler, $func)) { - return ActionBase::$ext_cache[$action] = $func; - } - } else { - $func = self::separatorToCamel('on_' . $action); - if (method_exists($handler, $func)) { - return ActionBase::$core_cache[$action] = $func; - } - } - throw new OneBotFailureException(RetCode::UNSUPPORTED_ACTION); - } - /** * 验证 $input 是否符合指定 $pattern. * diff --git a/src/OneBot/V12/Action/ActionBase.php b/src/OneBot/V12/Action/ActionBase.php index 4dd1568..16031e5 100644 --- a/src/OneBot/V12/Action/ActionBase.php +++ b/src/OneBot/V12/Action/ActionBase.php @@ -4,10 +4,10 @@ namespace OneBot\V12\Action; +use OneBot\Util\Utils; use OneBot\V12\Object\ActionObject; use OneBot\V12\OneBot; use OneBot\V12\RetCode; -use OneBot\V12\Utils; use ReflectionClass; abstract class ActionBase diff --git a/src/OneBot/V12/Action/ReplAction.php b/src/OneBot/V12/Action/ReplAction.php index 84737a9..3f51034 100644 --- a/src/OneBot/V12/Action/ReplAction.php +++ b/src/OneBot/V12/Action/ReplAction.php @@ -4,8 +4,8 @@ namespace OneBot\V12\Action; +use OneBot\Util\Utils; use OneBot\V12\Object\ActionObject; -use OneBot\V12\Utils; /** * Demo REPL Action Handler. diff --git a/src/OneBot/V12/Config/Config.php b/src/OneBot/V12/Config/Config.php index 19806b0..e1110a0 100644 --- a/src/OneBot/V12/Config/Config.php +++ b/src/OneBot/V12/Config/Config.php @@ -4,8 +4,8 @@ namespace OneBot\V12\Config; +use OneBot\Util\Utils; use OneBot\V12\Exception\OneBotException; -use OneBot\V12\Utils; class Config extends \Noodlehaus\Config implements ConfigInterface { diff --git a/src/OneBot/V12/Driver/Driver.php b/src/OneBot/V12/Driver/Driver.php index 62a5849..8a77cc9 100644 --- a/src/OneBot/V12/Driver/Driver.php +++ b/src/OneBot/V12/Driver/Driver.php @@ -8,6 +8,8 @@ use MessagePack\MessagePack; use OneBot\Http\Client\StreamClient; use OneBot\Http\Client\SwooleClient; +use OneBot\Util\Utils; +use OneBot\V12\Action\ActionBase; use OneBot\V12\Action\ActionResponse; use OneBot\V12\Config\ConfigInterface; use OneBot\V12\Exception\OneBotFailureException; @@ -15,7 +17,6 @@ use OneBot\V12\Object\Event\OneBotEvent; use OneBot\V12\OneBot; use OneBot\V12\RetCode; -use OneBot\V12\Utils; abstract class Driver { @@ -26,6 +27,8 @@ abstract class Driver protected $alt_client_class; + protected $_events = []; + public function __construct($default_client_class = SwooleClient::class, $alt_client_class = StreamClient::class) { $this->default_client_class = $default_client_class; @@ -83,8 +86,42 @@ protected function emitHttpRequest($raw_data, int $type = ONEBOT_JSON): ActionRe } // 解析调用action handler $action_handler = OneBot::getInstance()->getActionHandler(); - $action_call_func = Utils::getActionFuncName($action_handler, $action_obj->action); + $action_call_func = $this->getActionFuncName($action_handler, $action_obj->action); + if ($action_call_func === null) { + return ActionResponse::create($action_obj->echo)->fail(RetCode::UNSUPPORTED_ACTION); + } $response_obj = $action_handler->{$action_call_func}($action_obj); return $response_obj instanceof ActionResponse ? $response_obj : ActionResponse::create($action_obj->echo)->fail(RetCode::BAD_HANDLER); } + + /** + * @throws OneBotFailureException + * @return mixed|string + */ + private function getActionFuncName(ActionBase $handler, string $action) + { + if (isset(ActionBase::$core_cache[$action])) { + return ActionBase::$core_cache[$action]; + } + + if (isset(ActionBase::$ext_cache[$action])) { + return ActionBase::$ext_cache[$action]; + } + if (substr( + $action, + 0, + strlen(OneBot::getInstance()->getPlatform()) + 1 + ) === (OneBot::getInstance()->getPlatform() . '.')) { + $func = Utils::separatorToCamel('ext_' . substr($action, strlen(OneBot::getInstance()->getPlatform()) + 1)); + if (method_exists($handler, $func)) { + return ActionBase::$ext_cache[$action] = $func; + } + } else { + $func = Utils::separatorToCamel('on_' . $action); + if (method_exists($handler, $func)) { + return ActionBase::$core_cache[$action] = $func; + } + } + throw new OneBotFailureException(RetCode::UNSUPPORTED_ACTION); + } }