Skip to content

Commit

Permalink
Merge pull request #30 from botuniverse/feature/optimize-namespace
Browse files Browse the repository at this point in the history
优化和调整部分类的命名空间
  • Loading branch information
crazywhalecc authored Jan 7, 2022
2 parents 4e92bdb + b8183a6 commit f4e9b7b
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/OneBot/V12/MPUtils.php → src/OneBot/Util/MPUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace OneBot\V12;
namespace OneBot\Util;

class MPUtils
{
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace OneBot\V12;
namespace OneBot\V12\Action;

interface ExtendedActionInterface
{
Expand Down
6 changes: 5 additions & 1 deletion src/OneBot/V12/Action/ReplAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions src/OneBot/V12/ActionHandler.php

This file was deleted.

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
23 changes: 0 additions & 23 deletions src/OneBot/V12/Console.php

This file was deleted.

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);
}
}
2 changes: 1 addition & 1 deletion src/OneBot/V12/Driver/SwooleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/V12/Driver/WorkermanDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f4e9b7b

Please sign in to comment.