1. 程式人生 > >使用事件註冊器進行swoole代碼封裝

使用事件註冊器進行swoole代碼封裝

cti tle RKE 應用 receive roc function str 0.0.0.0

在使用swoole的時候,事件回調很難維護與編寫,寫起來很亂。特別在封裝一些代碼的時候,使用這種註冊,先註冊用戶自己定義的,然後註冊些默認的事件函數。

Server.php

class Server
{
    public function __construct()
    {
        $server   = new \swoole_http_server(‘0.0.0.0‘, 9501);
        // 創建一個註冊器,其實這個很簡單,就是將一些要執行的回調函數進統一以回調名為鍵,要執行的方法放在一個數組中[‘workerStart‘=>[callback]]
        $register
= new EventRegister(); // 首先註冊默認的 $this->finalHook($register); // 然後是用戶自定義的,所以只給定應用層這些接口來寫回調 WPHPEvent::mainServerCreate($register); $events = $register->all(); foreach ($events as $event => $callback) { $server->on($event
, function () use ($callback) { $args = func_get_args(); foreach ($callback as $item) { // 可以使用這三種方式調用 // call_user_func($item, ...$args); // call_user_func_array($item, $args); $item(...$args
); } }); } $server->start(); } private function finalHook(EventRegister $register) { $register->add(‘workerStart‘, function () { cli_set_process_title(‘wphp-work-zhangsan‘); }); // ... $register->add(‘request‘, function ($request, $response) { $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>"); }); } }
MultiContainer.php
class MultiContainer
{
    private $container = [];
    private $allowKeys = null;


    public function __construct(array $allowKeys)
    {
        $this->allowKeys = $allowKeys;
    }

    public function add($key, $item)
    {
        if (!in_array($key, $this->allowKeys)) {
            return false;
        }
        $this->container[$key][] = $item;
        return $this;
    }

    public function set($key, $item)
    {
        if (!in_array($key, $this->allowKeys)) {
            return false;
        }
        $this->container[$key] = [$item];
        return $this;
    }

    public function delete($key)
    {
        unset($this->container[$key]);
        return $this;
    }

    public function get($key)
    {
        if (isset($this->container[$key])) {
            return $this->container[$key];
        }
        return null;
    }

    public function all()
    {
        return $this->container;
    }

}

Event.php

class Event extends MultiContainer
{
    public function add($key, $item)
    {
        if (is_callable($item)) {
            return parent::add($key, $item);
        } else {
            return false;
        }
    }

    public function set($key, $item)
    {
        if (is_callable($item)) {
            return parent::set($key, $item);
        }
    }


}

EventRegister.php

class EventRegister extends Event
{
    public function __construct()
    {
        parent::__construct([
            ‘start‘, ‘shutdown‘, ‘workerStart‘, ‘workerStop‘, ‘workerExit‘, ‘timer‘,
            ‘connect‘, ‘receive‘, ‘packet‘, ‘close‘, ‘bufferFull‘, ‘bufferEmpty‘, ‘task‘,
            ‘finish‘, ‘pipeMessage‘, ‘workerError‘, ‘managerStart‘, ‘managerStop‘,
            ‘request‘, ‘handShake‘, ‘message‘, ‘open‘
        ]);
    }

    public function add($key, $item)
    {
        if (!parent::add($key, $item)) {
            throw new \Exception(‘錯誤的調用方式‘);
        }
        return $this;
    }

    public function set($key, $item)
    {
        if (!parent::set($key, $item)) {
            throw new \Exception(‘錯誤的調用方式‘);
        }
        return $this;
    }
}

WPHPEvent.php

class WPHPEvent
{
    public static function mainServerCreate($register)
    {
        $register->add(‘workerStart‘, function () {
            echo 22;
        });
    }
}

使用事件註冊器進行swoole代碼封裝