mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-03 23:05:37 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5de283d30c | ||
|
|
7513fd1a1d |
@@ -3,7 +3,7 @@
|
||||
"description": "High performance QQ robot and web server development framework",
|
||||
"minimum-stability": "stable",
|
||||
"license": "Apache-2.0",
|
||||
"version": "1.6",
|
||||
"version": "1.6.2",
|
||||
"authors": [
|
||||
{
|
||||
"name": "whale",
|
||||
|
||||
@@ -8,23 +8,23 @@ $config['host'] = '0.0.0.0';
|
||||
$config['port'] = 20001;
|
||||
|
||||
/** 框架开到公网或外部的HTTP访问链接,通过 DataProvider::getFrameworkLink() 获取 */
|
||||
$config['http_reverse_link'] = "http://127.0.0.1:".$config['port'];
|
||||
$config['http_reverse_link'] = "http://127.0.0.1:" . $config['port'];
|
||||
|
||||
/** 框架是否启动debug模式 */
|
||||
$config['debug_mode'] = false;
|
||||
|
||||
/** 存放框架内文件数据的目录 */
|
||||
$config['zm_data'] = realpath(__DIR__ . "/../").'/zm_data/';
|
||||
$config['zm_data'] = realpath(__DIR__ . "/../") . '/zm_data/';
|
||||
|
||||
/** 存放各个模块配置文件的目录 */
|
||||
$config['config_dir'] = $config['zm_data'].'config/';
|
||||
$config['config_dir'] = $config['zm_data'] . 'config/';
|
||||
|
||||
/** 存放崩溃和运行日志的目录 */
|
||||
$config['crash_dir'] = $config['zm_data'].'crash/';
|
||||
$config['crash_dir'] = $config['zm_data'] . 'crash/';
|
||||
|
||||
/** 对应swoole的server->set参数 */
|
||||
$config['swoole'] = [
|
||||
'log_file' => $config['crash_dir'].'swoole_error.log',
|
||||
'log_file' => $config['crash_dir'] . 'swoole_error.log',
|
||||
'worker_num' => 1,
|
||||
'dispatch_mode' => 2,
|
||||
//'task_worker_num' => 1,
|
||||
@@ -88,7 +88,7 @@ $config['static_file_server'] = [
|
||||
|
||||
/** 注册 Swoole Server 事件注解的类列表 */
|
||||
$config['server_event_handler_class'] = [
|
||||
\Framework\ServerEventHandler::class, //默认不可删除,否则会不能使用框架
|
||||
\Framework\ServerEventHandler::class,
|
||||
];
|
||||
|
||||
return $config;
|
||||
|
||||
@@ -76,7 +76,10 @@ class FrameworkLoader
|
||||
self::$argv[] = "--disable-console-input";
|
||||
}
|
||||
$this->server->set($settings);
|
||||
$all_event_class = self::$settings->get("server_event_handler_class");
|
||||
$all_event_class = self::$settings->get("server_event_handler_class") ?? [];
|
||||
if (!in_array(ServerEventHandler::class, $all_event_class)) {
|
||||
$all_event_class[] = ServerEventHandler::class;
|
||||
}
|
||||
$event_list = [];
|
||||
foreach ($all_event_class as $v) {
|
||||
$reader = new AnnotationReader();
|
||||
@@ -125,15 +128,6 @@ class FrameworkLoader
|
||||
}
|
||||
if (in_array("--debug-mode", self::$argv))
|
||||
Console::warning("You are in debug mode, do not use in production!");
|
||||
register_shutdown_function(function() {
|
||||
$error = error_get_last();
|
||||
if(isset($error["type"]) && $error["type"] == 1) {
|
||||
if(mb_strpos($error["message"], "require") !== false && mb_strpos($error["message"], "callback") !== false) {
|
||||
echo "\e[38;5;203mYou may need to update your \"global.php\" config!\n";
|
||||
echo "Please see: https://github.com/zhamao-robot/zhamao-framework/issues/15\e[m\n";
|
||||
}
|
||||
}
|
||||
});
|
||||
$this->server->start();
|
||||
} catch (Exception $e) {
|
||||
Console::error("Framework初始化出现错误,请检查!");
|
||||
@@ -177,7 +171,6 @@ class FrameworkLoader
|
||||
//if (!file_exists(CRASH_DIR . "last_error.log")) die("Can not find log file.\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
global $motd;
|
||||
|
||||
@@ -22,16 +22,11 @@ class ZMRequest
|
||||
* 如果请求失败或返回状态不是200,则返回 false
|
||||
*/
|
||||
public static function get($url, $headers = [], $set = [], $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->setHeaders($headers);
|
||||
/** @var Client $cli */
|
||||
list($cli, $parse) = self::getNewClient($url);
|
||||
if($cli === null) return false;
|
||||
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
|
||||
$cli->setHeaders($headers);
|
||||
$cli->get($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
|
||||
if ($return_body) {
|
||||
if ($cli->errCode != 0 || $cli->statusCode != 200) return false;
|
||||
@@ -56,14 +51,9 @@ class ZMRequest
|
||||
* @return bool|string|Client
|
||||
*/
|
||||
public static function post($url, array $header, $data, $set = [], $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));
|
||||
/** @var Client $cli */
|
||||
list($cli, $parse) = self::getNewClient($url);
|
||||
if($cli === null) return false;
|
||||
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
|
||||
$cli->setHeaders($header);
|
||||
$cli->post($parse["path"] . (isset($parse["query"]) ? ("?" . $parse["query"]) : ""), $data);
|
||||
@@ -97,15 +87,16 @@ class ZMRequest
|
||||
return Saber::session($option);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param array $attribute
|
||||
* @param bool $return_body
|
||||
* @return bool|string|Client
|
||||
*/
|
||||
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));
|
||||
/** @var Client $cli */
|
||||
list($cli, $parse) = self::getNewClient($url);
|
||||
if($cli === null) return false;
|
||||
$cli->set($attribute["set"] ?? ["timeout" => 15.0]);
|
||||
$cli->setMethod($attribute["method"] ?? "GET");
|
||||
$cli->setHeaders($attribute["headers"] ?? []);
|
||||
@@ -126,4 +117,37 @@ class ZMRequest
|
||||
return $cli;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param null|bool $dst
|
||||
* @return bool
|
||||
*/
|
||||
public static function downloadFile($url, $dst = null) {
|
||||
/** @var Client $cli */
|
||||
list($cli, $parse) = self::getNewClient($url);
|
||||
if($cli === null) return false;
|
||||
$cli->set(["timeout" => 60.0]);
|
||||
$save_path = $dst === null ? "/tmp/_zm_".mt_rand(1000000, 9999999) : $dst;
|
||||
$result = $cli->download($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""), $save_path);
|
||||
if($result === false) return false;
|
||||
elseif ($dst === null) return $save_path;
|
||||
else return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @return bool|array
|
||||
*/
|
||||
private static function getNewClient($url) {
|
||||
$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");
|
||||
return [$cli, $parse];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user