mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
update to 1.5 version
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
@@ -103,7 +101,7 @@ class FrameworkLoader
|
||||
", 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)
|
||||
"\nworking_dir: " . DataProvider::getWorkingDir()
|
||||
);
|
||||
global $motd;
|
||||
if (!file_exists(DataProvider::getWorkingDir() . "/config/motd.txt")) {
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -67,6 +67,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.";
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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中");
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
namespace ZM\Utils;
|
||||
|
||||
|
||||
use Framework\Console;
|
||||
use Swlib\Saber;
|
||||
use Swoole\Coroutine\Http\Client;
|
||||
|
||||
@@ -11,18 +12,23 @@ 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;
|
||||
}
|
||||
$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 +56,12 @@ 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;
|
||||
}
|
||||
$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 +76,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 +94,4 @@ class ZMRequest
|
||||
public static function session($option) {
|
||||
return Saber::session($option);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
105
src/ZM/Utils/ZMWebSocket.php
Normal file
105
src/ZM/Utils/ZMWebSocket.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?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;
|
||||
}
|
||||
$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){
|
||||
var_dump($frame);
|
||||
});
|
||||
$cli->onClose(function($client){
|
||||
echo "Connection closed.\n";
|
||||
});
|
||||
if($cli->upgrade()){
|
||||
echo "成功连接!\n";
|
||||
} else {
|
||||
echo "连接失败!\n";
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user