mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-03 23:05:37 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d4db23d27 | ||
|
|
4496b67dcc | ||
|
|
2a13298384 | ||
|
|
d6ec404d76 | ||
|
|
3235fd4dc1 | ||
|
|
293740fee2 | ||
|
|
d3c420ec84 | ||
|
|
85ef09d43c | ||
|
|
e4561d69c4 | ||
|
|
6b4d206099 | ||
|
|
50843edf6a | ||
|
|
3a1686f8da |
@@ -120,7 +120,7 @@ $config['static_file_server'] = [
|
||||
$config['onebot'] = [
|
||||
'status' => true,
|
||||
'single_bot_mode' => false,
|
||||
'message_level' => 99999
|
||||
'message_level' => 99
|
||||
];
|
||||
|
||||
/** 一个远程简易终端,使用nc直接连接即可,但是不建议开放host为0.0.0.0(远程连接) */
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
$wrapper = \ZM\MySQL\MySQLManager::getWrapper();
|
||||
```
|
||||
|
||||
!!! tip "提示"
|
||||
|
||||
这部分内容部分直接取自 [DBAL - Data Retrieval And Manipulation](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.13/reference/data-retrieval-and-manipulation.html) 原文并直接翻译,如有实际不同请提交 Issue 反馈。
|
||||
|
||||
## 执行预处理 SQL 语句
|
||||
|
||||
预处理查询很巧妙地解决了 SQL 注入问题,并且可以方便地绑定参数进行查询。
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
|
||||
炸毛框架的数据库组件支持原生 SQL、查询构造器,去掉了复杂的对象模型关联,同时默认为数据库连接池,使开发变得简单。
|
||||
|
||||
数据库的配置位于 `config/global.php` 文件的 `mysql_config` 段。
|
||||
数据库的配置位于 `config/global.php` 文件的 `mysql_config` 段,见 [全局配置](../../../../guide/basic-config#mysql_config)。
|
||||
|
||||
如果 `mysql_config.host` 字段为空,则不创建数据库连接池,填写后将创建,且默认保持长连接。
|
||||
@@ -23,7 +23,7 @@
|
||||
| `runtime` | 一些框架运行时调整的设置 | 见子表 `runtime` |
|
||||
| `light_cache` | 轻量内置 key-value 缓存 | 见字表 `light_cache` |
|
||||
| `worker_cache` | 跨进程变量级缓存 | 见子表 `worker_cache` |
|
||||
| `sql_config` | MySQL 数据库连接信息 | 见子表 `sql_config` |
|
||||
| `mysql_config` | MySQL 数据库连接信息 | 见子表 `mysql_config` |
|
||||
| `redis_config` | Redis 连接信息 | 见子表 `redis_config` |
|
||||
| `access_token` | OneBot 客户端连接约定的token,留空则无,相关设置见 [组件 - Access Token 验证](component/access-token) | 空 |
|
||||
| `http_header` | HTTP 请求自定义返回的header | 见配置文件 |
|
||||
@@ -73,17 +73,18 @@
|
||||
| -------- | --------------------------- | ------ |
|
||||
| `worker` | 跨进程缓存的存储工作进程 id | 0 |
|
||||
|
||||
### 子表 **sql_config**
|
||||
### 子表 **mysql_config**
|
||||
|
||||
| 配置名称 | 说明 | 默认值 |
|
||||
| ------------------------ | ------------------------------ | ------------------------------------------------------------ |
|
||||
| `sql_host` | 数据库地址(留空则不使用数据库) | 空 |
|
||||
| `sql_port` | 数据库端口 | 3306 |
|
||||
| `sql_username` | 连接数据库的用户名 | |
|
||||
| `sql_database` | 要连接的数据库名 | |
|
||||
| `sql_password` | 数据库连接密码 | |
|
||||
| `sql_options` | PDO 数据库的 options 参数 | `[PDO::ATTR_STRINGIFY_FETCHES => false,PDO::ATTR_EMULATE_PREPARES => false]` |
|
||||
| `sql_default_fetch_mode` | PDO 的 fetch 模式 | `PDO::FETCH_ASSOC` |
|
||||
| `host` | 数据库地址(留空则不使用数据库) | 空 |
|
||||
| `port` | 数据库端口 | 3306 |
|
||||
| `username` | 连接数据库的用户名 | |
|
||||
| `dbname` | 要连接的数据库名 | |
|
||||
| `password` | 数据库连接密码 | |
|
||||
| `options` | PDO 数据库的 options 参数 | `[PDO::ATTR_STRINGIFY_FETCHES => false,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]` |
|
||||
| `pool_size` | MySQL 连接池大小 | 64 |
|
||||
| `charset` | MySQL 字符集 | `utf8mb4` |
|
||||
|
||||
### 子表 **redis_config**
|
||||
|
||||
|
||||
@@ -4,6 +4,37 @@
|
||||
|
||||
同时此处将只使用 build 版本号进行区分。
|
||||
|
||||
## build 426 (2021-11-10)
|
||||
|
||||
- 修复 CQ 码的解析函数 Bug(#52)
|
||||
|
||||
## build 425 (2021-11-3)
|
||||
|
||||
- 删除未实际应用功能的配置参数
|
||||
- 修复 reload 时会断开 WebSocket 连接且导致进程崩溃的 Bug
|
||||
|
||||
## build 424 (2021-11-2)
|
||||
|
||||
- 新增 InstantModule 类、ZMServer 类、ModuleBase 类
|
||||
- 配置文件新增 `runtime.reload_kill_connect`、`runtime.global_middleware_binding` 选项
|
||||
- 修复部分情况下闭包事件分发时崩溃的 bug
|
||||
- 新增内部方法 `_zm_env_check`
|
||||
- 调整默认的 OneBot 模块对应的等级从 99999 调整为 99
|
||||
- 新增导出框架运行参数的列表功能
|
||||
|
||||
## build 423 (2021-10-17)
|
||||
|
||||
- 修复 PHP 7.2 ~ 7.3 下无法使用新版 MySQL 组件的 bug
|
||||
|
||||
## build 422 (2021-10-6)
|
||||
|
||||
- 修复 `script_` 前缀无法被排除加载模块的 bug
|
||||
- 修复 MySQL 组件的依赖问题
|
||||
|
||||
## build 421 (2021-9-11)
|
||||
|
||||
- 删除多余的调试信息
|
||||
|
||||
## build 420 (2021-9-11)
|
||||
|
||||
- 修复 OneBot 事件无法响应的 bug
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
# 更新日志(v2 版本)
|
||||
|
||||
## v2.5.8 (build 426)
|
||||
|
||||
> 更新时间:2021.11.10
|
||||
|
||||
- 修复 CQ 码的解析函数 Bug(#52)
|
||||
|
||||
## v2.5.7 (build 425)
|
||||
|
||||
> 更新时间:2021.11.3
|
||||
|
||||
- 调低 OneBot 相关事件在 Swoole 的优先级
|
||||
- 修复部分情况下闭包事件函数分发时引发的崩溃 bug
|
||||
- 修复 reload 时会断开 WebSocket 连接且导致进程崩溃的 bug
|
||||
|
||||
## v2.5.6 (build 423)
|
||||
|
||||
> 更新时间:2021.10.17
|
||||
@@ -434,4 +448,4 @@
|
||||
|
||||
> 更新时间:2020.12.23
|
||||
|
||||
已发布正式版。
|
||||
已发布正式版。
|
||||
|
||||
25
instant-test.php
Normal file
25
instant-test.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
require_once "vendor/autoload.php";
|
||||
|
||||
use ZM\Annotation\CQ\CQCommand;
|
||||
use ZM\Annotation\Swoole\OnOpenEvent;
|
||||
use ZM\ConnectionManager\ConnectionObject;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Module\InstantModule;
|
||||
use ZM\ZMServer;
|
||||
|
||||
$weather = new InstantModule("weather");
|
||||
|
||||
|
||||
$weather->onEvent(OnOpenEvent::class, ['connect_type' => 'qq'], function(ConnectionObject $conn) {
|
||||
Console::info("机器人 " . $conn->getOption("connect_id") . " 已连接!");
|
||||
});
|
||||
|
||||
$weather->onEvent(CQCommand::class, ['match' => '你好'], function() {
|
||||
ctx()->reply("hello呀!");
|
||||
});
|
||||
|
||||
$app = new ZMServer("app-name");
|
||||
$app->addModule($weather);
|
||||
$app->run();
|
||||
@@ -10,6 +10,7 @@ use ZM\Annotation\Swoole\OnOpenEvent;
|
||||
use ZM\Annotation\Swoole\OnRequestEvent;
|
||||
use ZM\API\CQ;
|
||||
use ZM\API\TuringAPI;
|
||||
use ZM\API\OneBotV11;
|
||||
use ZM\ConnectionManager\ConnectionObject;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Annotation\CQ\CQCommand;
|
||||
@@ -49,8 +50,12 @@ class Hello
|
||||
* @CQCommand("我是谁")
|
||||
*/
|
||||
public function whoami() {
|
||||
$user = ctx()->getRobot()->getLoginInfo();
|
||||
return "你是" . $user["data"]["nickname"] . ",QQ号是" . $user["data"]["user_id"];
|
||||
$bot = ctx()->getRobot()->getLoginInfo();
|
||||
$bot_id = $bot["data"]["user_id"];
|
||||
$r = OneBotV11::get($bot_id);
|
||||
$QQid = ctx()->getUserId();
|
||||
$nick = $r->getStrangerInfo($QQid)["data"]["nickname"];
|
||||
return "你是" . $nick . ",QQ号是" . $QQid;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -356,7 +356,7 @@ class CQ
|
||||
}
|
||||
$cq["start"] = $offset + $head;
|
||||
$cq["end"] = $offset + $tmpmsg + $head;
|
||||
$offset += $tmpmsg + 1;
|
||||
$offset += $head + $tmpmsg + 1;
|
||||
$cqs[] = (!$is_object ? $cq : CQObject::fromArray($cq));
|
||||
}
|
||||
return $cqs;
|
||||
|
||||
@@ -50,4 +50,10 @@ class RunServerCommand extends Command
|
||||
(new Framework($input->getOptions()))->start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function exportDefinition() {
|
||||
$cmd = new self();
|
||||
$cmd->configure();
|
||||
return $cmd->getDefinition();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,14 @@ use ZM\Command\RunServerCommand;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Exception\InitException;
|
||||
|
||||
class ConsoleApplication extends Application
|
||||
{
|
||||
private static $obj = null;
|
||||
|
||||
const VERSION_ID = 423;
|
||||
const VERSION = "2.5.6";
|
||||
const VERSION_ID = 426;
|
||||
const VERSION = "2.5.8";
|
||||
|
||||
/**
|
||||
* @throws InitException
|
||||
@@ -49,7 +48,8 @@ class ConsoleApplication extends Application
|
||||
*/
|
||||
public function initEnv($with_default_cmd = ""): ConsoleApplication {
|
||||
if (defined("WORKDING_DIR")) throw new InitException();
|
||||
$this->selfCheck();
|
||||
|
||||
_zm_env_check();
|
||||
|
||||
define("WORKING_DIR", getcwd());
|
||||
if (Phar::running() !== "") {
|
||||
@@ -121,17 +121,4 @@ class ConsoleApplication extends Application
|
||||
die(zm_internal_errcode("E00005") . "{$e->getMessage()} at {$e->getFile()}({$e->getLine()})");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动炸毛前要做的环境检查项目
|
||||
*/
|
||||
private function selfCheck(): void {
|
||||
if (!extension_loaded("swoole")) die(zm_internal_errcode("E00001") . "Can not find swoole extension.\n");
|
||||
if (version_compare(SWOOLE_VERSION, "4.5.0") == -1) die(zm_internal_errcode("E00002") . "You must install swoole version >= 4.5.0 !");
|
||||
if (version_compare(PHP_VERSION, "7.2") == -1) die(zm_internal_errcode("E00003") . "PHP >= 7.2 required.");
|
||||
if (version_compare(SWOOLE_VERSION, "4.6.7") < 0 && !extension_loaded("pcntl")) {
|
||||
Console::error(zm_internal_errcode("E00004") . "Swoole 版本必须不低于 4.6.7 或 PHP 安装加载了 pcntl 扩展!");
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ class EventDispatcher
|
||||
try {
|
||||
foreach ((EventManager::$events[$this->class] ?? []) as $v) {
|
||||
$this->dispatchEvent($v, $this->rule, ...$params);
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] 单一对象 " . $v->class . "::" . $v->method . " 分发结束。");
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] 单一对象 " . $v->class . "::" . (is_string($v->method) ? $v->method : "{closure}") . " 分发结束。");
|
||||
if ($this->status == self::STATUS_BEFORE_FAILED || $this->status == self::STATUS_RULE_FAILED) continue;
|
||||
if (is_callable($this->return_func) && $this->status === self::STATUS_NORMAL) {
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] 单一对象 " . $v->class . "::" . $v->method . " 正在执行返回值处理函数 ...");
|
||||
@@ -121,7 +121,7 @@ class EventDispatcher
|
||||
public function dispatchEvent($v, $rule_func = null, ...$params) {
|
||||
$q_c = $v->class;
|
||||
$q_f = $v->method;
|
||||
if ($q_c === "" && ($q_f instanceof Closure)) {
|
||||
if (($q_c ?? "") === "" && ($q_f instanceof Closure)) {
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] 闭包函数的事件触发过程!");
|
||||
if ($rule_func !== null && !$rule_func($v)) {
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] 闭包函数下的 ruleFunc 判断为 false, 拒绝执行此方法。");
|
||||
@@ -158,6 +158,7 @@ class EventDispatcher
|
||||
$r[$k]->class = $q_c;
|
||||
$r[$k]->method = $q_f;
|
||||
$r[$k]->middleware = $middleware;
|
||||
$r[$k]->current_event = $v;
|
||||
if (isset($middleware_obj["before"])) {
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] Middleware 存在前置事件,执行中 ...");
|
||||
$rs = $middleware_obj["before"];
|
||||
|
||||
@@ -23,7 +23,11 @@ class EventManager
|
||||
public static $req_mapping = [];
|
||||
|
||||
public static function addEvent($event_name, ?AnnotationBase $event_obj) {
|
||||
Console::debug("Adding event $event_name at ".$event_obj->class.":".$event_obj->method);
|
||||
if ($event_obj->method instanceof \Closure) {
|
||||
Console::debug("Adding event $event_name at @Anonymous");
|
||||
} else {
|
||||
Console::debug("Adding event $event_name at " . ($event_obj->class) . ":" . ($event_obj->method));
|
||||
}
|
||||
self::$events[$event_name][] = $event_obj;
|
||||
(new AnnotationParser())->sortByLevel(self::$events, $event_name);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace ZM\Event\SwooleEvent;
|
||||
use Swoole\Process;
|
||||
use Swoole\WebSocket\Server;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\ConnectionManager\ManagerGM;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
|
||||
@@ -23,10 +22,6 @@ class OnBeforeReload implements SwooleEvent
|
||||
for ($i = 0; $i < ZM_WORKER_NUM; ++$i) {
|
||||
Process::kill(zm_atomic("_#worker_" . $i)->get(), SIGUSR1);
|
||||
}
|
||||
foreach ($server->connections as $fd) {
|
||||
if (ManagerGM::get($fd) !== null) $server->disconnect($fd);
|
||||
else $server->close($fd);
|
||||
}
|
||||
|
||||
usleep(800 * 1000);
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class OnOpen implements SwooleEvent
|
||||
try {
|
||||
$obb_onebot = ZMConfig::get("global", "onebot") ??
|
||||
ZMConfig::get("global", "modules")["onebot"] ??
|
||||
["status" => true, "single_bot_mode" => false, "message_level" => 99999];
|
||||
["status" => true, "single_bot_mode" => false, "message_level" => 99];
|
||||
$onebot_status = $obb_onebot["status"];
|
||||
if ($conn->getName() === 'qq' && $onebot_status === true) {
|
||||
if ($obb_onebot["single_bot_mode"]) {
|
||||
|
||||
@@ -132,7 +132,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
* @throws Exception
|
||||
*/
|
||||
private function loadAnnotations() {
|
||||
|
||||
if (Framework::$instant_mode) goto skip;
|
||||
//加载各个模块的注解类,以及反射
|
||||
Console::debug("Mapping annotations");
|
||||
$parser = new AnnotationParser();
|
||||
@@ -146,20 +146,6 @@ class OnWorkerStart implements SwooleEvent
|
||||
}
|
||||
}
|
||||
|
||||
//加载自定义的全局函数
|
||||
Console::debug("Loading context class...");
|
||||
$context_class = ZMConfig::get("global", "context_class");
|
||||
if (!is_a($context_class, ContextInterface::class, true)) {
|
||||
throw new ZMKnownException("E00032", "Context class must implemented from ContextInterface!");
|
||||
}
|
||||
|
||||
//加载插件
|
||||
$obb_onebot = ZMConfig::get("global", "onebot") ??
|
||||
ZMConfig::get("global", "modules")["onebot"] ??
|
||||
["status" => true, "single_bot_mode" => false, "message_level" => 99999];
|
||||
|
||||
|
||||
|
||||
// 检查是否允许热加载phar模块,允许的话将遍历phar内的文件
|
||||
$plugin_enable_hotload = ZMConfig::get("global", "module_loader")["enable_hotload"] ?? false;
|
||||
if ($plugin_enable_hotload) {
|
||||
@@ -176,12 +162,23 @@ class OnWorkerStart implements SwooleEvent
|
||||
$parser->registerMods();
|
||||
EventManager::loadEventByParser($parser); //加载事件
|
||||
|
||||
skip:
|
||||
//加载自定义的全局函数
|
||||
Console::debug("Loading context class...");
|
||||
$context_class = ZMConfig::get("global", "context_class");
|
||||
if (!is_a($context_class, ContextInterface::class, true)) {
|
||||
throw new ZMKnownException("E00032", "Context class must implemented from ContextInterface!");
|
||||
}
|
||||
//加载插件
|
||||
$obb_onebot = ZMConfig::get("global", "onebot") ??
|
||||
ZMConfig::get("global", "modules")["onebot"] ??
|
||||
["status" => true, "single_bot_mode" => false, "message_level" => 99999];
|
||||
if ($obb_onebot["status"]) {
|
||||
Console::debug("OneBot support enabled, listening OneBot event(3).");
|
||||
$obj = new OnMessageEvent();
|
||||
$obj->class = QQBot::class;
|
||||
$obj->method = 'handleByEvent';
|
||||
$obj->level = $obb_onebot["message_level"] ?? 99999;
|
||||
$obj->level = $obb_onebot["message_level"] ?? 99;
|
||||
$obj->rule = 'connectIsQQ()';
|
||||
EventManager::addEvent(OnMessageEvent::class, $obj);
|
||||
if ($obb_onebot["single_bot_mode"]) {
|
||||
|
||||
@@ -76,9 +76,11 @@ class Framework
|
||||
*/
|
||||
private $setup_events = [];
|
||||
|
||||
public function __construct($args = []) {
|
||||
$tty_width = $this->getTtyWidth();
|
||||
public static $instant_mode = false;
|
||||
|
||||
public function __construct($args = [], $instant_mode = false) {
|
||||
$tty_width = $this->getTtyWidth();
|
||||
self::$instant_mode = $instant_mode;
|
||||
self::$argv = $args;
|
||||
ZMConfig::setDirectory(DataProvider::getSourceRootDir() . '/config');
|
||||
ZMConfig::setEnv($args["env"] ?? "");
|
||||
@@ -124,7 +126,9 @@ class Framework
|
||||
$this->server_set["log_level"] = SWOOLE_LOG_DEBUG;
|
||||
$add_port = ZMConfig::get("global", "remote_terminal")["status"] ?? false;
|
||||
|
||||
$this->loadServerEvents();
|
||||
if ($instant_mode) {
|
||||
$this->loadServerEvents();
|
||||
}
|
||||
|
||||
$this->parseCliArgs(self::$argv, $add_port);
|
||||
|
||||
@@ -148,7 +152,7 @@ class Framework
|
||||
} else {
|
||||
$out["worker"] = $this->server_set["worker_num"];
|
||||
}
|
||||
$out["environment"] = $args["env"] === null ? "default" : $args["env"];
|
||||
$out["environment"] = ($args["env"] ?? null) === null ? "default" : $args["env"];
|
||||
$out["log_level"] = Console::getLevel();
|
||||
$out["version"] = ZM_VERSION . (LOAD_MODE == 0 ? (" (build " . ZM_VERSION_ID . ")") : "");
|
||||
$out["master_pid"] = posix_getpid();
|
||||
@@ -182,7 +186,7 @@ class Framework
|
||||
}
|
||||
|
||||
self::printProps($out, $tty_width, $args["log-theme"] === null);
|
||||
if ($args["preview"]) {
|
||||
if ($args["preview"] ?? false) {
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
18
src/ZM/Module/InstantModule.php
Normal file
18
src/ZM/Module/InstantModule.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Module;
|
||||
|
||||
/**
|
||||
* @since 2.6
|
||||
*/
|
||||
class InstantModule extends ModuleBase
|
||||
{
|
||||
public function onEvent($event_class, $params, callable $callable) {
|
||||
$class = new $event_class();
|
||||
foreach ($params as $k => $v) {
|
||||
if (is_string($k)) $class->$k = $v;
|
||||
}
|
||||
$class->method = $callable;
|
||||
$this->events[] = $class;
|
||||
}
|
||||
}
|
||||
31
src/ZM/Module/ModuleBase.php
Normal file
31
src/ZM/Module/ModuleBase.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace ZM\Module;
|
||||
|
||||
/**
|
||||
* @since 2.6
|
||||
*/
|
||||
abstract class ModuleBase
|
||||
{
|
||||
protected $module_name;
|
||||
|
||||
protected $events = [];
|
||||
|
||||
public function __construct($module_name) {
|
||||
$this->module_name = $module_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getModuleName() {
|
||||
return $this->module_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getEvents(): array {
|
||||
return $this->events;
|
||||
}
|
||||
}
|
||||
59
src/ZM/ZMServer.php
Normal file
59
src/ZM/ZMServer.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace ZM;
|
||||
|
||||
use ZM\Command\RunServerCommand;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\EventManager;
|
||||
use ZM\Exception\InitException;
|
||||
use ZM\Module\ModuleBase;
|
||||
|
||||
/**
|
||||
* @since 2.6
|
||||
*/
|
||||
class ZMServer
|
||||
{
|
||||
protected $app_name;
|
||||
|
||||
/** @var ModuleBase[] */
|
||||
protected $modules = [];
|
||||
|
||||
public function __construct($app_name) {
|
||||
$this->app_name = $app_name;
|
||||
}
|
||||
|
||||
public function addModule($module_class) {
|
||||
$this->modules[] = $module_class;
|
||||
}
|
||||
|
||||
public function run() {
|
||||
Console::setLevel(4);
|
||||
foreach ($this->modules as $module_class) {
|
||||
foreach ($module_class->getEvents() as $event) {
|
||||
EventManager::addEvent(get_class($event), $event);
|
||||
}
|
||||
}
|
||||
echo "Running...\n";
|
||||
if (defined("WORKDING_DIR")) throw new InitException();
|
||||
|
||||
_zm_env_check();
|
||||
|
||||
define("WORKING_DIR", getcwd());
|
||||
define("SOURCE_ROOT_DIR", WORKING_DIR);
|
||||
define("LOAD_MODE", is_dir(SOURCE_ROOT_DIR . "/src/ZM") ? 0 : 1);
|
||||
define("FRAMEWORK_ROOT_DIR", realpath(__DIR__ . "/../../"));
|
||||
define("ZM_VERSION_ID", ConsoleApplication::VERSION_ID);
|
||||
define("ZM_VERSION", ConsoleApplication::VERSION);
|
||||
$options = array_map(function ($x) {
|
||||
return $x->getDefault();
|
||||
}, RunServerCommand::exportDefinition()->getOptions());
|
||||
(new Framework($options, true))->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAppName() {
|
||||
return $this->app_name;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,19 @@ function getClassPath($class_name) {
|
||||
if (file_exists($dir2)) return $dir2;
|
||||
else return null;
|
||||
}
|
||||
/**
|
||||
* 检查炸毛框架运行的环境
|
||||
* @internal
|
||||
*/
|
||||
function _zm_env_check() {
|
||||
if (!extension_loaded("swoole")) die(zm_internal_errcode("E00001") . "Can not find swoole extension.\n");
|
||||
if (version_compare(SWOOLE_VERSION, "4.5.0") == -1) die(zm_internal_errcode("E00002") . "You must install swoole version >= 4.5.0 !");
|
||||
if (version_compare(PHP_VERSION, "7.2") == -1) die(zm_internal_errcode("E00003") . "PHP >= 7.2 required.");
|
||||
if (version_compare(SWOOLE_VERSION, "4.6.7") < 0 && !extension_loaded("pcntl")) {
|
||||
Console::error(zm_internal_errcode("E00004") . "Swoole 版本必须不低于 4.6.7 或 PHP 安装加载了 pcntl 扩展!");
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用自己定义的万(san)能分割函数
|
||||
@@ -331,19 +344,33 @@ function zm_dump($var, ...$moreVars) {
|
||||
return $var;
|
||||
}
|
||||
|
||||
function zm_info($obj) { Console::info($obj); }
|
||||
function zm_info($obj) {
|
||||
Console::info($obj);
|
||||
}
|
||||
|
||||
function zm_warning($obj) { Console::warning($obj); }
|
||||
function zm_warning($obj) {
|
||||
Console::warning($obj);
|
||||
}
|
||||
|
||||
function zm_success($obj) { Console::success($obj); }
|
||||
function zm_success($obj) {
|
||||
Console::success($obj);
|
||||
}
|
||||
|
||||
function zm_debug($obj) { Console::debug($obj); }
|
||||
function zm_debug($obj) {
|
||||
Console::debug($obj);
|
||||
}
|
||||
|
||||
function zm_verbose($obj) { Console::verbose($obj); }
|
||||
function zm_verbose($obj) {
|
||||
Console::verbose($obj);
|
||||
}
|
||||
|
||||
function zm_error($obj) { Console::error($obj); }
|
||||
function zm_error($obj) {
|
||||
Console::error($obj);
|
||||
}
|
||||
|
||||
function zm_config($name, $key = null) { return ZMConfig::get($name, $key); }
|
||||
function zm_config($name, $key = null) {
|
||||
return ZMConfig::get($name, $key);
|
||||
}
|
||||
|
||||
function quick_reply_closure($reply) {
|
||||
return function () use ($reply) {
|
||||
|
||||
Reference in New Issue
Block a user