Compare commits

...

11 Commits
1.4 ... 1.5.2

Author SHA1 Message Date
whale
c0ea068d04 update to 1.5.2 version
add ZM_VERSION const
2020-06-08 23:52:55 +08:00
whale
9ba58ff90f remove ModBase to default Module 2020-06-05 19:54:20 +08:00
whale
1a1cf0ad30 update to 1.5.1 version 2020-06-05 19:31:43 +08:00
whale
1de93b9dc1 update to 1.5.1 version
fix a warning bug
2020-06-05 19:31:18 +08:00
Whale
99e44eea3d Merge pull request #12 from 854854321/patch-1
Update ZMRequest.php
2020-06-05 19:25:20 +08:00
Whale
e67958a8d1 Update ZMRequest.php 2020-06-05 19:24:56 +08:00
Whale
23b3dc34e2 Merge pull request #11 from 854854321/master
Update README.md
2020-06-05 18:51:52 +08:00
775672d515 Update ZMRequest.php
增加了http请求 传输 数据体
2020-06-05 18:31:55 +08:00
40e17fab62 Update README.md 2020-06-05 16:02:13 +08:00
whale
59fde3d075 update to 1.5 version 2020-06-05 13:36:30 +08:00
whale
a8183757be update to 1.4.1 version
fix uncaught PDOException
2020-05-31 14:22:39 +08:00
19 changed files with 260 additions and 57 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.4-green.svg)]()
[![版本](https://img.shields.io/badge/version-1.5-green.svg)]()
[![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)
@@ -19,8 +19,9 @@ zhamao-framework 是一个基于 酷Q 的 PHP Swoole 的机器人框架,它会
除了起到解析消息的作用,炸毛框架 还提供了完整的 WebSocket + HTTP 服务器,你还能用此框架构建出高性能的 API 接口服务器。
## 开始
1. 你可以使用项目的 `Use this template` 功能将框架克隆到你的公开或私有仓库进行开发
2. 你也可以直接到 **Release** 中下载最新的 phar 包,放入文件夹后快速启动框架
先安装环境,环境安装见下方文档。
1. `composer create-project zhamao/framework-starter` 从模板新建基础文档结构进行使用
2. 你也可以直接到 **Release** 中下载最新的 phar 包,放入文件夹后 `php server.phar` 快速启动框架
3. 还可以使用 Dockerfile 构建 Docker 容器
## 文档
@@ -83,6 +84,6 @@ Pages托管[https://framework.zhamao.xin/](https://framework.zhamao.xin/)
欢迎随时在 HTTP-API 插件群里提问,当然更好的话可以加作者 QQ627577391或提交 Issue 进行疑难解答。
本项目在更内容时,请及时关注 GitHub 动态,更新前请将自己的模块代码做好备份。
本项目在更内容时,请及时关注 GitHub 动态,更新前请将自己的模块代码做好备份。
项目框架采用 Apache-2.0 协议开源,在分发或重写修改等操作时需遵守协议。项目模块部分(`Module` 文件夹) 在非借鉴框架内代码时可不遵守 Apache-2.0 协议进行分发和修改(声明版权)。

File diff suppressed because one or more lines are too long

View File

@@ -10,6 +10,14 @@ require __DIR__ . '/../src/Scheduler/Scheduler.php';
Swoole\Coroutine::set([
'max_coroutine' => 30000,
]);
global $vendor_mode;
$vendor_mode = false;
if (mb_strpos(__DIR__, getcwd()) !== false && substr(str_replace(getcwd(), "", __DIR__), 0, 8) == "/vendor/") {
define("LOAD_MODE", 1); //composer项目模式
define("LOAD_MODE_COMPOSER_PATH", getcwd());
} else {
define("LOAD_MODE", 0); //正常模式
}
date_default_timezone_set("Asia/Shanghai");
@@ -24,13 +32,21 @@ switch ($argv[1] ?? '') {
}
});
break;
case 'phar-build':
array_shift($argv);
require_once 'phar-build';
break;
case 'systemd':
array_shift($argv);
require_once 'systemd';
break;
case '':
case 'framework':
case 'server':
if(!is_dir(__DIR__.'/../vendor/')){
if (!is_dir(__DIR__ . '/../vendor/') && LOAD_MODE == 0) {
echo "Warning: you have not update composer!\n";
exec("composer update", $out, $var);
if($var != 0) {
if ($var != 0) {
echo "You need to run \"composer update\" at root of zhamao-framework!\n";
die;
}
@@ -39,10 +55,12 @@ switch ($argv[1] ?? '') {
break;
case '--help':
case '-h':
echo "\nUsage: ".$argv[0]." [OPTION]\n";
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";
echo "\n framework, server\tstart main framework, this is default option";
echo "\n phar-build\t\tbuild a new phar archive";
echo "\n systemd\t\tgenerate a new systemd \".service\" file to use\n\n";
break;
default:
echo "Unknown option \"{$argv[1]}\"!\n\"--help\" for more information\n";

View File

@@ -2,7 +2,8 @@
<?php /** @since 1.2 */
switch ($argv[1] ?? '') {
case '--generate':
generate();
case '':
generate($argv);
break;
case '--help':
case '-h':
@@ -14,13 +15,17 @@ switch ($argv[1] ?? '') {
break;
}
function generate() {
function generate($argv) {
$s = "[Unit]\nDescription=zhamao-framework Daemon\nAfter=rc-local.service\n\n[Service]\nType=simple";
$s .= "\nUser=" . exec("whoami");
$s .= "\nGroup=" . exec("groups | awk '{print $1}'");
$s .= "\nWorkingDirectory=" . getcwd();
$s .= "\nExecStart=" . getcwd() . "/bin/start server --disable-console-input";
if ($argv[0] == "systemd" && !file_exists(getcwd() . '/systemd'))
$s .= "\nExecStart=" . getcwd() . "/vendor/bin/start server --disable-console-input";
else
$s .= "\nExecStart=" . getcwd() . "/bin/start server --disable-console-input";
$s .= "\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n";
@mkdir(getcwd() . "/resources/");
file_put_contents(getcwd() . "/resources/zhamao.service", $s);
echo "File successfully generated. Path: " . getcwd() . "/resources/zhamao.service\n";
}

View File

@@ -1,3 +0,0 @@
#!/bin/bash
composer update

View File

@@ -1,9 +1,9 @@
{
"name": "zhamao/framework",
"description": "high-performance intelligent assistant",
"description": "High performance QQ robot and web server development framework",
"minimum-stability": "stable",
"license": "Apache-2.0",
"version": "1.4.0",
"version": "1.5.2",
"authors": [
{
"name": "whale",
@@ -14,6 +14,10 @@
"email": "hugo_swift@yahoo.com"
}
],
"prefer-stable": true,
"bin": [
"bin/start"
],
"require": {
"php": ">=7.2",
"swoole/ide-helper": "@dev",
@@ -25,11 +29,5 @@
"ext-ctype": "*",
"ext-pdo": "*",
"psy/psysh": "@stable"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/"
}
}
}

View File

@@ -14,7 +14,7 @@ $config['http_reverse_link'] = "http://127.0.0.1:".$config['port'];
$config['debug_mode'] = false;
/** 存放框架内文件数据的目录 */
$config['zm_data'] = WORKING_DIR.'/zm_data/';
$config['zm_data'] = realpath(__DIR__ . "/../").'/zm_data/';
/** 存放各个模块配置文件的目录 */
$config['config_dir'] = $config['zm_data'].'config/';
@@ -73,7 +73,7 @@ $config['context_class'] = \ZM\Context\Context::class;
/** 静态文件访问 */
$config['static_file_server'] = [
'status' => false,
'document_root' => WORKING_DIR . '/resources/html',
'document_root' => realpath(__DIR__ . "/../") . '/resources/html',
'document_index' => [
'index.html'
]

View File

@@ -30,6 +30,8 @@ Swoole\Coroutine::set([
date_default_timezone_set("Asia/Shanghai");
define('WORKING_DIR', __DIR__);
define('FRAMEWORK_DIR', __DIR__);
define('LOAD_MODE', 2);
$s = new FrameworkLoader($argv);

View File

@@ -15,12 +15,10 @@ class DataProvider
}
public static function getWorkingDir() {
global $is_phar;
if ($is_phar === true) {
return realpath('.');
} else {
return WORKING_DIR;
}
if(LOAD_MODE == 0) return WORKING_DIR;
elseif (LOAD_MODE == 1) return LOAD_MODE_COMPOSER_PATH;
elseif (LOAD_MODE == 2) return realpath('.');
return null;
}
public static function getDataConfig() {

View File

@@ -42,11 +42,9 @@ class FrameworkLoader
$this->requireGlobalFunctions();
if (!isPharMode()) {
define('WORKING_DIR', getcwd());
} else {
echo "Phar mode: " . WORKING_DIR . PHP_EOL;
}
if (LOAD_MODE == 0) define("WORKING_DIR", getcwd());
elseif(LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../"));
elseif (LOAD_MODE == 2) echo "Phar mode: " . WORKING_DIR . PHP_EOL;
$this->registerAutoloader('classLoader');
self::$settings = new GlobalConfig();
if (self::$settings->get("debug_mode") === true) {
@@ -66,7 +64,6 @@ class FrameworkLoader
$this->selfCheck();
try {
$this->server = new Server(self::$settings->get("host"), self::$settings->get("port"));
if (in_array("--remote-shell", $args)) RemoteShell::listen($this->server, "127.0.0.1");
$settings = self::$settings->get("swoole");
if (in_array("--daemon", $args)) {
$settings["daemonize"] = 1;
@@ -93,6 +90,7 @@ class FrameworkLoader
EventHandler::callSwooleEvent("close", $server, $fd);
});
ZMBuf::initAtomic();
if (in_array("--remote-shell", $args)) RemoteShell::listen($this->server, "127.0.0.1");
if (in_array("--log-error", $args)) ZMBuf::$atomics["info_level"]->set(0);
if (in_array("--log-warning", $args)) ZMBuf::$atomics["info_level"]->set(1);
if (in_array("--log-info", $args)) ZMBuf::$atomics["info_level"]->set(2);
@@ -102,8 +100,8 @@ class FrameworkLoader
"host: " . self::$settings->get("host") .
", port: " . self::$settings->get("port") .
", log_level: " . ZMBuf::$atomics["info_level"]->get() .
", version: " . json_decode(file_get_contents(WORKING_DIR . "/composer.json"), true)["version"] .
"\nworking_dir: " . (isPharMode() ? realpath('.') : WORKING_DIR)
", version: " . ZM_VERSION .
"\nworking_dir: " . DataProvider::getWorkingDir()
);
global $motd;
if (!file_exists(DataProvider::getWorkingDir() . "/config/motd.txt")) {
@@ -132,6 +130,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("CONFIG_DIR", self::$settings->get("config_dir"));
define("CRASH_DIR", self::$settings->get("crash_dir"));
@mkdir(ZM_DATA);

View File

@@ -7,9 +7,6 @@ use Swoole\Coroutine\System;
use ZM\Context\ContextInterface;
use ZM\Utils\ZMUtil;
function isPharMode() {
return substr(__DIR__, 0, 7) == 'phar://';
}
function classLoader($p) {
$filepath = getClassPath($p);
@@ -72,6 +69,7 @@ function unicode_decode($str) {
* @return array
*/
function getAllClasses($dir, $indoor_name) {
if(!is_dir($dir)) return [];
$list = scandir($dir);
$classes = [];
unset($list[0], $list[1]);
@@ -224,3 +222,5 @@ function zm_timer_tick($ms, callable $callable) {
}

View File

@@ -10,7 +10,6 @@ use ZM\Annotation\Http\Middleware;
use ZM\Annotation\Http\RequestMapping;
use ZM\Annotation\Swoole\SwooleEventAt;
use ZM\Connection\CQConnection;
use ZM\ModBase;
use ZM\Utils\ZMUtil;
/**
@@ -18,7 +17,7 @@ use ZM\Utils\ZMUtil;
* @package Module\Example
* @since 1.0
*/
class Hello extends ModBase
class Hello
{
/**
* 在机器人连接后向终端输出信息
@@ -67,6 +66,7 @@ class Hello extends ModBase
* @Middleware("timer")
*/
public function timer() {
eval(ZM_BREAKPOINT);
return "This page is used as testing TimerMiddleware! Do not use it in production.";
}

View File

@@ -7,6 +7,7 @@ namespace ZM\DB;
use Exception;
use framework\Console;
use framework\ZMBuf;
use PDOException;
use PDOStatement;
use Swoole\Coroutine;
use Swoole\Database\PDOStatementProxy;
@@ -132,7 +133,7 @@ class DB
}
return $ps->fetchAll();
}
} catch (DBException $e) {
} catch (DbException $e) {
if (ZMBuf::get("sql_log") === true) {
$log =
"[" . date("Y-m-d H:i:s") .
@@ -147,6 +148,21 @@ class DB
}
Console::warning($e->getMessage());
throw $e;
} catch (PDOException $e) {
if (ZMBuf::get("sql_log") === true) {
$log =
"[" . date("Y-m-d H:i:s") .
" " . round(microtime(true) - $starttime, 4) .
"] " . $line . " " . json_encode($params, JSON_UNESCAPED_UNICODE) . " (Error:" . $e->getMessage() . ")\n";
Coroutine::writeFile(CRASH_DIR . "sql.log", $log, FILE_APPEND);
}
if(mb_strpos($e->getMessage(), "has gone away") !== false) {
zm_sleep(0.2);
Console::warning("Gone away of MySQL! retrying!");
return self::rawQuery($line, $params);
}
Console::warning($e->getMessage());
throw new DbException($e->getMessage(), $e->getCode(), $e);
}
}

View File

@@ -30,6 +30,16 @@ class SelectBody
*/
public function get() { return $this->fetchAll(); }
/**
* @throws DbException
*/
public function count() {
$this->select_thing = ["count(*)"];
$str = $this->queryPrepare();
$this->result = DB::rawQuery($str[0], $str[1]);
return intval($this->result[0]["count(*)"]);
}
/**
* @return null
* @throws DbException

View File

@@ -38,7 +38,7 @@ class MessageEvent
* @throws AnnotationException
*/
public function onBefore() {
$obj_list = ZMBuf::$events[CQBefore::class]["message"];
$obj_list = ZMBuf::$events[CQBefore::class]["message"] ?? [];
foreach ($obj_list as $v) {
if($v->level < 200) break;
EventHandler::callWithMiddleware(

View File

@@ -42,6 +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);
$uri = explode("/", $uri);
$uri = array_diff($uri, ["..", "", "."]);
$node = ZMBuf::$req_mapping;

View File

@@ -7,6 +7,7 @@ namespace ZM\Event\Swoole;
use Co;
use Doctrine\Common\Annotations\AnnotationException;
use Exception;
use PDO;
use ReflectionException;
use Swoole\Coroutine;
use Swoole\Database\PDOConfig;
@@ -101,6 +102,7 @@ class WorkerStartEvent implements SwooleEvent
->withCharset('utf8mb4')
->withUsername($sql["sql_username"])
->withPassword($sql["sql_password"])
->withOptions([PDO::ATTR_STRINGIFY_FETCHES => false])
);
DB::initTableList();
}
@@ -180,7 +182,7 @@ class WorkerStartEvent implements SwooleEvent
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
}
if (isPharMode()) require_once WORKING_DIR . "/vendor/autoload.php";
if (LOAD_MODE == 2) require_once FRAMEWORK_DIR . "/vendor/autoload.php";
//加载各个模块的注解类,以及反射
Console::info("检索Module中");

View File

@@ -4,6 +4,7 @@
namespace ZM\Utils;
use Framework\Console;
use Swlib\Saber;
use Swoole\Coroutine\Http\Client;
@@ -11,18 +12,24 @@ class ZMRequest
{
/**
* 使用Swoole协程客户端发起HTTP GET请求
* @version 1.1
* 返回请求后的body
* 如果请求失败或返回状态不是200则返回 false
* @param $url
* @param array $headers
* @param array $set
* @param bool $return_body
* @return bool|string|Client
* @version 1.1
* 返回请求后的body
* 如果请求失败或返回状态不是200则返回 false
*/
public static function get($url, $headers = [], $set = [], $return_body = true) {
$parse = parse_url($url);
$cli = new Client($parse["host"], ($parse["scheme"] == "https" ? 443 : (isset($parse["port"]) ? $parse["port"] : 80)), ($parse["scheme"] == "https" ? true : false));
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)\"");
return false;
}
if(!isset($parse["path"])) $parse["path"] = "/";
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->setHeaders($headers);
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
$cli->get($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
@@ -50,7 +57,13 @@ class ZMRequest
*/
public static function post($url, array $header, $data, $set = [], $return_body = true) {
$parse = parse_url($url);
$cli = new Client($parse["host"], ($parse["scheme"] == "https" ? 443 : (isset($parse["port"]) ? $parse["port"] : 80)), ($parse["scheme"] == "https" ? true : false));
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
return false;
}
if(!isset($parse["path"])) $parse["path"] = "/";
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
$cli->setHeaders($header);
$cli->post($parse["path"] . (isset($parse["query"]) ? ("?" . $parse["query"]) : ""), $data);
@@ -65,6 +78,17 @@ class ZMRequest
}
}
/**
* @param $url
* @param array $set
* @param array $header
* @return ZMWebSocket
* @since 1.5
*/
public static function websocket($url, $set = ['websocket_mask' => true], $header = []) {
return new ZMWebSocket($url, $set, $header);
}
/**
* @param $option
* @return Saber
@@ -72,4 +96,34 @@ class ZMRequest
public static function session($option) {
return Saber::session($option);
}
}
public static function request($url, $attribute = [], $return_body = true) {
$parse = parse_url($url);
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
return false;
}
if(!isset($parse["path"])) $parse["path"] = "/";
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->set($attribute["set"] ?? ["timeout" => 15.0]);
$cli->setMethod($attribute["method"] ?? "GET");
$cli->setHeaders($attribute["headers"] ?? []);
if(isset($attribute["data"])) $cli->setData($attribute["data"]);
if(isset($attribute["file"])) {
foreach($attribute["file"] as $k => $v) {
$cli->addFile($v["path"], $v["name"], $v["mime_type"] ?? null, $v["filename"] ?? null, $v["offset"] ?? 0, $v["length"] ?? 0);
}
}
$cli->execute($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
if ($return_body) {
if ($cli->errCode != 0 || $cli->statusCode != 200) return false;
$a = $cli->body;
$cli->close();
return $a;
} else {
$cli->close();
return $cli;
}
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace ZM\Utils;
use Framework\Console;
use Swoole\Coroutine\Http\Client;
use Swoole\WebSocket\Frame;
/**
* Class ZMWebSocket
* @package ZM\Utils
* @since 1.5
*/
class ZMWebSocket
{
private $parse;
private $client;
public $is_available = false;
private $close_func;
private $message_func;
public function __construct($url, $set = ['websocket_mask' => true], $header = []) {
$this->parse = parse_url($url);
if (!isset($this->parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"ws(s)://\"");
return;
}
if (!isset($this->parse["path"])) $this->parse["path"] = "/";
$port = $this->parse["port"] ?? (($this->parse["scheme"] ?? "ws") == "wss" ? 443 : 80);
$this->client = new Client($this->parse["host"], $port, (($this->parse["scheme"] ?? "ws") == "wss" ? true : false));
$this->client->set($set);
if ($header != []) $this->client->setHeaders($header);
$this->is_available = true;
}
/**
* @return bool
*/
public function upgrade() {
if (!$this->is_available) return false;
$r = $this->client->upgrade($this->parse["path"] . (isset($this->parse["query"]) ? ("?" . $this->parse["query"]) : ""));
if ($r) {
go(function () {
while (true) {
$result = $this->client->recv(60);
if ($result === false) {
if ($this->client->connected === false) {
go(function () {
call_user_func($this->close_func, $this->client);
});
break;
}
} elseif ($result instanceof Frame) {
go(function () use ($result) {
$this->is_available = false;
call_user_func($this->message_func, $result, $this->client);
});
}
}
});
return true;
}
return false;
}
/**
* @param callable $callable
* @return $this
*/
public function onMessage(callable $callable) {
$this->message_func = $callable;
return $this;
}
/**
* @param callable $callable
* @return $this
*/
public function onClose(callable $callable) {
$this->close_func = $callable;
return $this;
}
}
if (!debug_backtrace()) {
go(function () {
require_once __DIR__ . "/../../Framework/Console.php";
$cli = new ZMWebSocket("ws://127.0.0.1:20001/");
if (!$cli->is_available) die("Error!\n");
$cli->onMessage(function (Frame $frame) {
var_dump($frame);
});
$cli->onClose(function () {
echo "Connection closed.\n";
});
if ($cli->upgrade()) {
echo "成功连接!\n";
} else {
echo "连接失败!\n";
}
});
}