Compare commits

..

18 Commits
1.5.2 ... 1.5.6

Author SHA1 Message Date
whale
dc7af105f4 deprecate ModBase, it will be removed in v1.6 2020-06-15 22:11:03 +08:00
whale
fd96543268 remove stupid access token 2020-06-15 19:51:32 +08:00
whale
cebc6fb310 update to 1.5.6 version
add CQCommand filter
add ZM_DEFAULT_FETCH_MODE
2020-06-15 19:50:07 +08:00
whale
c654cf40d5 update README.md 2020-06-13 17:45:29 +08:00
whale
68cc536af3 update to 1.5.5 version 2020-06-13 17:39:50 +08:00
whale
42412fee98 update init structure 2020-06-13 17:23:53 +08:00
whale
500ec4707e update Hello default module 2020-06-13 17:08:13 +08:00
whale
b09857e3b8 add init command of start commandline 2020-06-13 16:58:45 +08:00
whale
7d79126c8f fix stupid bug of rule parsing 2020-06-13 16:19:11 +08:00
whale
a542558503 close ws connection after calling event 2020-06-13 16:05:57 +08:00
whale
6697591a22 fix a bug 2020-06-13 14:09:25 +08:00
whale
aad28f1ec4 update to 1.5.4 version 2020-06-13 12:21:27 +08:00
whale
f1949b1bd0 remove stupid debug breakpoint 2020-06-13 11:29:09 +08:00
whale
3b8aac5d8f add CQCommand alias
switch spl_autoload to psr-4 autoload
2020-06-13 11:28:31 +08:00
whale
5fd45c2542 add sql_no_exception config 2020-06-13 10:17:14 +08:00
whale
af89c1b1f6 fix some sql bugs 2020-06-10 14:39:30 +08:00
whale
3287b96f30 update to 1.5.3 version 2020-06-10 13:24:04 +08:00
whale
00a8683658 update to 1.5.3 version
fix linux terminal input bug
2020-06-10 13:23:40 +08:00
20 changed files with 168 additions and 43 deletions

View File

@@ -2,7 +2,7 @@
[![作者QQ](https://img.shields.io/badge/作者QQ-627577391-orange.svg)]()
[![zhamao License](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/zhamao-robot/zhamao-framework/blob/master/LICENSE)
[![版本](https://img.shields.io/badge/version-1.5-green.svg)]()
[![Latest Stable Version](http://img.shields.io/packagist/v/zhamao/framework.svg)](https://packagist.org/packages/zhamao/framework)
[![stupid counter](https://img.shields.io/github/search/zhamao-robot/zhamao-framework/stupid.svg)](https://github.com/zhamao-robot/zhamao-framework/search?q=stupid)
[![TODO counter](https://img.shields.io/github/search/zhamao-robot/zhamao-framework/TODO.svg)](https://github.com/zhamao-robot/zhamao-framework/search?q=TODO)

View File

@@ -40,6 +40,71 @@ switch ($argv[1] ?? '') {
array_shift($argv);
require_once 'systemd';
break;
case 'init':
array_shift($argv);
if (LOAD_MODE != 1) {
echo "initialization must be started with composer-project mode!\n";
exit(1);
}
$cwd = LOAD_MODE_COMPOSER_PATH;
echo "Copying default module file ...";
@mkdir($cwd . "/config");
@mkdir($cwd . "/src");
@mkdir($cwd . "/src/Custom");
@mkdir($cwd . "/src/Module");
@mkdir($cwd . "/src/Module/Example");
@mkdir($cwd . "/src/Module/Middleware");
$ls = [
"/config/global.php",
"/.gitignore",
"/config/file_header.json",
"/config/motd.txt",
"/src/Module/Example/Hello.php",
"/src/Module/Middleware/TimerMiddleware.php",
"/src/Custom/global_function.php"
];
foreach($ls as $v) {
if(!file_exists($cwd.$v)) {
echo "Copying ".$v.PHP_EOL;
copy($cwd."/vendor/zhamao/framework".$v, $cwd.$v);
}
}
$autoload = [
"psr-4" => [
"Module\\" => "src/Module",
"Custom\\" => "src/Custom"
],
"files" => [
"src/Custom/global_function.php"
]
];
$scripts = [
"server" => "vendor/bin/start server",
"server:log-debug" => "vendor/bin/start server --log-debug",
"server:log-verbose" => "vendor/bin/start server --log-verbose",
"server:log-info" => "vendor/bin/start server --log-info",
"server:log-warning" => "vendor/bin/start server --log-warning",
"server:debug-mode" => "vendor/bin/start server --debug-mode",
"systemd" => "vendor/bin/start systemd"
];
echo PHP_EOL;
if (file_exists($cwd . "/composer.json")) {
echo "Updating composer.json ...";
$composer = json_decode(file_get_contents($cwd . "/composer.json"), true);
if (!isset($composer["autoload"])) {
$composer["autoload"] = $autoload;
}
if (!isset($composer["scripts"])) {
$composer["scripts"] = $scripts;
}
file_put_contents($cwd . "/composer.json", json_encode($composer, 64 | 128 | 256));
echo PHP_EOL;
} else {
echo("Error occurred. Please check your updates.\n");
exit(1);
}
echo "success!\n";
break;
case '':
case 'framework':
case 'server':
@@ -60,6 +125,7 @@ switch ($argv[1] ?? '') {
echo "\n\n -h, --help\t\tShow this help menu";
echo "\n framework, server\tstart main framework, this is default option";
echo "\n phar-build\t\tbuild a new phar archive";
echo "\n init\t\t\tinitialize framework structure in this directory";
echo "\n systemd\t\tgenerate a new systemd \".service\" file to use\n\n";
break;
default:

View File

@@ -3,7 +3,7 @@
"description": "High performance QQ robot and web server development framework",
"minimum-stability": "stable",
"license": "Apache-2.0",
"version": "1.5.2",
"version": "1.5.6",
"authors": [
{
"name": "whale",
@@ -23,11 +23,19 @@
"swoole/ide-helper": "@dev",
"ext-mbstring": "*",
"swlib/saber": "^1.0",
"doctrine/annotations": "<1.10.2",
"doctrine/annotations": "~1.10",
"ext-json": "*",
"ext-posix": "*",
"ext-ctype": "*",
"ext-pdo": "*",
"psy/psysh": "@stable"
},
"autoload": {
"psr-4": {
"Custom\\": "src/Custom",
"Framework\\": "src/Framework",
"ZM\\": "src/ZM",
"Module\\": "src/Module"
}
}
}

View File

@@ -38,7 +38,13 @@ $config['sql_config'] = [
'sql_database' => 'db_name',
'sql_password' => '',
'sql_enable_cache' => true,
'sql_reset_cache' => '0300'
'sql_reset_cache' => '0300',
'sql_options' => [
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false
],
'sql_no_exception' => false,
'sql_default_fetch_mode' => PDO::FETCH_BOTH // added in 1.5.6
];
/** CQHTTP连接约定的token */

View File

@@ -1,7 +1,3 @@
<?php
//这里写你的全局函数
function phptest(){
echo "Nothing.\n";
}

View File

@@ -184,7 +184,7 @@ class Console
$vss->callback = function(?WSConnection $conn) use ($terminal_id){
$req = ctx()->getRequest();
if($conn->getType() != "terminal") return false;
if(($req->header["x-terminal-id"] ?? "") != $terminal_id || ($req->header["x-pid"] ?? "") != posix_getpid()) {
if(($req->header["x-terminal-id"] ?? "") != $terminal_id) {
$conn->close();
return false;
}

View File

@@ -43,9 +43,19 @@ class FrameworkLoader
$this->requireGlobalFunctions();
if (LOAD_MODE == 0) define("WORKING_DIR", getcwd());
elseif(LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../"));
elseif (LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../"));
elseif (LOAD_MODE == 2) echo "Phar mode: " . WORKING_DIR . PHP_EOL;
$this->registerAutoloader('classLoader');
//$this->registerAutoloader('classLoader');
require_once "DataProvider.php";
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
}
if (LOAD_MODE == 2) {
require_once FRAMEWORK_DIR . "/vendor/autoload.php";
spl_autoload_register('phar_classloader');
}
self::$settings = new GlobalConfig();
if (self::$settings->get("debug_mode") === true) {
$args[] = "--debug-mode";
@@ -130,7 +140,7 @@ class FrameworkLoader
private function defineProperties() {
define("ZM_START_TIME", microtime(true));
define("ZM_DATA", self::$settings->get("zm_data"));
define("ZM_VERSION", json_decode(file_get_contents(__DIR__."/../../composer.json"), true)["version"] ?? "unknown");
define("ZM_VERSION", json_decode(file_get_contents(__DIR__ . "/../../composer.json"), true)["version"] ?? "unknown");
define("CONFIG_DIR", self::$settings->get("config_dir"));
define("CRASH_DIR", self::$settings->get("crash_dir"));
@mkdir(ZM_DATA);
@@ -141,6 +151,7 @@ class FrameworkLoader
define("ZM_MATCH_NUMBER", 2);
define("ZM_MATCH_SECOND", 3);
define("ZM_BREAKPOINT", 'if(in_array("--debug-mode", \Framework\FrameworkLoader::$argv)) extract(\Psy\debug(get_defined_vars(), isset($this) ? $this : @get_called_class()));');
define("ZM_DEFAULT_FETCH_MODE", self::$settings->get("sql_config")["sql_default_fetch_mode"] ?? 4);
}
private function selfCheck() {

View File

@@ -8,11 +8,12 @@ use ZM\Context\ContextInterface;
use ZM\Utils\ZMUtil;
function classLoader($p) {
function phar_classloader($p){
$filepath = getClassPath($p);
if ($filepath === null)
echo "F:Warning: get class path wrongs.$p\n";
//else echo "F:DBG: Found " . $p . "\n";
if($filepath === null) {
Console::debug("F:Warning: get class path wrongs.$p");
return;
}
try {
require_once $filepath;
} catch (Exception $e) {

View File

@@ -28,9 +28,18 @@ class Hello
Console::info("机器人 " . $conn->getQQ() . " 已连接!");
}
/**
* 在机器人连接后向终端输出信息
* @SwooleEventAt("close",rule="connectType:qq")
*/
public function onDisconnect() {
$conn = ctx()->getConnection();
Console::info("机器人 " . $conn->getQQ() . " 已断开连接!");
}
/**
* 向机器人发送"你好",即可回复这句话
* @CQCommand("你好")
* @CQCommand(match="你好",alias={"你好啊","你是谁"})
*/
public function hello() {
return "你好啊,我是由炸毛框架构建的机器人!";
@@ -66,7 +75,6 @@ class Hello
* @Middleware("timer")
*/
public function timer() {
eval(ZM_BREAKPOINT);
return "This page is used as testing TimerMiddleware! Do not use it in production.";
}

View File

@@ -110,7 +110,7 @@ class AnnotationParser
foreach ($method_annotations as $vss) {
if ($vss instanceof Rule) $vss = self::registerRuleEvent($vss, $vs, $reflection_class);
else $vss = self::registerMethod($vss, $vs, $reflection_class);
Console::debug("寻找 ".$vs->getName() ." -> ".get_class($vss));
if ($vss instanceof SwooleEventAt) ZMBuf::$events[SwooleEventAt::class][] = $vss;
elseif ($vss instanceof SwooleEventAfter) ZMBuf::$events[SwooleEventAfter::class][] = $vss;
elseif ($vss instanceof CQMessage) ZMBuf::$events[CQMessage::class][] = $vss;
@@ -326,6 +326,7 @@ class AnnotationParser
$class = getAllClasses(DataProvider::getWorkingDir() . "/src/Custom/Annotation/", "Custom\\Annotation");
foreach ($class as $v) {
$s = DataProvider::getWorkingDir() . '/src/' . str_replace("\\", "/", $v) . ".php";
Console::debug("Requiring custom annotation ".$s);
require_once $s;
}
}

View File

@@ -19,6 +19,16 @@ class CQCommand extends AnnotationBase implements Level
public $match = "";
/** @var string */
public $regexMatch = "";
/** @var string[] */
public $alias = [];
/** @var string */
public $message_type = "";
/** @var int */
public $user_id = 0;
/** @var int */
public $group_id = 0;
/** @var int */
public $discuss_id = 0;
/** @var int */
public $level = 20;
@@ -32,4 +42,4 @@ class CQCommand extends AnnotationBase implements Level
*/
public function setLevel(int $level) { $this->level = $level; }
}
}

View File

@@ -90,10 +90,11 @@ class DB
/**
* @param string $line
* @param array $params
* @param int $fetch_mode
* @return mixed
* @throws DbException
*/
public static function rawQuery(string $line, $params = []) {
public static function rawQuery(string $line, $params = [], $fetch_mode = ZM_DEFAULT_FETCH_MODE) {
if (ZMBuf::get("sql_log") === true) {
$starttime = microtime(true);
}
@@ -131,7 +132,7 @@ class DB
"] " . $line . " " . json_encode($params, JSON_UNESCAPED_UNICODE) . "\n";
Coroutine::writeFile(CRASH_DIR . "sql.log", $log, FILE_APPEND);
}
return $ps->fetchAll();
return $ps->fetchAll($fetch_mode);
}
} catch (DbException $e) {
if (ZMBuf::get("sql_log") === true) {

View File

@@ -41,10 +41,11 @@ class SelectBody
}
/**
* @param int $fetch_mode
* @return null
* @throws DbException
*/
public function fetchAll() {
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
if ($this->table->isCacheEnabled()) {
$rr = md5(implode(",", $this->select_thing) . serialize($this->where_thing));
if (array_key_exists($rr, $this->table->cache)) {
@@ -52,7 +53,7 @@ class SelectBody
return $this->table->cache[$rr]->getResult();
}
}
$this->execute();
$this->execute($fetch_mode);
if ($this->table->isCacheEnabled() && !in_array($rr, $this->table->cache)) {
$this->table->cache[$rr] = $this;
}
@@ -81,11 +82,12 @@ class SelectBody
}
/**
* @param int $fetch_mode
* @throws DbException
*/
public function execute() {
public function execute($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
$str = $this->queryPrepare();
$this->result = DB::rawQuery($str[0], $str[1]);
$this->result = DB::rawQuery($str[0], $str[1], $fetch_mode);
}
public function getResult() { return $this->result; }

View File

@@ -9,7 +9,7 @@ trait WhereBody
protected $where_thing = [];
public function where($column, $operation_or_value, $value = null) {
if (!in_array($operation_or_value, ['=', '!='])) $this->where_thing['='][$column] = $operation_or_value;
if (!in_array($operation_or_value, ['=', '!=', '>', '<', '>=', '<=', 'IN', 'in'])) $this->where_thing['='][$column] = $operation_or_value;
elseif ($value !== null) $this->where_thing[$operation_or_value][$column] = $value;
else $this->where_thing['='][$column] = $operation_or_value;
return $this;

View File

@@ -40,7 +40,7 @@ class MessageEvent
public function onBefore() {
$obj_list = ZMBuf::$events[CQBefore::class]["message"] ?? [];
foreach ($obj_list as $v) {
if($v->level < 200) break;
if ($v->level < 200) break;
EventHandler::callWithMiddleware(
$v->class,
$v->method,
@@ -65,7 +65,7 @@ class MessageEvent
}
}
foreach (ZMBuf::$events[CQBefore::class]["message"] ?? [] as $v) {
if($v->level >= 200) continue;
if ($v->level >= 200) continue;
$c = $v->class;
if (ctx()->getCache("level") != 0) continue;
EventHandler::callWithMiddleware(
@@ -101,7 +101,11 @@ class MessageEvent
foreach (ZMBuf::$events[CQCommand::class] ?? [] as $v) {
/** @var CQCommand $v */
if ($v->match == "" && $v->regexMatch == "") continue;
else {
elseif (($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == context()->getData()["user_id"])) &&
($v->group_id == 0 || ($v->group_id != 0 && $v->group_id == (context()->getData()["group_id"] ?? 0))) &&
($v->discuss_id == 0 || ($v->discuss_id != 0 && $v->discuss_id == (context()->getData()["discuss_id"] ?? 0))) &&
($v->message_type == '' || ($v->message_type != '' && $v->message_type == context()->getData()["message_type"]))
) {
$c = $v->class;
$class_construct = [
"data" => context()->getData(),
@@ -117,6 +121,13 @@ class MessageEvent
return true;
});
return;
} elseif (in_array($word[0], $v->alias)) {
Console::debug("Calling $c -> {$v->method}");
$this->function_call = EventHandler::callWithMiddleware($obj[$c], $v->method, $class_construct, [$word], function ($r) {
if (is_string($r)) context()->reply($r);
return true;
});
return;
} elseif ($v->regexMatch != "" && ($args = matchArgs($v->regexMatch, context()->getMessage())) !== false) {
Console::debug("Calling $c -> {$v->method}");
$this->function_call = EventHandler::callWithMiddleware($obj[$c], $v->method, $class_construct, [$args], function ($r) {

View File

@@ -85,7 +85,7 @@ class EventHandler
" [" . $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::error("Internal server exception (500), caused by ".get_class($e));
Console::log($e->getTraceAsString(), "gray");
} catch (Error $e) {
/** @var Response $param1 */

View File

@@ -42,7 +42,7 @@ class RequestEvent implements SwooleEvent
$this->response->setHeader($k, $v);
}
$uri = $this->request->server["request_uri"];
Console::verbose($this->request->server["remote_addr"]." request ".$uri);
Console::verbose($this->request->server["remote_addr"] . " request " . $uri);
$uri = explode("/", $uri);
$uri = array_diff($uri, ["..", "", "."]);
$node = ZMBuf::$req_mapping;

View File

@@ -4,6 +4,7 @@
namespace ZM\Event\Swoole;
use Closure;
use Doctrine\Common\Annotations\AnnotationException;
use Framework\ZMBuf;
use Swoole\Server;
@@ -11,8 +12,6 @@ use ZM\Annotation\Swoole\SwooleEventAfter;
use ZM\Annotation\Swoole\SwooleEventAt;
use ZM\Connection\ConnectionManager;
use ZM\Event\EventHandler;
use ZM\ModBase;
use ZM\ModHandleType;
use ZM\Utils\ZMUtil;
class WSCloseEvent implements SwooleEvent
@@ -32,8 +31,7 @@ class WSCloseEvent implements SwooleEvent
*/
public function onActivate() {
ZMUtil::checkWait();
ConnectionManager::close($this->fd);
set_coroutine_params(["server" => $this->server, "fd" => $this->fd]);
set_coroutine_params(["server" => $this->server, "fd" => $this->fd, "connection" => ConnectionManager::get($this->fd)]);
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
$c = $v->class;
@@ -41,6 +39,7 @@ class WSCloseEvent implements SwooleEvent
if(context()->getCache("block_continue") === true) break;
}
}
ConnectionManager::close($this->fd);
return $this;
}
@@ -60,6 +59,11 @@ class WSCloseEvent implements SwooleEvent
}
private function parseSwooleRule($v) {
switch (explode(":", $v->rule)[0]) {
case "connectType": //websocket连接类型
if ($v->callback instanceof Closure) return call_user_func($v->callback, ConnectionManager::get($this->fd));
break;
}
return true;
}
}

View File

@@ -102,7 +102,7 @@ class WorkerStartEvent implements SwooleEvent
->withCharset('utf8mb4')
->withUsername($sql["sql_username"])
->withPassword($sql["sql_password"])
->withOptions([PDO::ATTR_STRINGIFY_FETCHES => false])
->withOptions($sql["sql_options"] ?? [PDO::ATTR_STRINGIFY_FETCHES => false])
);
DB::initTableList();
}
@@ -176,13 +176,8 @@ class WorkerStartEvent implements SwooleEvent
}
}
}
//加载composer类
Console::info("加载composer资源中");
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
}
if (LOAD_MODE == 2) require_once FRAMEWORK_DIR . "/vendor/autoload.php";
//remove stupid duplicate code
//加载各个模块的注解类,以及反射
Console::info("检索Module中");

View File

@@ -15,6 +15,11 @@ use ZM\Http\Response;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
/**
* Class ModBase
* @package ZM
* @deprecated
*/
abstract class ModBase
{
/** @var Server */