Merge pull request #157 from zhamao-robot/fix-config-loading

修正&改进配置加载
This commit is contained in:
sunxyw
2022-08-27 16:25:43 +08:00
committed by GitHub
2 changed files with 33 additions and 8 deletions

View File

@@ -27,6 +27,15 @@ class ZMConfig implements \ArrayAccess
*/ */
public const DEFAULT_CONFIG_PATH = SOURCE_ROOT_DIR . '/config'; public const DEFAULT_CONFIG_PATH = SOURCE_ROOT_DIR . '/config';
/**
* @var string[] 环境别名
*/
public static array $environment_alias = [
'dev' => 'development',
'test' => 'testing',
'prod' => 'production',
];
/** /**
* @var array 已加载的配置文件 * @var array 已加载的配置文件
*/ */
@@ -55,12 +64,14 @@ class ZMConfig implements \ArrayAccess
* *
* @throws ConfigException 配置文件加载出错 * @throws ConfigException 配置文件加载出错
*/ */
public function __construct(array $config_paths = [], string $environment = 'development') public function __construct(array $config_paths = [], string $environment = 'uninitiated')
{ {
$this->config_paths = $config_paths ?: [self::DEFAULT_CONFIG_PATH]; $this->config_paths = $config_paths ?: [self::DEFAULT_CONFIG_PATH];
$this->environment = $environment; $this->environment = self::$environment_alias[$environment] ?? $environment;
$this->holder = new Config([]); $this->holder = new Config([]);
$this->loadFiles(); if ($environment !== 'uninitiated') {
$this->loadFiles();
}
} }
/** /**
@@ -75,6 +86,16 @@ class ZMConfig implements \ArrayAccess
} }
} }
/**
* 获取当前环境
*
* @return string 当前环境
*/
public function getEnvironment(): string
{
return $this->environment;
}
/** /**
* 设置当前环境 * 设置当前环境
* *
@@ -84,8 +105,9 @@ class ZMConfig implements \ArrayAccess
*/ */
public function setEnvironment(string $environment): void public function setEnvironment(string $environment): void
{ {
if ($this->environment !== $environment) { $target = self::$environment_alias[$environment] ?? $environment;
$this->environment = $environment; if ($this->environment !== $target) {
$this->environment = $target;
$this->reload(); $this->reload();
} }
} }
@@ -137,7 +159,6 @@ class ZMConfig implements \ArrayAccess
// 按照加载顺序加载配置文件 // 按照加载顺序加载配置文件
foreach (self::LOAD_ORDER as $load_type) { foreach (self::LOAD_ORDER as $load_type) {
foreach ($stages[$load_type] as $file_path) { foreach ($stages[$load_type] as $file_path) {
logger()->info("加载配置文件:{$file_path}");
$this->loadConfigFromPath($file_path); $this->loadConfigFromPath($file_path);
} }
} }
@@ -205,6 +226,7 @@ class ZMConfig implements \ArrayAccess
public function reload(): void public function reload(): void
{ {
$this->holder = new Config([]); $this->holder = new Config([]);
$this->loaded_files = [];
$this->loadFiles(); $this->loadFiles();
} }
@@ -226,7 +248,7 @@ class ZMConfig implements \ArrayAccess
public function offsetUnset($offset): void public function offsetUnset($offset): void
{ {
$this->set($offset, null); $this->set($offset);
} }
/** /**
@@ -246,6 +268,7 @@ class ZMConfig implements \ArrayAccess
$env = null; $env = null;
} else { } else {
$env = array_pop($parts); $env = array_pop($parts);
$env = self::$environment_alias[$env] ?? $env;
} }
$group = implode('.', $parts); $group = implode('.', $parts);
return [$group, $ext, $load_type, $env]; return [$group, $ext, $load_type, $env];
@@ -365,5 +388,6 @@ class ZMConfig implements \ArrayAccess
// 加入配置 // 加入配置
$this->merge($group, $config); $this->merge($group, $config);
logger()->debug("已载入配置文件:{$path}");
} }
} }

View File

@@ -19,6 +19,7 @@ use OneBot\Driver\Workerman\WorkermanDriver;
use OneBot\Util\Singleton; use OneBot\Util\Singleton;
use Phar; use Phar;
use ZM\Command\Server\ServerStartCommand; use ZM\Command\Server\ServerStartCommand;
use ZM\Config\ZMConfig;
use ZM\Event\EventProvider; use ZM\Event\EventProvider;
use ZM\Event\Listener\HttpEventListener; use ZM\Event\Listener\HttpEventListener;
use ZM\Event\Listener\ManagerEventListener; use ZM\Event\Listener\ManagerEventListener;
@@ -324,7 +325,7 @@ class Framework
// 打印工作目录 // 打印工作目录
$properties['working_dir'] = WORKING_DIR; $properties['working_dir'] = WORKING_DIR;
// 打印环境信息 // 打印环境信息
$properties['environment'] = $this->argv['env']; $properties['environment'] = ZMConfig::getInstance()->getEnvironment();
// 打印驱动 // 打印驱动
$properties['driver'] = config('global.driver'); $properties['driver'] = config('global.driver');
// 打印logger显示等级 // 打印logger显示等级