mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 08:35:35 +08:00
Normalization of Context class
This commit is contained in:
@@ -37,8 +37,15 @@ switch ($argv[1] ?? '') {
|
|||||||
}
|
}
|
||||||
$loader = new FrameworkLoader($argv);
|
$loader = new FrameworkLoader($argv);
|
||||||
break;
|
break;
|
||||||
|
case '--help':
|
||||||
|
case '-h':
|
||||||
|
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";
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
echo "Unknown option \"{$argv[1]}\"!\n";
|
echo "Unknown option \"{$argv[1]}\"!\n\"--help\" for more information\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,4 +64,7 @@ $config['init_atomics'] = [
|
|||||||
/** 自动保存的缓存保存时间(秒) */
|
/** 自动保存的缓存保存时间(秒) */
|
||||||
$config['auto_save_interval'] = 900;
|
$config['auto_save_interval'] = 900;
|
||||||
|
|
||||||
|
/** 上下文接口类 implemented from ContextInterface */
|
||||||
|
$config['context_class'] = \ZM\Context\Context::class;
|
||||||
|
|
||||||
return $config;
|
return $config;
|
||||||
@@ -18,8 +18,7 @@ class GlobalConfig
|
|||||||
public $success = false;
|
public $success = false;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
/** @noinspection PhpIncludeInspection */
|
include_once WORKING_DIR . '/config/global.php';
|
||||||
include_once WORKING_DIR.'/config/global.php';
|
|
||||||
global $config;
|
global $config;
|
||||||
$this->success = true;
|
$this->success = true;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
@@ -30,4 +29,8 @@ class GlobalConfig
|
|||||||
if ($r === null) return null;
|
if ($r === null) return null;
|
||||||
return $r;
|
return $r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAll() {
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use Framework\Console;
|
use Framework\Console;
|
||||||
use Framework\ZMBuf;
|
use Framework\ZMBuf;
|
||||||
use ZM\Utils\Context;
|
use ZM\Context\ContextInterface;
|
||||||
|
|
||||||
function classLoader($p) {
|
function classLoader($p) {
|
||||||
$filepath = getClassPath($p);
|
$filepath = getClassPath($p);
|
||||||
@@ -158,13 +158,17 @@ function set_coroutine_params($array) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ContextInterface|null
|
||||||
|
*/
|
||||||
function context() {
|
function context() {
|
||||||
$cid = Co::getCid();
|
$cid = Co::getCid();
|
||||||
|
$c_class = ZMBuf::globals("context_class");
|
||||||
if (isset(ZMBuf::$context[$cid])) {
|
if (isset(ZMBuf::$context[$cid])) {
|
||||||
return new Context(ZMBuf::$context[$cid], $cid);
|
return new $c_class(ZMBuf::$context[$cid], $cid);
|
||||||
} else {
|
} else {
|
||||||
while (($pcid = Co::getPcid($cid)) !== -1) {
|
while (($pcid = Co::getPcid($cid)) !== -1) {
|
||||||
if (isset(ZMBuf::$context[$cid])) return new Context(ZMBuf::$context[$cid], $cid);
|
if (isset(ZMBuf::$context[$cid])) return new $c_class(ZMBuf::$context[$cid], $cid);
|
||||||
else $cid = $pcid;
|
else $cid = $pcid;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -107,9 +107,7 @@ class AnnotationParser
|
|||||||
elseif ($vss instanceof CQBefore) ZMBuf::$events[CQBefore::class][$vss->cq_event][] = $vss;
|
elseif ($vss instanceof CQBefore) ZMBuf::$events[CQBefore::class][$vss->cq_event][] = $vss;
|
||||||
elseif ($vss instanceof CQAfter) ZMBuf::$events[CQAfter::class][$vss->cq_event][] = $vss;
|
elseif ($vss instanceof CQAfter) ZMBuf::$events[CQAfter::class][$vss->cq_event][] = $vss;
|
||||||
elseif ($vss instanceof OnStart) ZMBuf::$events[OnStart::class][] = $vss;
|
elseif ($vss instanceof OnStart) ZMBuf::$events[OnStart::class][] = $vss;
|
||||||
elseif ($vss instanceof Middleware) {
|
elseif ($vss instanceof Middleware) ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware;
|
||||||
ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace ZM\Utils;
|
namespace ZM\Context;
|
||||||
|
|
||||||
|
|
||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
@@ -9,7 +9,7 @@ use Swoole\WebSocket\Frame;
|
|||||||
use swoole_server;
|
use swoole_server;
|
||||||
use ZM\Http\Response;
|
use ZM\Http\Response;
|
||||||
|
|
||||||
class Context
|
class Context implements ContextInterface
|
||||||
{
|
{
|
||||||
private $server = null;
|
private $server = null;
|
||||||
private $frame = null;
|
private $frame = null;
|
||||||
@@ -18,12 +18,12 @@ class Context
|
|||||||
private $response = null;
|
private $response = null;
|
||||||
private $cid;
|
private $cid;
|
||||||
|
|
||||||
public function __construct($param0, $cid) {
|
public function __construct($param, $cid) {
|
||||||
if (isset($param0["server"])) $this->server = $param0["server"];
|
if (isset($param["server"])) $this->server = $param["server"];
|
||||||
if (isset($param0["frame"])) $this->frame = $param0["frame"];
|
if (isset($param["frame"])) $this->frame = $param["frame"];
|
||||||
if (isset($param0["data"])) $this->data = $param0["data"];
|
if (isset($param["data"])) $this->data = $param["data"];
|
||||||
if (isset($param0["request"])) $this->request = $param0["request"];
|
if (isset($param["request"])) $this->request = $param["request"];
|
||||||
if (isset($param0["response"])) $this->response = $param0["response"];
|
if (isset($param["response"])) $this->response = $param["response"];
|
||||||
$this->cid = $cid;
|
$this->cid = $cid;
|
||||||
}
|
}
|
||||||
|
|
||||||
22
src/ZM/Context/ContextInterface.php
Normal file
22
src/ZM/Context/ContextInterface.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace ZM\Context;
|
||||||
|
|
||||||
|
|
||||||
|
interface ContextInterface
|
||||||
|
{
|
||||||
|
public function __construct($param, $cid);
|
||||||
|
|
||||||
|
public function getServer();
|
||||||
|
|
||||||
|
public function getFrame();
|
||||||
|
|
||||||
|
public function getData();
|
||||||
|
|
||||||
|
public function getCid();
|
||||||
|
|
||||||
|
public function getResponse();
|
||||||
|
|
||||||
|
public function getRequest();
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ namespace ZM\Event\Swoole;
|
|||||||
|
|
||||||
use Co;
|
use Co;
|
||||||
use Doctrine\Common\Annotations\AnnotationException;
|
use Doctrine\Common\Annotations\AnnotationException;
|
||||||
|
use Exception;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Swoole\Coroutine;
|
use Swoole\Coroutine;
|
||||||
use Swoole\Timer;
|
use Swoole\Timer;
|
||||||
@@ -14,6 +15,7 @@ use ZM\Annotation\AnnotationParser;
|
|||||||
use ZM\Annotation\Swoole\OnStart;
|
use ZM\Annotation\Swoole\OnStart;
|
||||||
use ZM\Annotation\Swoole\SwooleEventAfter;
|
use ZM\Annotation\Swoole\SwooleEventAfter;
|
||||||
use ZM\Connection\ConnectionManager;
|
use ZM\Connection\ConnectionManager;
|
||||||
|
use ZM\Context\ContextInterface;
|
||||||
use ZM\DB\DB;
|
use ZM\DB\DB;
|
||||||
use Framework\Console;
|
use Framework\Console;
|
||||||
use Framework\GlobalConfig;
|
use Framework\GlobalConfig;
|
||||||
@@ -121,6 +123,7 @@ class WorkerStartEvent implements SwooleEvent
|
|||||||
/**
|
/**
|
||||||
* @throws AnnotationException
|
* @throws AnnotationException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function loadAllClass() {
|
private function loadAllClass() {
|
||||||
//加载phar包
|
//加载phar包
|
||||||
@@ -149,6 +152,7 @@ class WorkerStartEvent implements SwooleEvent
|
|||||||
//加载自定义的全局函数
|
//加载自定义的全局函数
|
||||||
if(file_exists(WORKING_DIR."/src/Custom/global_function.php"))
|
if(file_exists(WORKING_DIR."/src/Custom/global_function.php"))
|
||||||
require_once WORKING_DIR."/src/Custom/global_function.php";
|
require_once WORKING_DIR."/src/Custom/global_function.php";
|
||||||
|
$this->afterCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setAutosaveTimer($globals) {
|
private function setAutosaveTimer($globals) {
|
||||||
@@ -157,4 +161,14 @@ class WorkerStartEvent implements SwooleEvent
|
|||||||
DataProvider::saveBuffer();
|
DataProvider::saveBuffer();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function afterCheck() {
|
||||||
|
$context_class = ZMBuf::globals("context_class");
|
||||||
|
if(!is_a($context_class, ContextInterface::class, true)) {
|
||||||
|
throw new Exception("Context class must implemented from ContextInterface!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user