Skip to content

Commit

Permalink
move Utils to OneBot\Util
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Jan 7, 2022
1 parent 1cfb355 commit b8183a6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
33 changes: 1 addition & 32 deletions src/OneBot/V12/Utils.php → src/OneBot/Util/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/V12/Action/ActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/V12/Action/ReplAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/V12/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
41 changes: 39 additions & 2 deletions src/OneBot/V12/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
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;
use OneBot\V12\Object\ActionObject;
use OneBot\V12\Object\Event\OneBotEvent;
use OneBot\V12\OneBot;
use OneBot\V12\RetCode;
use OneBot\V12\Utils;

abstract class Driver
{
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit b8183a6

Please sign in to comment.