mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-04 15:25:38 +08:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c41d17802 | ||
|
|
4ba5e3fc57 | ||
|
|
920680ee3d | ||
|
|
c632977ca1 | ||
|
|
94834c6625 | ||
|
|
e3ffeec782 | ||
|
|
438a204751 | ||
|
|
b0331f6346 | ||
|
|
0cbb81788e | ||
|
|
7173da9ad2 | ||
|
|
302e2c60da | ||
|
|
87e637f759 | ||
|
|
1d2aaf3c99 | ||
|
|
676527205f | ||
|
|
b560246efb | ||
|
|
c1f720a0b3 |
26
README.md
26
README.md
@@ -2,14 +2,14 @@
|
||||
|
||||
[]()
|
||||
[](https://github.com/zhamao-robot/zhamao-framework/blob/master/LICENSE)
|
||||
[]()
|
||||
[]()
|
||||
|
||||
[](https://github.com/zhamao-robot/zhamao-framework/search?q=goto)
|
||||
[](https://github.com/zhamao-robot/zhamao-framework/search?q=TODO)
|
||||
|
||||
一个异步、多平台兼容的 **聊天机器人** 框架。
|
||||
|
||||
<img src="./resources/images/logo.png" height = "200" alt="炸毛框架" align=center/>
|
||||
<img src="https://avatars0.githubusercontent.com/u/48620312" height = "200" alt="炸毛框架" align=center/>
|
||||
|
||||
## 简介
|
||||
zhamao-framework 是一个基于 酷Q 的 PHP Swoole 的机器人框架,它会对 QQ 机器人收到的消息进行解析处理,并以模块化的形式进行开发,来完成机器人的自然语言对话等功能。
|
||||
@@ -18,6 +18,9 @@ zhamao-framework 是一个基于 酷Q 的 PHP Swoole 的机器人框架,它会
|
||||
|
||||
除了起到解析消息的作用,炸毛框架 还提供了完整的 WebSocket + HTTP 服务器,你还能用此框架构建出高性能的 API 接口服务器。
|
||||
|
||||
## 开始
|
||||
你可以使用 GitHub 的 `Use This Template` 功能快速将本项目克隆到你的公开或私有仓库,也可以直接 clone 本项目到本地私有开发。
|
||||
|
||||
## 文档
|
||||
本项目文档正在努力编写中。
|
||||
|
||||
@@ -38,9 +41,24 @@ Pages:[https://framework.zhamao.xin/](https://framework.zhamao.xin/)
|
||||
| 模块名称 | 说明 | 模块地址 |
|
||||
| ------------------ | -------------------------------- | ------------------------------------------------------------ |
|
||||
| 微信公众号兼容模块 | 为框架提供微信公众号订阅号兼容层 | [zhamao-wechat-patch](https://github.com/zhamao-robot/zhamao-wechat-patch) |
|
||||
| 通用模块 | 图片上传和下载模块 | [zhamao-general-tools](https://github.com/zhamao-robot/zhamao-general-tools) |
|
||||
|
||||
|
||||
|
||||
## 计划开发内容
|
||||
- [ ] WebSocket测试脚本(客户端)
|
||||
- [X] Session 和中间层管理模块
|
||||
- [ ] 支持本地和远程两种方式的定时器(计划任务)
|
||||
- [ ] 常驻服务脚本
|
||||
- [ ] 一些常用的通用 API 例如经济(用户积分、亲密度等)的模块
|
||||
- [ ] 图灵机器人/腾讯AI 聊天模块
|
||||
- [ ] 分词模块(可能会放弃计划,因为目前好用的分词都是其他语言的)
|
||||
- [ ] HTTP 过滤器、Auth 模块、完整的 MVC 兼容(可能会放弃计划,因为框架主打机器人开发)
|
||||
- [ ] Redis 连接池或开箱即用的相应功能内置
|
||||
- [ ] 2.0 版本抛弃 `ModBase` 继承结构,完全使用上下文代替
|
||||
- [ ] 更好的 Logger,稳定和漂亮的控制台输出
|
||||
- [ ] 日志服务
|
||||
- [ ] 模块支持 Phar 打包(可能会比较靠后支持)
|
||||
- [ ] 兼容面向过程的模块编写方案(可能会比较靠后支持)
|
||||
- [ ] 完整的单元测试(如果有需求则尽快开发)
|
||||
|
||||
## 从 cqbot-swoole 升级
|
||||
目前新的框架采用了全新的注解机制,所以旧版的框架上写的模块到新框架需要重新编写。当然为了减少工作量,新的框架也最大限度地保留了旧版框架编写的风格,一般情况下根据新版框架的文档仅需修改少量地方即可完成重写。
|
||||
|
||||
@@ -37,8 +37,15 @@ switch ($argv[1] ?? '') {
|
||||
}
|
||||
$loader = new FrameworkLoader($argv);
|
||||
break;
|
||||
case '--help':
|
||||
case '-h':
|
||||
echo "\nUsage: ".$argv[0]." [OPTION]\n";
|
||||
echo "\nzhamao-framework start script, provides several startup arguments.";
|
||||
echo "\n\n -h, --help\t\tShow this help menu";
|
||||
echo "\n framework, server\tstart main framework, this is default option\n\n";
|
||||
break;
|
||||
default:
|
||||
echo "Unknown option \"{$argv[1]}\"!\n";
|
||||
echo "Unknown option \"{$argv[1]}\"!\n\"--help\" for more information\n";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
141
config/file_header.json
Normal file
141
config/file_header.json
Normal file
@@ -0,0 +1,141 @@
|
||||
{
|
||||
"ai": "application/postscript",
|
||||
"aif": "audio/x-aiff",
|
||||
"aifc": "audio/x-aiff",
|
||||
"aiff": "audio/x-aiff",
|
||||
"asc": "text/plain",
|
||||
"au": "audio/basic",
|
||||
"avi": "video/x-msvideo",
|
||||
"bcpio": "application/x-bcpio",
|
||||
"bin": "application/octet-stream",
|
||||
"bmp": "image/bmp",
|
||||
"cdf": "application/x-netcdf",
|
||||
"class": "application/octet-stream",
|
||||
"cpio": "application/x-cpio",
|
||||
"cpt": "application/mac-compactpro",
|
||||
"csh": "application/x-csh",
|
||||
"css": "text/css",
|
||||
"dcr": "application/x-director",
|
||||
"dir": "application/x-director",
|
||||
"djv": "image/vnd.djvu",
|
||||
"djvu": "image/vnd.djvu",
|
||||
"dll": "application/octet-stream",
|
||||
"dms": "application/octet-stream",
|
||||
"doc": "application/msword",
|
||||
"dvi": "application/x-dvi",
|
||||
"dxr": "application/x-director",
|
||||
"eps": "application/postscript",
|
||||
"etx": "text/x-setext",
|
||||
"exe": "application/octet-stream",
|
||||
"ez": "application/andrew-inset",
|
||||
"gif": "image/gif",
|
||||
"gtar": "application/x-gtar",
|
||||
"hdf": "application/x-hdf",
|
||||
"hqx": "application/mac-binhex40",
|
||||
"htm": "text/html",
|
||||
"html": "text/html",
|
||||
"ice": "x-conference/x-cooltalk",
|
||||
"ief": "image/ief",
|
||||
"iges": "model/iges",
|
||||
"igs": "model/iges",
|
||||
"jpe": "image/jpeg",
|
||||
"jpeg": "image/jpeg",
|
||||
"jpg": "image/jpeg",
|
||||
"js": "application/x-javascript",
|
||||
"kar": "audio/midi",
|
||||
"latex": "application/x-latex",
|
||||
"lha": "application/octet-stream",
|
||||
"lzh": "application/octet-stream",
|
||||
"m3u": "audio/x-mpegurl",
|
||||
"man": "application/x-troff-man",
|
||||
"me": "application/x-troff-me",
|
||||
"mesh": "model/mesh",
|
||||
"mid": "audio/midi",
|
||||
"midi": "audio/midi",
|
||||
"mif": "application/vnd.mif",
|
||||
"mov": "video/quicktime",
|
||||
"movie": "video/x-sgi-movie",
|
||||
"mp2": "audio/mpeg",
|
||||
"mp3": "audio/mpeg",
|
||||
"mpe": "video/mpeg",
|
||||
"mpeg": "video/mpeg",
|
||||
"mpg": "video/mpeg",
|
||||
"mpga": "audio/mpeg",
|
||||
"ms": "application/x-troff-ms",
|
||||
"msh": "model/mesh",
|
||||
"mxu": "video/vnd.mpegurl",
|
||||
"nc": "application/x-netcdf",
|
||||
"oda": "application/oda",
|
||||
"pbm": "image/x-portable-bitmap",
|
||||
"pdb": "chemical/x-pdb",
|
||||
"pdf": "application/pdf",
|
||||
"pgm": "image/x-portable-graymap",
|
||||
"pgn": "application/x-chess-pgn",
|
||||
"png": "image/png",
|
||||
"pnm": "image/x-portable-anymap",
|
||||
"ppm": "image/x-portable-pixmap",
|
||||
"ppt": "application/vnd.ms-powerpoint",
|
||||
"ps": "application/postscript",
|
||||
"qt": "video/quicktime",
|
||||
"ra": "audio/x-realaudio",
|
||||
"ram": "audio/x-pn-realaudio",
|
||||
"ras": "image/x-cmu-raster",
|
||||
"rgb": "image/x-rgb",
|
||||
"rm": "audio/x-pn-realaudio",
|
||||
"roff": "application/x-troff",
|
||||
"rpm": "audio/x-pn-realaudio-plugin",
|
||||
"rtf": "text/rtf",
|
||||
"rtx": "text/richtext",
|
||||
"sgm": "text/sgml",
|
||||
"sgml": "text/sgml",
|
||||
"sh": "application/x-sh",
|
||||
"shar": "application/x-shar",
|
||||
"silo": "model/mesh",
|
||||
"sit": "application/x-stuffit",
|
||||
"skd": "application/x-koan",
|
||||
"skm": "application/x-koan",
|
||||
"skp": "application/x-koan",
|
||||
"skt": "application/x-koan",
|
||||
"smi": "application/smil",
|
||||
"smil": "application/smil",
|
||||
"snd": "audio/basic",
|
||||
"so": "application/octet-stream",
|
||||
"spl": "application/x-futuresplash",
|
||||
"src": "application/x-wais-source",
|
||||
"sv4cpio": "application/x-sv4cpio",
|
||||
"sv4crc": "application/x-sv4crc",
|
||||
"swf": "application/x-shockwave-flash",
|
||||
"t": "application/x-troff",
|
||||
"tar": "application/x-tar",
|
||||
"tcl": "application/x-tcl",
|
||||
"tex": "application/x-tex",
|
||||
"texi": "application/x-texinfo",
|
||||
"texinfo": "application/x-texinfo",
|
||||
"tif": "image/tiff",
|
||||
"tiff": "image/tiff",
|
||||
"tr": "application/x-troff",
|
||||
"tsv": "text/tab-separated-values",
|
||||
"txt": "text/plain",
|
||||
"ustar": "application/x-ustar",
|
||||
"vcd": "application/x-cdlink",
|
||||
"vrml": "model/vrml",
|
||||
"wav": "audio/x-wav",
|
||||
"wbmp": "image/vnd.wap.wbmp",
|
||||
"wbxml": "application/vnd.wap.wbxml",
|
||||
"wml": "text/vnd.wap.wml",
|
||||
"wmlc": "application/vnd.wap.wmlc",
|
||||
"wmls": "text/vnd.wap.wmlscript",
|
||||
"wmlsc": "application/vnd.wap.wmlscriptc",
|
||||
"wrl": "model/vrml",
|
||||
"xbm": "image/x-xbitmap",
|
||||
"xht": "application/xhtml+xml",
|
||||
"xhtml": "application/xhtml+xml",
|
||||
"xls": "application/vnd.ms-excel",
|
||||
"xml": "text/xml",
|
||||
"xpm": "image/x-xpixmap",
|
||||
"xsl": "text/xml",
|
||||
"xwd": "image/x-xwindowdump",
|
||||
"xyz": "chemical/x-xyz",
|
||||
"zip": "application/zip",
|
||||
"": "application/octet-stream"
|
||||
}
|
||||
@@ -64,4 +64,16 @@ $config['init_atomics'] = [
|
||||
/** 自动保存的缓存保存时间(秒) */
|
||||
$config['auto_save_interval'] = 900;
|
||||
|
||||
return $config;
|
||||
/** 上下文接口类 implemented from ContextInterface */
|
||||
$config['context_class'] = \ZM\Context\Context::class;
|
||||
|
||||
/** 静态文件访问 */
|
||||
$config['static_file_server'] = [
|
||||
'status' => false,
|
||||
'document_root' => WORKING_DIR . '/resources/html',
|
||||
'document_index' => [
|
||||
'index.html'
|
||||
]
|
||||
];
|
||||
|
||||
return $config;
|
||||
|
||||
10
resources/html/static.html
Normal file
10
resources/html/static.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>This is example</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello zhamao!</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
namespace Framework;
|
||||
|
||||
use Swoole\Runtime;
|
||||
use ZM\Event\EventHandler;
|
||||
use Exception;
|
||||
use Swoole\WebSocket\Server;
|
||||
@@ -38,6 +39,7 @@ class FrameworkLoader
|
||||
|
||||
chdir(__DIR__ . '/../..');
|
||||
define('WORKING_DIR', getcwd());
|
||||
Runtime::enableCoroutine();
|
||||
$this->requireGlobalFunctions();
|
||||
$this->registerAutoloader('classLoader');
|
||||
self::$settings = new GlobalConfig();
|
||||
@@ -108,4 +110,4 @@ class FrameworkLoader
|
||||
self::$run_time = microtime(true);
|
||||
EventHandler::callSwooleEvent("WorkerStart", $server, $worker_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,7 @@ class GlobalConfig
|
||||
public $success = false;
|
||||
|
||||
public function __construct() {
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
include_once WORKING_DIR.'/config/global.php';
|
||||
include_once WORKING_DIR . '/config/global.php';
|
||||
global $config;
|
||||
$this->success = true;
|
||||
$this->config = $config;
|
||||
@@ -30,4 +29,8 @@ class GlobalConfig
|
||||
if ($r === null) return null;
|
||||
return $r;
|
||||
}
|
||||
}
|
||||
|
||||
public function getAll() {
|
||||
return $this->config;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ class ZMBuf
|
||||
}
|
||||
|
||||
static function config($config_name) {
|
||||
return self::$config ?? null;
|
||||
return self::$config[$config_name] ?? null;
|
||||
}
|
||||
|
||||
public static function resetCache() {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Framework\Console;
|
||||
use Framework\ZMBuf;
|
||||
use ZM\Utils\Context;
|
||||
use ZM\Context\ContextInterface;
|
||||
|
||||
function classLoader($p) {
|
||||
$filepath = getClassPath($p);
|
||||
@@ -11,7 +12,7 @@ function classLoader($p) {
|
||||
try {
|
||||
require_once $filepath;
|
||||
} catch (Exception $e) {
|
||||
echo "Error when finding class: ".$p.PHP_EOL;
|
||||
echo "Error when finding class: " . $p . PHP_EOL;
|
||||
die;
|
||||
}
|
||||
}
|
||||
@@ -148,18 +149,33 @@ function matchArgs($pattern, $context) {
|
||||
} else return false;
|
||||
}
|
||||
|
||||
function set_coroutine_params($array){
|
||||
function set_coroutine_params($array) {
|
||||
$cid = Co::getCid();
|
||||
if($cid == -1) die("Cannot set coroutine params at none coroutine mode.");
|
||||
if ($cid == -1) die("Cannot set coroutine params at none coroutine mode.");
|
||||
ZMBuf::$context[$cid] = $array;
|
||||
foreach(ZMBuf::$context as $c => $v) {
|
||||
if(!Co::exists($c)) unset(ZMBuf::$context[$c]);
|
||||
foreach (ZMBuf::$context as $c => $v) {
|
||||
if (!Co::exists($c)) unset(ZMBuf::$context[$c]);
|
||||
}
|
||||
}
|
||||
|
||||
function context(){
|
||||
/**
|
||||
* @return ContextInterface|null
|
||||
*/
|
||||
function context() {
|
||||
$cid = Co::getCid();
|
||||
if(isset(ZMBuf::$context[$cid])) {
|
||||
return new Context(ZMBuf::$context[$cid], $cid);
|
||||
} else return null;
|
||||
$c_class = ZMBuf::globals("context_class");
|
||||
if (isset(ZMBuf::$context[$cid])) {
|
||||
return new $c_class(ZMBuf::$context[$cid], $cid);
|
||||
} else {
|
||||
while (($pcid = Co::getPcid($cid)) !== -1) {
|
||||
if (isset(ZMBuf::$context[$cid])) return new $c_class(ZMBuf::$context[$cid], $cid);
|
||||
else $cid = $pcid;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function debug($msg) {
|
||||
if (ZMBuf::$atomics["info_level"]->get() == 1)
|
||||
Console::log(date("[H:i:s ") . "DEBUG] " . $msg, 'gray');
|
||||
}
|
||||
|
||||
@@ -3,29 +3,17 @@
|
||||
|
||||
namespace ZM\Annotation;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Framework\Console;
|
||||
use Framework\ZMBuf;
|
||||
use Doctrine\Common\Annotations\{AnnotationException, AnnotationReader};
|
||||
use Framework\{Console, ZMBuf};
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionMethod;
|
||||
use ZM\Annotation\CQ\{CQAfter, CQBefore, CQCommand, CQMessage, CQMetaEvent, CQNotice, CQRequest};
|
||||
use ZM\Annotation\Http\After;
|
||||
use ZM\Annotation\Http\Before;
|
||||
use ZM\Annotation\Http\Controller;
|
||||
use ZM\Annotation\Http\HandleException;
|
||||
use ZM\Annotation\Http\Middleware;
|
||||
use ZM\Annotation\Http\MiddlewareClass;
|
||||
use ZM\Annotation\Http\RequestMapping;
|
||||
use ZM\Annotation\Http\{After, Before, Controller, HandleException, Middleware, MiddlewareClass, RequestMapping};
|
||||
use ZM\Annotation\Interfaces\CustomAnnotation;
|
||||
use ZM\Annotation\Interfaces\Level;
|
||||
use ZM\Annotation\Module\Closed;
|
||||
use ZM\Annotation\Module\InitBuffer;
|
||||
use ZM\Annotation\Module\SaveBuffer;
|
||||
use ZM\Annotation\Swoole\OnStart;
|
||||
use ZM\Annotation\Swoole\SwooleEventAfter;
|
||||
use ZM\Annotation\Swoole\SwooleEventAt;
|
||||
use ZM\Annotation\Module\{Closed, InitBuffer, SaveBuffer};
|
||||
use ZM\Annotation\Swoole\{OnStart, SwooleEventAfter, SwooleEventAt};
|
||||
use ZM\Annotation\Interfaces\Rule;
|
||||
use ZM\Connection\WSConnection;
|
||||
use ZM\Http\MiddlewareInterface;
|
||||
@@ -52,6 +40,7 @@ class AnnotationParser
|
||||
$class_prefix = '';
|
||||
$methods = $reflection_class->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||
$class_annotations = $reader->getClassAnnotations($reflection_class);
|
||||
$middleware_addon = null;
|
||||
foreach ($class_annotations as $vs) {
|
||||
if ($vs instanceof Closed) {
|
||||
continue 2;
|
||||
@@ -62,7 +51,7 @@ class AnnotationParser
|
||||
} elseif ($vs instanceof InitBuffer) {
|
||||
ZMBuf::set($vs->buf_name, []);
|
||||
} elseif ($vs instanceof MiddlewareClass) {
|
||||
Console::info("正在注册中间件 ".$vs->class);
|
||||
Console::info("正在注册中间件 " . $vs->class);
|
||||
$result = [
|
||||
"class" => "\\" . $reflection_class->getName()
|
||||
];
|
||||
@@ -87,6 +76,8 @@ class AnnotationParser
|
||||
}
|
||||
ZMBuf::$events[MiddlewareClass::class][$result["name"]] = $result;
|
||||
continue 2;
|
||||
} elseif ($vs instanceof Middleware) {
|
||||
$middleware_addon = $vs;
|
||||
}
|
||||
}
|
||||
foreach ($methods as $vs) {
|
||||
@@ -102,14 +93,15 @@ class AnnotationParser
|
||||
elseif ($vss instanceof CQRequest) ZMBuf::$events[CQRequest::class][] = $vss;
|
||||
elseif ($vss instanceof CQMetaEvent) ZMBuf::$events[CQMetaEvent::class][] = $vss;
|
||||
elseif ($vss instanceof CQCommand) ZMBuf::$events[CQCommand::class][] = $vss;
|
||||
elseif ($vss instanceof RequestMapping) self::registerRequestMapping($vss, $vs, $reflection_class, $class_prefix);
|
||||
elseif ($vss instanceof CustomAnnotation) ZMBuf::$events[get_class($vss)][] = $vss;
|
||||
elseif ($vss instanceof RequestMapping) {
|
||||
self::registerRequestMapping($vss, $vs, $reflection_class, $class_prefix);
|
||||
if($middleware_addon !== null)
|
||||
ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $middleware_addon->middleware;
|
||||
} elseif ($vss instanceof CustomAnnotation) ZMBuf::$events[get_class($vss)][] = $vss;
|
||||
elseif ($vss instanceof CQBefore) ZMBuf::$events[CQBefore::class][$vss->cq_event][] = $vss;
|
||||
elseif ($vss instanceof CQAfter) ZMBuf::$events[CQAfter::class][$vss->cq_event][] = $vss;
|
||||
elseif ($vss instanceof OnStart) ZMBuf::$events[OnStart::class][] = $vss;
|
||||
elseif ($vss instanceof Middleware) {
|
||||
ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware;
|
||||
}
|
||||
elseif ($vss instanceof Middleware) ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -240,9 +232,9 @@ class AnnotationParser
|
||||
}
|
||||
}
|
||||
if ($prefix_exp == [] && $route_exp == []) {
|
||||
$array[$uid - 1]['method'] = $method->getName();
|
||||
$array[$uid - 1]['class'] = $class->getName();
|
||||
$array[$uid - 1]['request_method'] = $vss->request_method;
|
||||
$array[0]['method'] = $method->getName();
|
||||
$array[0]['class'] = $class->getName();
|
||||
$array[0]['request_method'] = $vss->request_method;
|
||||
ZMBuf::$req_mapping = $array;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use ZM\Annotation\AnnotationBase;
|
||||
* Class Middleware
|
||||
* @package ZM\Annotation\Http
|
||||
* @Annotation
|
||||
* @Target("METHOD")
|
||||
* @Target("ALL")
|
||||
*/
|
||||
class Middleware extends AnnotationBase
|
||||
{
|
||||
@@ -21,4 +21,4 @@ class Middleware extends AnnotationBase
|
||||
* @Required()
|
||||
*/
|
||||
public $middleware;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Utils;
|
||||
namespace ZM\Context;
|
||||
|
||||
|
||||
use Swoole\Http\Request;
|
||||
@@ -9,7 +9,7 @@ use Swoole\WebSocket\Frame;
|
||||
use swoole_server;
|
||||
use ZM\Http\Response;
|
||||
|
||||
class Context
|
||||
class Context implements ContextInterface
|
||||
{
|
||||
private $server = null;
|
||||
private $frame = null;
|
||||
@@ -18,12 +18,12 @@ class Context
|
||||
private $response = null;
|
||||
private $cid;
|
||||
|
||||
public function __construct($param0, $cid) {
|
||||
if (isset($param0["server"])) $this->server = $param0["server"];
|
||||
if (isset($param0["frame"])) $this->frame = $param0["frame"];
|
||||
if (isset($param0["data"])) $this->data = $param0["data"];
|
||||
if (isset($param0["request"])) $this->request = $param0["request"];
|
||||
if (isset($param0["response"])) $this->response = $param0["response"];
|
||||
public function __construct($param, $cid) {
|
||||
if (isset($param["server"])) $this->server = $param["server"];
|
||||
if (isset($param["frame"])) $this->frame = $param["frame"];
|
||||
if (isset($param["data"])) $this->data = $param["data"];
|
||||
if (isset($param["request"])) $this->request = $param["request"];
|
||||
if (isset($param["response"])) $this->response = $param["response"];
|
||||
$this->cid = $cid;
|
||||
}
|
||||
|
||||
33
src/ZM/Context/ContextInterface.php
Normal file
33
src/ZM/Context/ContextInterface.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Context;
|
||||
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Swoole\WebSocket\Server;
|
||||
use ZM\Http\Response;
|
||||
|
||||
interface ContextInterface
|
||||
{
|
||||
public function __construct($param, $cid);
|
||||
|
||||
/** @return Server */
|
||||
public function getServer();
|
||||
|
||||
/** @return Frame */
|
||||
public function getFrame();
|
||||
|
||||
/** @return mixed */
|
||||
public function getData();
|
||||
|
||||
/** @return int */
|
||||
public function getCid();
|
||||
|
||||
/** @return Response */
|
||||
public function getResponse();
|
||||
|
||||
/** @return Request */
|
||||
public function getRequest();
|
||||
}
|
||||
@@ -69,7 +69,7 @@ class DB
|
||||
}
|
||||
}
|
||||
|
||||
public static function rawQuery(string $line, $params) {
|
||||
public static function rawQuery(string $line, $params = []) {
|
||||
if (ZMBuf::get("sql_log") === true) {
|
||||
$starttime = microtime(true);
|
||||
}
|
||||
@@ -117,4 +117,8 @@ class DB
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function isTableExists($table) {
|
||||
return in_array($table, self::$table_list);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,6 @@ class InsertBody
|
||||
}
|
||||
|
||||
public function save() {
|
||||
DB::rawQuery('INSERT INTO ' . $this->table->getTableName() . ' VALUES ('.implode(',', array_fill(0, 5, '?')).')', $this->row);
|
||||
DB::rawQuery('INSERT INTO ' . $this->table->getTableName() . ' VALUES ('.implode(',', array_fill(0, count($this->row), '?')).')', $this->row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,10 +44,12 @@ class SelectBody
|
||||
return $this->fetchAll()[0] ?? null;
|
||||
}
|
||||
|
||||
public function value() {
|
||||
public function value($key = null) {
|
||||
$r = $this->fetchFirst();
|
||||
if ($r === null) return null;
|
||||
return current($r);
|
||||
if ($key === null)
|
||||
return current($r);
|
||||
else return $r[$key] ?? null;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
@@ -85,4 +87,4 @@ class SelectBody
|
||||
}
|
||||
return [$msg, $array ?? []];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,9 @@ class EventHandler
|
||||
} catch (Exception $e) {
|
||||
/** @var Response $param1 */
|
||||
$param1->status(500);
|
||||
Console::info($param0->server["remote_addr"].":".$param0->server["remote_port"].
|
||||
" [".$param1->getStatusCode()."] ".$param0->server["request_uri"]
|
||||
);
|
||||
if (!$param1->isEnd()) $param1->end("Internal server error: " . $e->getMessage());
|
||||
Console::error("Internal server error (500), caused by uncaught exception.");
|
||||
Console::log($e->getTraceAsString(), "gray");
|
||||
|
||||
@@ -39,17 +39,19 @@ class MessageEvent implements SwooleEvent
|
||||
*/
|
||||
public function onActivate() {
|
||||
ZMUtil::checkWait();
|
||||
$conn = ConnectionManager::get($this->frame->fd);
|
||||
set_coroutine_params(["server" => $this->server, "frame" => $this->frame, "connection" => $conn]);
|
||||
try {
|
||||
if (ConnectionManager::get($this->frame->fd)->getType() == "qq") {
|
||||
if ($conn->getType() == "qq") {
|
||||
$data = json_decode($this->frame->data, true);
|
||||
if (isset($data["post_type"])) {
|
||||
set_coroutine_params(["data" => $data, "connection" => $conn]);
|
||||
EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0);
|
||||
} else
|
||||
EventHandler::callCQResponse($data);
|
||||
}
|
||||
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
||||
if (strtolower($v->type) == "message" && $this->parseSwooleRule($v)) {
|
||||
$conn = ConnectionManager::get($this->frame->fd);
|
||||
$c = $v->class;
|
||||
/** @var ModBase $class */
|
||||
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
|
||||
@@ -91,4 +93,4 @@ class MessageEvent implements SwooleEvent
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace ZM\Event\Swoole;
|
||||
use Closure;
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Exception;
|
||||
use Framework\Console;
|
||||
use Framework\ZMBuf;
|
||||
use Swoole\Http\Request;
|
||||
use ZM\Annotation\Http\MiddlewareClass;
|
||||
@@ -63,9 +64,6 @@ class RequestEvent implements SwooleEvent
|
||||
} elseif ($node["son"][0]["name"] == $r) {
|
||||
$node = $node["son"][0];
|
||||
continue;
|
||||
} else {
|
||||
$this->responseStatus(404);
|
||||
return $this;
|
||||
}
|
||||
} elseif ($cnt >= 1) {
|
||||
if (isset($node["param_route"])) {
|
||||
@@ -84,7 +82,40 @@ class RequestEvent implements SwooleEvent
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->responseStatus(404);
|
||||
|
||||
if (ZMBuf::globals("static_file_server")["status"]) {
|
||||
$base_dir = ZMBuf::globals("static_file_server")["document_root"];
|
||||
$base_index = ZMBuf::globals("static_file_server")["document_index"];
|
||||
$uri = $this->request->server["request_uri"];
|
||||
$path = realpath($base_dir . urldecode($uri));
|
||||
if ($path !== false) {
|
||||
if (is_dir($path)) $path = $path . '/';
|
||||
$work = realpath(WORKING_DIR) . '/';
|
||||
if (strpos($path, $work) !== 0) {
|
||||
$this->responseStatus(403);
|
||||
return $this;
|
||||
}
|
||||
if (is_dir($path)) {
|
||||
foreach ($base_index as $vp) {
|
||||
if (is_file($path . $vp)) {
|
||||
Console::info("[200] " . $uri . " (static)");
|
||||
$exp = strtolower(pathinfo($path . $vp)['extension'] ?? "unknown");
|
||||
$this->response->setHeader("Content-Type", ZMBuf::config("file_header")[$exp] ?? "application/octet-stream");
|
||||
$this->response->end(file_get_contents($path . $vp));
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
} elseif (is_file($path)) {
|
||||
Console::info("[200] " . $uri . " (static)");
|
||||
$exp = strtolower(pathinfo($path)['extension'] ?? "unknown");
|
||||
$this->response->setHeader("Content-Type", ZMBuf::config("file_header")[$exp] ?? "application/octet-stream");
|
||||
$this->response->end(file_get_contents($path));
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->response->status(404);
|
||||
$this->response->end(ZMUtil::getHttpCodePage(404));
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -94,7 +125,7 @@ class RequestEvent implements SwooleEvent
|
||||
$c_name = $node["class"];
|
||||
if (isset(ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]])) {
|
||||
$middleware = ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]];
|
||||
if(!isset(ZMBuf::$events[MiddlewareClass::class][$middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware}\"!");
|
||||
if (!isset(ZMBuf::$events[MiddlewareClass::class][$middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware}\"!");
|
||||
$middleware = ZMBuf::$events[MiddlewareClass::class][$middleware];
|
||||
$before = $middleware["class"];
|
||||
$r = new $before();
|
||||
@@ -112,8 +143,8 @@ class RequestEvent implements SwooleEvent
|
||||
if (!$this->response->isEnd()) goto eventCall;
|
||||
} catch (Exception $e) {
|
||||
if (!isset($middleware["exceptions"])) throw $e;
|
||||
foreach($middleware["exceptions"] as $name => $method) {
|
||||
if($e instanceof $name) {
|
||||
foreach ($middleware["exceptions"] as $name => $method) {
|
||||
if ($e instanceof $name) {
|
||||
call_user_func([$r, $method], $e, $this->request, $this->response, $params);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ class WSCloseEvent implements SwooleEvent
|
||||
public function onActivate() {
|
||||
ZMUtil::checkWait();
|
||||
ConnectionManager::close($this->fd);
|
||||
set_coroutine_params(["server" => $this->server, "fd" => $this->fd]);
|
||||
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
||||
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
|
||||
$c = $v->class;
|
||||
@@ -60,4 +61,4 @@ class WSCloseEvent implements SwooleEvent
|
||||
private function parseSwooleRule($v) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ class WSOpenEvent implements SwooleEvent
|
||||
$this->conn = new $type_conn($this->server, $this->request->fd);
|
||||
}
|
||||
ZMBuf::$connect[$this->request->fd] = $this->conn;
|
||||
set_coroutine_params(["server" => $this->server, "request" => $this->request, "connection" => $this->conn]);
|
||||
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
|
||||
if (strtolower($v->type) == "open" && $this->parseSwooleRule($v) === true) {
|
||||
$c = $v->class;
|
||||
@@ -90,4 +91,4 @@ class WSOpenEvent implements SwooleEvent
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace ZM\Event\Swoole;
|
||||
|
||||
use Co;
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Timer;
|
||||
@@ -14,6 +15,7 @@ use ZM\Annotation\AnnotationParser;
|
||||
use ZM\Annotation\Swoole\OnStart;
|
||||
use ZM\Annotation\Swoole\SwooleEventAfter;
|
||||
use ZM\Connection\ConnectionManager;
|
||||
use ZM\Context\ContextInterface;
|
||||
use ZM\DB\DB;
|
||||
use Framework\Console;
|
||||
use Framework\GlobalConfig;
|
||||
@@ -58,11 +60,11 @@ class WorkerStartEvent implements SwooleEvent
|
||||
$name = explode(".", $v);
|
||||
if (($prefix = end($name)) == "json") {
|
||||
ZMBuf::$config[$name[0]] = json_decode(Co::readFile(WORKING_DIR . '/config/' . $v), true);
|
||||
Console::info("已读取配置文件(json):" . $prefix);
|
||||
Console::info("已读取配置文件:" . $v);
|
||||
} elseif ($prefix == "php") {
|
||||
ZMBuf::$config[$name[0]] = include_once WORKING_DIR . '/config/' . $v;
|
||||
if (is_array(ZMBuf::$config[$name[0]]))
|
||||
Console::info("已读取配置文件(php):" . $prefix);
|
||||
Console::info("已读取配置文件:" . $v);
|
||||
}
|
||||
}
|
||||
if (ZMBuf::globals("sql_config")["sql_host"] != "") {
|
||||
@@ -94,6 +96,7 @@ class WorkerStartEvent implements SwooleEvent
|
||||
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
|
||||
call_user_func_array([$class, $v->method], []);
|
||||
}
|
||||
set_coroutine_params(["server" => $this->server, "worker_id" => $this->worker_id]);
|
||||
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
|
||||
/** @var AnnotationBase $v */
|
||||
if (strtolower($v->type) == "workerstart") {
|
||||
@@ -120,6 +123,7 @@ class WorkerStartEvent implements SwooleEvent
|
||||
/**
|
||||
* @throws AnnotationException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function loadAllClass() {
|
||||
//加载phar包
|
||||
@@ -148,6 +152,7 @@ class WorkerStartEvent implements SwooleEvent
|
||||
//加载自定义的全局函数
|
||||
if(file_exists(WORKING_DIR."/src/Custom/global_function.php"))
|
||||
require_once WORKING_DIR."/src/Custom/global_function.php";
|
||||
$this->afterCheck();
|
||||
}
|
||||
|
||||
private function setAutosaveTimer($globals) {
|
||||
@@ -156,4 +161,14 @@ class WorkerStartEvent implements SwooleEvent
|
||||
DataProvider::saveBuffer();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private function afterCheck() {
|
||||
$context_class = ZMBuf::globals("context_class");
|
||||
if(!is_a($context_class, ContextInterface::class, true)) {
|
||||
throw new Exception("Context class must implemented from ContextInterface!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ class Response
|
||||
*/
|
||||
private $response;
|
||||
private $is_end = false;
|
||||
private $status_code;
|
||||
|
||||
public function __construct(\Swoole\Http\Response $response) {
|
||||
$this->response = $response;
|
||||
@@ -90,9 +91,14 @@ class Response
|
||||
* @return mixed
|
||||
*/
|
||||
public function status($http_code, $reason = null) {
|
||||
$this->status_code = $http_code;
|
||||
return $this->response->status($http_code, $reason);
|
||||
}
|
||||
|
||||
public function getStatusCode() {
|
||||
return $this->status_code ?? 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $http_code
|
||||
* @param $reason
|
||||
|
||||
@@ -10,7 +10,6 @@ use Framework\ZMBuf;
|
||||
|
||||
class DataProvider
|
||||
{
|
||||
const HEADER_TYPE = '{"ai":"application/postscript","aif":"audio/x-aiff","aifc":"audio/x-aiff","aiff":"audio/x-aiff","asc":"text/plain","au":"audio/basic","avi":"video/x-msvideo","bcpio":"application/x-bcpio","bin":"application/octet-stream","bmp":"image/bmp","cdf":"application/x-netcdf","class":"application/octet-stream","cpio":"application/x-cpio","cpt":"application/mac-compactpro","csh":"application/x-csh","css":"text/css","dcr":"application/x-director","dir":"application/x-director","djv":"image/vnd.djvu","djvu":"image/vnd.djvu","dll":"application/octet-stream","dms":"application/octet-stream","doc":"application/msword","dvi":"application/x-dvi","dxr":"application/x-director","eps":"application/postscript","etx":"text/x-setext","exe":"application/octet-stream","ez":"application/andrew-inset","gif":"image/gif","gtar":"application/x-gtar","hdf":"application/x-hdf","hqx":"application/mac-binhex40","htm":"text/html","html":"text/html","ice":"x-conference/x-cooltalk","ief":"image/ief","iges":"model/iges","igs":"model/iges","jpe":"image/jpeg","jpeg":"image/jpeg","jpg":"image/jpeg","js":"application/x-javascript","kar":"audio/midi","latex":"application/x-latex","lha":"application/octet-stream","lzh":"application/octet-stream","m3u":"audio/x-mpegurl","man":"application/x-troff-man","me":"application/x-troff-me","mesh":"model/mesh","mid":"audio/midi","midi":"audio/midi","mif":"application/vnd.mif","mov":"video/quicktime","movie":"video/x-sgi-movie","mp2":"audio/mpeg","mp3":"audio/mpeg","mpe":"video/mpeg","mpeg":"video/mpeg","mpg":"video/mpeg","mpga":"audio/mpeg","ms":"application/x-troff-ms","msh":"model/mesh","mxu":"video/vnd.mpegurl","nc":"application/x-netcdf","oda":"application/oda","pbm":"image/x-portable-bitmap","pdb":"chemical/x-pdb","pdf":"application/pdf","pgm":"image/x-portable-graymap","pgn":"application/x-chess-pgn","png":"image/png","pnm":"image/x-portable-anymap","ppm":"image/x-portable-pixmap","ppt":"application/vnd.ms-powerpoint","ps":"application/postscript","qt":"video/quicktime","ra":"audio/x-realaudio","ram":"audio/x-pn-realaudio","ras":"image/x-cmu-raster","rgb":"image/x-rgb","rm":"audio/x-pn-realaudio","roff":"application/x-troff","rpm":"audio/x-pn-realaudio-plugin","rtf":"text/rtf","rtx":"text/richtext","sgm":"text/sgml","sgml":"text/sgml","sh":"application/x-sh","shar":"application/x-shar","silo":"model/mesh","sit":"application/x-stuffit","skd":"application/x-koan","skm":"application/x-koan","skp":"application/x-koan","skt":"application/x-koan","smi":"application/smil","smil":"application/smil","snd":"audio/basic","so":"application/octet-stream","spl":"application/x-futuresplash","src":"application/x-wais-source","sv4cpio":"application/x-sv4cpio","sv4crc":"application/x-sv4crc","swf":"application/x-shockwave-flash","t":"application/x-troff","tar":"application/x-tar","tcl":"application/x-tcl","tex":"application/x-tex","texi":"application/x-texinfo","texinfo":"application/x-texinfo","tif":"image/tiff","tiff":"image/tiff","tr":"application/x-troff","tsv":"text/tab-separated-values","txt":"text/plain","ustar":"application/x-ustar","vcd":"application/x-cdlink","vrml":"model/vrml","wav":"audio/x-wav","wbmp":"image/vnd.wap.wbmp","wbxml":"application/vnd.wap.wbxml","wml":"text/vnd.wap.wml","wmlc":"application/vnd.wap.wmlc","wmls":"text/vnd.wap.wmlscript","wmlsc":"application/vnd.wap.wmlscriptc","wrl":"model/vrml","xbm":"image/x-xbitmap","xht":"application/xhtml+xml","xhtml":"application/xhtml+xml","xls":"application/vnd.ms-excel","xml":"text/xml","xpm":"image/x-xpixmap","xsl":"text/xml","xwd":"image/x-xwindowdump","xyz":"chemical/x-xyz","zip":"application/zip"}';
|
||||
public static $buffer_list = [];
|
||||
|
||||
public static function getResourceFolder() {
|
||||
@@ -52,4 +51,4 @@ class DataProvider
|
||||
public static function getDataFolder() {
|
||||
return ZM_DATA;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user