tweet9ra/logux-processor

5.0.3 2020-06-21 22:15 UTC

README

本软件包允许您将 Logux 服务器用作日志服务器与您的 PHP 应用程序之间的代理。

此软件包的 Laravel 适配器

与 logux 后端协议的版本兼容性

快速入门

composer require tweet9ra/logux-processor

初始化

  • 加载配置
/**
* password - logux controll password, that you specify in logux proxy
* control_url - logux proxy http endpoint (usually https://:31338)
* protocol_version - version of protocol, default 2
*/
$app = new tweet9ra\Logux\App('password', 'control_url', 'protocol_version');
  • 设置路由
use \tweet9ra\Logux\ProcessableAction;

$app->setActionsMap([
    /**
    * If your logux proxy does not authenticate itself, you must specify this action
    */
    'auth' => function (array $loguxAuthCommand): bool {
        $userId = $loguxAuthCommand['userId'];
        $token = $loguxAuthCommand['token'];

        /* Anonymous login */
        if ($userId === 'false') {
            return true;
        }

        return function_that_validates_token($userId, $token);
    },

    /**
    * This action handle subscriptions to channels
    * You can use callback or match with assoative array
    */
    'logux/subscribe' => function (ProcessableAction $action) {},
    'logux/subscribe' => [
        'chats/:chatId' => function (ProcessableAction $action, $chatId) {},
        'users/:userId/alert/:alertType' => function (ProcessableAction $action, $userId, $alertType) {}
    ],

    // Your app actions
    'ADD_CHAT_MESSAGE' => function (ProcessableAction $action) {
        if (!user_can_add_messages($action->userId())) {
            // If you have an error while processing action you can use error method
            $action->error('You are not allowed to sent messages!');
            return;
        }

        /* Your logic */
    },

    // Or instead of closures you can specify callback method
    'ADD_CHAT_MESSAGE' => 'App\Controllers\ChatController@addMessage',
    'auth' => 'App\Controllers\AuthController@loguxAuth'
]);

所有您的回调函数都将\tweet9ra\Logux\ProcessableAction作为第一个参数,但可以被子类替换

/**
 * @property int $text Message text content
 * @property int $chatId Chat room id
 *
 * There is no useful features besides IDE autocompletion atm
*/
class AddChatMessage extends \tweet9ra\Logux\ProcessableAction {}

$app->setActionsMap([
    'ADD_CHAT_MESSAGE' => function (AddChatMessage $action) {
        create_chat_message($action->userId(), $action->text, $action->chatId);
        // Resend action to channels, users, clients or nodes
        $action->sendTo('channels', "chats/$action->chatId");
    }
]);

处理来自 logux 代理的请求

$input = file_get_contents('php://input');
$inputDecoded = json_decode($input, true);

$responseContent = $app->processRequest($inputDecoded);

echo json_encode($response);