mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
update to 1.6 version
This commit is contained in:
parent
23b1f797ad
commit
7b7a2d7010
@ -3,7 +3,7 @@
|
||||
"description": "High performance QQ robot and web server development framework",
|
||||
"minimum-stability": "stable",
|
||||
"license": "Apache-2.0",
|
||||
"version": "1.5.8",
|
||||
"version": "1.6",
|
||||
"authors": [
|
||||
{
|
||||
"name": "whale",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -27,7 +27,8 @@ $config['swoole'] = [
|
||||
'log_file' => $config['crash_dir'].'swoole_error.log',
|
||||
'worker_num' => 1,
|
||||
'dispatch_mode' => 2,
|
||||
'task_worker_num' => 0
|
||||
//'task_worker_num' => 1,
|
||||
//'task_enable_coroutine' => true
|
||||
];
|
||||
|
||||
/** MySQL数据库连接信息,host留空则启动时不创建sql连接池 */
|
||||
@ -85,4 +86,9 @@ $config['static_file_server'] = [
|
||||
]
|
||||
];
|
||||
|
||||
/** 注册 Swoole Server 事件注解的类列表 */
|
||||
$config['server_event_handler_class'] = [
|
||||
\Framework\ServerEventHandler::class, //默认不可删除,否则会不能使用框架
|
||||
];
|
||||
|
||||
return $config;
|
||||
|
||||
@ -3,15 +3,13 @@
|
||||
|
||||
namespace Framework;
|
||||
|
||||
use Co;
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Swoole\Http\Request;
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use Swoole\Runtime;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use ZM\Event\EventHandler;
|
||||
use ZM\Annotation\Swoole\OnEvent;
|
||||
use Exception;
|
||||
use Swoole\WebSocket\Server;
|
||||
use ZM\Http\Response;
|
||||
|
||||
/**
|
||||
* Class FrameworkLoader
|
||||
@ -37,10 +35,6 @@ class FrameworkLoader
|
||||
private $server;
|
||||
|
||||
public function __construct($args = []) {
|
||||
if (self::$instance !== null) die("Cannot run two FrameworkLoader in one process!");
|
||||
self::$instance = $this;
|
||||
|
||||
|
||||
$this->requireGlobalFunctions();
|
||||
if (LOAD_MODE == 0) define("WORKING_DIR", getcwd());
|
||||
elseif (LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../"));
|
||||
@ -48,6 +42,7 @@ class FrameworkLoader
|
||||
//$this->registerAutoloader('classLoader');
|
||||
require_once "DataProvider.php";
|
||||
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
|
||||
}
|
||||
if (LOAD_MODE == 2) {
|
||||
@ -81,24 +76,33 @@ class FrameworkLoader
|
||||
self::$argv[] = "--disable-console-input";
|
||||
}
|
||||
$this->server->set($settings);
|
||||
$this->server->on("WorkerStart", [$this, "onWorkerStart"]);
|
||||
$this->server->on("message", function ($server, Frame $frame) {
|
||||
Console::debug("Calling Swoole \"message\" from fd=" . $frame->fd);
|
||||
EventHandler::callSwooleEvent("message", $server, $frame);
|
||||
});
|
||||
$this->server->on("request", function ($request, $response) {
|
||||
$response = new Response($response);
|
||||
Console::debug("Receiving Http request event, cid=" . Co::getCid());
|
||||
EventHandler::callSwooleEvent("request", $request, $response);
|
||||
});
|
||||
$this->server->on("open", function ($server, Request $request) {
|
||||
Console::debug("Calling Swoole \"open\" event from fd=" . $request->fd);
|
||||
EventHandler::callSwooleEvent("open", $server, $request);
|
||||
});
|
||||
$this->server->on("close", function ($server, $fd) {
|
||||
Console::debug("Calling Swoole \"close\" event from fd=" . $fd);
|
||||
EventHandler::callSwooleEvent("close", $server, $fd);
|
||||
$all_event_class = self::$settings->get("server_event_handler_class");
|
||||
$event_list = [];
|
||||
foreach ($all_event_class as $v) {
|
||||
$reader = new AnnotationReader();
|
||||
$reflection_class = new ReflectionClass($v);
|
||||
$methods = $reflection_class->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||
foreach ($methods as $vs) {
|
||||
$method_annotations = $reader->getMethodAnnotations($vs);
|
||||
if ($method_annotations != []) {
|
||||
$annotation = $method_annotations[0];
|
||||
if ($annotation instanceof OnEvent) {
|
||||
$annotation->class = $v;
|
||||
$annotation->method = $vs->getName();
|
||||
$event_list[strtolower($annotation->event)] = $annotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($event_list as $k => $v) {
|
||||
$this->server->on($k, function (...$param) use ($v) {
|
||||
$c = $v->class;
|
||||
//echo $c.PHP_EOL;
|
||||
$c = new $c();
|
||||
call_user_func_array([$c, $v->method], $param);
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
@ -121,6 +125,15 @@ 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初始化出现错误,请检查!");
|
||||
@ -133,10 +146,6 @@ class FrameworkLoader
|
||||
require_once __DIR__ . '/global_functions.php';
|
||||
}
|
||||
|
||||
private function registerAutoloader(string $string) {
|
||||
if (!spl_autoload_register($string)) die("Failed to register autoloader named \"$string\" !");
|
||||
}
|
||||
|
||||
private function defineProperties() {
|
||||
define("ZM_START_TIME", microtime(true));
|
||||
define("ZM_DATA", self::$settings->get("zm_data"));
|
||||
@ -169,16 +178,6 @@ class FrameworkLoader
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Swoole\Server $server
|
||||
* @param $worker_id
|
||||
* @throws AnnotationException
|
||||
*/
|
||||
public function onWorkerStart(\Swoole\Server $server, $worker_id) {
|
||||
self::$instance = $this;
|
||||
self::$run_time = microtime(true);
|
||||
EventHandler::callSwooleEvent("WorkerStart", $server, $worker_id);
|
||||
}
|
||||
}
|
||||
|
||||
global $motd;
|
||||
|
||||
84
src/Framework/ServerEventHandler.php
Normal file
84
src/Framework/ServerEventHandler.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Framework;
|
||||
|
||||
|
||||
use Co;
|
||||
use Doctrine\Common\Annotations\AnnotationException;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Server;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use ZM\Annotation\AnnotationParser;
|
||||
use ZM\Annotation\Swoole\OnEvent;
|
||||
use ZM\Connection\ConnectionManager;
|
||||
use ZM\Event\EventHandler;
|
||||
use ZM\Http\Response;
|
||||
|
||||
class ServerEventHandler
|
||||
{
|
||||
/**
|
||||
* @OnEvent("WorkerStart")
|
||||
* @param Server $server
|
||||
* @param $worker_id
|
||||
* @throws AnnotationException
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function onWorkerStart(Server $server, $worker_id) {
|
||||
if ($server->taskworker === false) {
|
||||
FrameworkLoader::$run_time = microtime(true);
|
||||
EventHandler::callSwooleEvent("WorkerStart", $server, $worker_id);
|
||||
} else {
|
||||
ob_start();
|
||||
AnnotationParser::registerMods();
|
||||
//加载Custom目录下的自定义的内部类
|
||||
ConnectionManager::registerCustomClass();
|
||||
ob_get_clean();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @OnEvent("message")
|
||||
* @param $server
|
||||
* @param Frame $frame
|
||||
* @throws AnnotationException
|
||||
*/
|
||||
public function onMessage($server, Frame $frame) {
|
||||
Console::debug("Calling Swoole \"message\" from fd=" . $frame->fd);
|
||||
EventHandler::callSwooleEvent("message", $server, $frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OnEvent("request")
|
||||
* @param $request
|
||||
* @param $response
|
||||
* @throws AnnotationException
|
||||
*/
|
||||
public function onRequest($request, $response) {
|
||||
$response = new Response($response);
|
||||
Console::debug("Receiving Http request event, cid=" . Co::getCid());
|
||||
EventHandler::callSwooleEvent("request", $request, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OnEvent("open")
|
||||
* @param $server
|
||||
* @param Request $request
|
||||
* @throws AnnotationException
|
||||
*/
|
||||
public function onOpen($server, Request $request) {
|
||||
Console::debug("Calling Swoole \"open\" event from fd=" . $request->fd);
|
||||
EventHandler::callSwooleEvent("open", $server, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OnEvent("close")
|
||||
* @param $server
|
||||
* @param $fd
|
||||
* @throws AnnotationException
|
||||
*/
|
||||
public function onClose($server, $fd) {
|
||||
Console::debug("Calling Swoole \"close\" event from fd=" . $fd);
|
||||
EventHandler::callSwooleEvent("close", $server, $fd);
|
||||
}
|
||||
}
|
||||
@ -52,6 +52,7 @@ class ZMBuf
|
||||
public static $context = [];
|
||||
public static $instance = [];
|
||||
public static $context_class = [];
|
||||
public static $server_events = [];
|
||||
|
||||
static function get($name, $default = null) {
|
||||
return self::$cache[$name] ?? $default;
|
||||
|
||||
24
src/ZM/Annotation/Swoole/OnEvent.php
Normal file
24
src/ZM/Annotation/Swoole/OnEvent.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Annotation\Swoole;
|
||||
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Required;
|
||||
use Doctrine\Common\Annotations\Annotation\Target;
|
||||
use ZM\Annotation\AnnotationBase;
|
||||
|
||||
/**
|
||||
* Class OnEvent
|
||||
* @package ZM\Annotation\Swoole
|
||||
* @Annotation
|
||||
* @Target("METHOD")
|
||||
*/
|
||||
class OnEvent extends AnnotationBase
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @Required()
|
||||
*/
|
||||
public $event;
|
||||
}
|
||||
15
src/ZM/Annotation/Swoole/OnTaskWorkerStart.php
Normal file
15
src/ZM/Annotation/Swoole/OnTaskWorkerStart.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Annotation\Swoole;
|
||||
|
||||
/**
|
||||
* Class OnTaskWorkerStart
|
||||
* @package ZM\Annotation\Swoole
|
||||
* @Annotation
|
||||
* @Target("METHOD")
|
||||
*/
|
||||
class OnTaskWorkerStart
|
||||
{
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user