From 1d2aaf3c99b0d1c99dfbe2280ed34a0ad54ebdb1 Mon Sep 17 00:00:00 2001 From: whale Date: Wed, 15 Apr 2020 10:55:10 +0800 Subject: [PATCH] Normalization of Context class --- bin/start | 9 ++++++++- config/global.php | 5 ++++- src/Framework/GlobalConfig.php | 9 ++++++--- src/Framework/global_functions.php | 10 +++++++--- src/ZM/Annotation/AnnotationParser.php | 4 +--- src/ZM/{Utils => Context}/Context.php | 16 ++++++++-------- src/ZM/Context/ContextInterface.php | 22 ++++++++++++++++++++++ src/ZM/Event/Swoole/WorkerStartEvent.php | 14 ++++++++++++++ 8 files changed, 70 insertions(+), 19 deletions(-) rename src/ZM/{Utils => Context}/Context.php (67%) create mode 100644 src/ZM/Context/ContextInterface.php diff --git a/bin/start b/bin/start index c9646dab..861866f8 100755 --- a/bin/start +++ b/bin/start @@ -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; } diff --git a/config/global.php b/config/global.php index 80f0ca24..b64ef93d 100644 --- a/config/global.php +++ b/config/global.php @@ -64,4 +64,7 @@ $config['init_atomics'] = [ /** 自动保存的缓存保存时间(秒) */ $config['auto_save_interval'] = 900; -return $config; \ No newline at end of file +/** 上下文接口类 implemented from ContextInterface */ +$config['context_class'] = \ZM\Context\Context::class; + +return $config; diff --git a/src/Framework/GlobalConfig.php b/src/Framework/GlobalConfig.php index c6daaf1b..e4413cac 100644 --- a/src/Framework/GlobalConfig.php +++ b/src/Framework/GlobalConfig.php @@ -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; } -} \ No newline at end of file + + public function getAll() { + return $this->config; + } +} diff --git a/src/Framework/global_functions.php b/src/Framework/global_functions.php index 98c1176c..b2d9e37e 100644 --- a/src/Framework/global_functions.php +++ b/src/Framework/global_functions.php @@ -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; diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index 330a3f11..42c4d1b5 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -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; } } } diff --git a/src/ZM/Utils/Context.php b/src/ZM/Context/Context.php similarity index 67% rename from src/ZM/Utils/Context.php rename to src/ZM/Context/Context.php index f633240f..a376622d 100644 --- a/src/ZM/Utils/Context.php +++ b/src/ZM/Context/Context.php @@ -1,7 +1,7 @@ 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; } diff --git a/src/ZM/Context/ContextInterface.php b/src/ZM/Context/ContextInterface.php new file mode 100644 index 00000000..b35810a3 --- /dev/null +++ b/src/ZM/Context/ContextInterface.php @@ -0,0 +1,22 @@ +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!"); + } + } }