Add other config file loader.

Fix SaveBuffer bug.
This commit is contained in:
whale 2020-03-07 17:13:56 +08:00
parent 69e450703b
commit 4af90e00ff
3 changed files with 55 additions and 15 deletions

View File

@ -34,7 +34,7 @@ class ZMBuf
static $globals = null;
// swoole server操作对象每个进程均分配
/** @var swoole_websocket_server $server */
/** @var \swoole_websocket_server $server */
static $server;
/** @var array Http请求uri路径根节点 */
public static $req_mapping_node;
@ -52,31 +52,56 @@ class ZMBuf
/** @var Atomic[] */
public static $atomics;
public static $req_mapping = [];
public static $config = [];
static function get($name, $default = null) { return self::$cache[$name] ?? $default; }
static function get($name, $default = null) {
return self::$cache[$name] ?? $default;
}
static function set($name, $value) { self::$cache[$name] = $value; }
static function set($name, $value) {
self::$cache[$name] = $value;
}
static function append($name, $value) { self::$cache[$name][] = $value; }
static function append($name, $value) {
self::$cache[$name][] = $value;
}
static function appendKey($name, $key, $value) { self::$cache[$name][$key] = $value; }
static function appendKey($name, $key, $value) {
self::$cache[$name][$key] = $value;
}
static function appendKeyInKey($name, $key, $value) { self::$cache[$name][$key][] = $value; }
static function appendKeyInKey($name, $key, $value) {
self::$cache[$name][$key][] = $value;
}
static function unsetCache($name) { unset(self::$cache[$name]); }
static function unsetCache($name) {
unset(self::$cache[$name]);
}
static function unsetByValue($name, $vale) {
$key = array_search($vale, self::$cache[$name]);
array_splice(self::$cache[$name], $key, 1);
}
static function isset($name) { return isset(self::$cache[$name]); }
static function isset($name) {
return isset(self::$cache[$name]);
}
static function array_key_exists($name, $key) { return isset(self::$cache[$name][$key]); }
static function array_key_exists($name, $key) {
return isset(self::$cache[$name][$key]);
}
static function in_array($name, $val) { return in_array($val, self::$cache[$name]); }
static function in_array($name, $val) {
return in_array($val, self::$cache[$name]);
}
static function globals($key) { return self::$globals->get($key); }
static function globals($key) {
return self::$globals->get($key);
}
static function config($config_name) {
return self::$config ?? null;
}
public static function resetCache() {
self::$cache = [];
@ -88,7 +113,7 @@ class ZMBuf
* 初始化atomic计数器
*/
public static function initAtomic() {
foreach(ZMBuf::globals("init_atomics") as $k => $v) {
foreach (ZMBuf::globals("init_atomics") as $k => $v) {
self::$atomics[$k] = new Atomic($v);
}
}

View File

@ -20,6 +20,6 @@ class SaveBuffer
*@Required()
*/
public $buf_name;
/** @var string|null $sub_folder */
/** @var string $sub_folder */
public $sub_folder = null;
}

View File

@ -4,6 +4,7 @@
namespace ZM\Event\Swoole;
use Co;
use Doctrine\Common\Annotations\AnnotationException;
use ReflectionException;
use Swoole\Coroutine;
@ -48,6 +49,21 @@ class WorkerStartEvent implements SwooleEvent
//设置炸毛buf中储存的对象
ZMBuf::$globals = new GlobalConfig();
ZMBuf::$config = [];
$file = scandir(WORKING_DIR . '/config/');
unset($file[0], $file[1]);
foreach ($file as $k => $v) {
if ($v == "global.php") continue;
$name = explode(".", $v);
if (($prefix = end($name)) == "json") {
ZMBuf::$config[$name[0]] = json_decode(Co::readFile(WORKING_DIR . '/config/' . $v), true);
Console::info("已读取配置文件(json)" . $prefix);
} elseif ($prefix == "php") {
ZMBuf::$config[$name[0]] = include_once WORKING_DIR . '/config/' . $v;
if (is_array(ZMBuf::$config[$name[0]]))
Console::info("已读取配置文件(php)" . $prefix);
}
}
if (ZMBuf::globals("sql_config")["sql_host"] != "") {
Console::info("新建SQL连接池中");
ZMBuf::$sql_pool = new SQLPool();
@ -113,7 +129,6 @@ class WorkerStartEvent implements SwooleEvent
//加载composer类
Console::info("加载composer资源中");
/** @noinspection PhpIncludeInspection */
require_once WORKING_DIR . "/vendor/autoload.php";
//加载各个模块的注解类,以及反射
@ -126,7 +141,7 @@ class WorkerStartEvent implements SwooleEvent
private function setAutosaveTimer($globals) {
DataProvider::$buffer_list = [];
Timer::tick($globals * 1000, function() {
Timer::tick($globals * 1000, function () {
DataProvider::saveBuffer();
});
}