mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-20 23:25:35 +08:00
initial 2.0.0-a2 commit
This commit is contained in:
@@ -18,6 +18,7 @@ use ZM\Store\ZMBuf;
|
||||
|
||||
class Context implements ContextInterface
|
||||
{
|
||||
public static $context = [];
|
||||
private $cid;
|
||||
|
||||
public function __construct($cid) { $this->cid = $cid; }
|
||||
@@ -25,31 +26,31 @@ class Context implements ContextInterface
|
||||
/**
|
||||
* @return swoole_server|null
|
||||
*/
|
||||
public function getServer() { return ZMBuf::$context[$this->cid]["server"] ?? null; }
|
||||
public function getServer() { return self::$context[$this->cid]["server"] ?? null; }
|
||||
|
||||
/**
|
||||
* @return Frame|null
|
||||
*/
|
||||
public function getFrame() { return ZMBuf::$context[$this->cid]["frame"] ?? null; }
|
||||
public function getFrame() { return self::$context[$this->cid]["frame"] ?? null; }
|
||||
|
||||
public function getFd() { return ZMBuf::$context[$this->cid]["fd"] ?? $this->getFrame()->fd ?? null; }
|
||||
public function getFd() { return self::$context[$this->cid]["fd"] ?? $this->getFrame()->fd ?? null; }
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getData() { return ZMBuf::$context[$this->cid]["data"] ?? null; }
|
||||
public function getData() { return self::$context[$this->cid]["data"] ?? null; }
|
||||
|
||||
public function setData($data) { ZMBuf::$context[$this->cid]["data"] = $data; }
|
||||
public function setData($data) { self::$context[$this->cid]["data"] = $data; }
|
||||
|
||||
/**
|
||||
* @return Request|null
|
||||
*/
|
||||
public function getRequest() { return ZMBuf::$context[$this->cid]["request"] ?? null; }
|
||||
public function getRequest() { return self::$context[$this->cid]["request"] ?? null; }
|
||||
|
||||
/**
|
||||
* @return Response|null
|
||||
*/
|
||||
public function getResponse() { return ZMBuf::$context[$this->cid]["response"] ?? null; }
|
||||
public function getResponse() { return self::$context[$this->cid]["response"] ?? null; }
|
||||
|
||||
/** @return ConnectionObject|null */
|
||||
public function getConnection() { return ManagerGM::get($this->getFd()); }
|
||||
@@ -67,33 +68,33 @@ class Context implements ContextInterface
|
||||
return $conn instanceof ConnectionObject ? new ZMRobot($conn) : null;
|
||||
}
|
||||
|
||||
public function getMessage() { return ZMBuf::$context[$this->cid]["data"]["message"] ?? null; }
|
||||
public function getMessage() { return self::$context[$this->cid]["data"]["message"] ?? null; }
|
||||
|
||||
public function setMessage($msg) { ZMBuf::$context[$this->cid]["data"]["message"] = $msg; }
|
||||
public function setMessage($msg) { self::$context[$this->cid]["data"]["message"] = $msg; }
|
||||
|
||||
public function getUserId() { return $this->getData()["user_id"] ?? null; }
|
||||
|
||||
public function setUserId($id) { ZMBuf::$context[$this->cid]["data"]["user_id"] = $id; }
|
||||
public function setUserId($id) { self::$context[$this->cid]["data"]["user_id"] = $id; }
|
||||
|
||||
public function getGroupId() { return $this->getData()["group_id"] ?? null; }
|
||||
|
||||
public function setGroupId($id) { ZMBuf::$context[$this->cid]["data"]["group_id"] = $id; }
|
||||
public function setGroupId($id) { self::$context[$this->cid]["data"]["group_id"] = $id; }
|
||||
|
||||
public function getDiscussId() { return $this->getData()["discuss_id"] ?? null; }
|
||||
|
||||
public function setDiscussId($id) { ZMBuf::$context[$this->cid]["data"]["discuss_id"] = $id; }
|
||||
public function setDiscussId($id) { self::$context[$this->cid]["data"]["discuss_id"] = $id; }
|
||||
|
||||
public function getMessageType() { return $this->getData()["message_type"] ?? null; }
|
||||
|
||||
public function setMessageType($type) { ZMBuf::$context[$this->cid]["data"]["message_type"] = $type; }
|
||||
public function setMessageType($type) { self::$context[$this->cid]["data"]["message_type"] = $type; }
|
||||
|
||||
public function getRobotId() { return $this->getData()["self_id"] ?? null; }
|
||||
|
||||
public function getCache($key) { return ZMBuf::$context[$this->cid]["cache"][$key] ?? null; }
|
||||
public function getCache($key) { return self::$context[$this->cid]["cache"][$key] ?? null; }
|
||||
|
||||
public function setCache($key, $value) { ZMBuf::$context[$this->cid]["cache"][$key] = $value; }
|
||||
public function setCache($key, $value) { self::$context[$this->cid]["cache"][$key] = $value; }
|
||||
|
||||
public function getCQResponse() { return ZMBuf::$context[$this->cid]["cq_response"] ?? null; }
|
||||
public function getCQResponse() { return self::$context[$this->cid]["cq_response"] ?? null; }
|
||||
|
||||
/**
|
||||
* only can used by cq->message event function
|
||||
@@ -114,8 +115,6 @@ class Context implements ContextInterface
|
||||
return (new ZMRobot($conn))->setCallback($yield)->sendGroupMsg($data["group_id"], $msg);
|
||||
case "private":
|
||||
return (new ZMRobot($conn))->setCallback($yield)->sendPrivateMsg($data["user_id"], $msg);
|
||||
case "discuss":
|
||||
return (new ZMRobot($conn))->setCallback($yield)->sendDiscussMsg($data["discuss_id"], $msg);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -123,7 +122,7 @@ class Context implements ContextInterface
|
||||
}
|
||||
|
||||
public function finalReply($msg, $yield = false) {
|
||||
ZMBuf::$context[$this->cid]["cache"]["block_continue"] = true;
|
||||
self::$context[$this->cid]["cache"]["block_continue"] = true;
|
||||
if ($msg == "") return true;
|
||||
return $this->reply($msg, $yield);
|
||||
}
|
||||
@@ -141,8 +140,8 @@ class Context implements ContextInterface
|
||||
if (!isset($this->getData()["user_id"], $this->getData()["message"], $this->getData()["self_id"]))
|
||||
throw new InvalidArgumentException("协程等待参数缺失");
|
||||
$cid = Co::getuid();
|
||||
$api_id = ZMBuf::$atomics["wait_msg_id"]->get();
|
||||
ZMBuf::$atomics["wait_msg_id"]->add(1);
|
||||
$api_id = ZMBuf::atomic("wait_msg_id")->get();
|
||||
ZMBuf::atomic("wait_msg_id")->add(1);
|
||||
$hang = [
|
||||
"coroutine" => $cid,
|
||||
"user_id" => $this->getData()["user_id"],
|
||||
@@ -206,9 +205,9 @@ class Context implements ContextInterface
|
||||
}
|
||||
|
||||
public function cloneFromParent() {
|
||||
set_coroutine_params(ZMBuf::$context[Co::getPcid()] ?? ZMBuf::$context[$this->cid]);
|
||||
set_coroutine_params(self::$context[Co::getPcid()] ?? self::$context[$this->cid]);
|
||||
return context();
|
||||
}
|
||||
|
||||
public function copy() { return ZMBuf::$context[$this->cid]; }
|
||||
public function copy() { return self::$context[$this->cid]; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user