Normalization of Context class

This commit is contained in:
whale 2020-04-15 10:55:10 +08:00
parent 676527205f
commit 1d2aaf3c99
8 changed files with 70 additions and 19 deletions

View File

@ -37,8 +37,15 @@ switch ($argv[1] ?? '') {
}
$loader = new FrameworkLoader($argv);
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:
echo "Unknown option \"{$argv[1]}\"!\n";
echo "Unknown option \"{$argv[1]}\"!\n\"--help\" for more information\n";
break;
}

View File

@ -64,4 +64,7 @@ $config['init_atomics'] = [
/** 自动保存的缓存保存时间(秒) */
$config['auto_save_interval'] = 900;
/** 上下文接口类 implemented from ContextInterface */
$config['context_class'] = \ZM\Context\Context::class;
return $config;

View File

@ -18,8 +18,7 @@ class GlobalConfig
public $success = false;
public function __construct() {
/** @noinspection PhpIncludeInspection */
include_once WORKING_DIR.'/config/global.php';
include_once WORKING_DIR . '/config/global.php';
global $config;
$this->success = true;
$this->config = $config;
@ -30,4 +29,8 @@ class GlobalConfig
if ($r === null) return null;
return $r;
}
public function getAll() {
return $this->config;
}
}

View File

@ -2,7 +2,7 @@
use Framework\Console;
use Framework\ZMBuf;
use ZM\Utils\Context;
use ZM\Context\ContextInterface;
function classLoader($p) {
$filepath = getClassPath($p);
@ -158,13 +158,17 @@ function set_coroutine_params($array) {
}
}
/**
* @return ContextInterface|null
*/
function context() {
$cid = Co::getCid();
$c_class = ZMBuf::globals("context_class");
if (isset(ZMBuf::$context[$cid])) {
return new Context(ZMBuf::$context[$cid], $cid);
return new $c_class(ZMBuf::$context[$cid], $cid);
} else {
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;
}
return null;

View File

@ -107,9 +107,7 @@ class AnnotationParser
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 OnStart) ZMBuf::$events[OnStart::class][] = $vss;
elseif ($vss instanceof Middleware) {
ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware;
}
elseif ($vss instanceof Middleware) ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware;
}
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace ZM\Utils;
namespace ZM\Context;
use Swoole\Http\Request;
@ -9,7 +9,7 @@ use Swoole\WebSocket\Frame;
use swoole_server;
use ZM\Http\Response;
class Context
class Context implements ContextInterface
{
private $server = null;
private $frame = null;
@ -18,12 +18,12 @@ class Context
private $response = null;
private $cid;
public function __construct($param0, $cid) {
if (isset($param0["server"])) $this->server = $param0["server"];
if (isset($param0["frame"])) $this->frame = $param0["frame"];
if (isset($param0["data"])) $this->data = $param0["data"];
if (isset($param0["request"])) $this->request = $param0["request"];
if (isset($param0["response"])) $this->response = $param0["response"];
public function __construct($param, $cid) {
if (isset($param["server"])) $this->server = $param["server"];
if (isset($param["frame"])) $this->frame = $param["frame"];
if (isset($param["data"])) $this->data = $param["data"];
if (isset($param["request"])) $this->request = $param["request"];
if (isset($param["response"])) $this->response = $param["response"];
$this->cid = $cid;
}

View 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();
}

View File

@ -6,6 +6,7 @@ namespace ZM\Event\Swoole;
use Co;
use Doctrine\Common\Annotations\AnnotationException;
use Exception;
use ReflectionException;
use Swoole\Coroutine;
use Swoole\Timer;
@ -14,6 +15,7 @@ use ZM\Annotation\AnnotationParser;
use ZM\Annotation\Swoole\OnStart;
use ZM\Annotation\Swoole\SwooleEventAfter;
use ZM\Connection\ConnectionManager;
use ZM\Context\ContextInterface;
use ZM\DB\DB;
use Framework\Console;
use Framework\GlobalConfig;
@ -121,6 +123,7 @@ class WorkerStartEvent implements SwooleEvent
/**
* @throws AnnotationException
* @throws ReflectionException
* @throws Exception
*/
private function loadAllClass() {
//加载phar包
@ -149,6 +152,7 @@ class WorkerStartEvent implements SwooleEvent
//加载自定义的全局函数
if(file_exists(WORKING_DIR."/src/Custom/global_function.php"))
require_once WORKING_DIR."/src/Custom/global_function.php";
$this->afterCheck();
}
private function setAutosaveTimer($globals) {
@ -157,4 +161,14 @@ class WorkerStartEvent implements SwooleEvent
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!");
}
}
}