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/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/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..3f51034 100644 --- a/src/OneBot/V12/Action/ReplAction.php +++ b/src/OneBot/V12/Action/ReplAction.php @@ -4,9 +4,13 @@ namespace OneBot\V12\Action; +use OneBot\Util\Utils; 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 @@ -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); + } } 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;