refactor config config

This commit is contained in:
sunxyw 2022-12-31 20:12:20 +08:00
parent c1a0fae6e6
commit aee7fa332a
No known key found for this signature in database
GPG Key ID: F391C42B19AFFC98
3 changed files with 64 additions and 19 deletions

View File

@ -14,7 +14,7 @@
"enabled": true,
"actions": [
{
"action": "composer cs-fix -- {$STAGED_FILES|of-type:php} --dry-run",
"action": "composer cs-fix -- --config=.php-cs-fixer.php --dry-run --diff {$STAGED_FILES|of-type:php}",
"conditions": [
{
"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",

28
config/config.php Normal file
View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/**
* Config 配置类的配置文件
* 由于 Config 类是第一批被加载的类,因此本文件存在以下限制:
* 1. 只能使用 PHP 格式
* 2. 无法利用容器及依赖注入
* 3. 必须存在于本地,无法使用远程配置(后续版本可能会支持)
*/
return [
'repository' => [
\OneBot\Config\Repository::class, // 配置仓库,须实现 \OneBot\Config\RepositoryInterface 接口
[], // 传入的参数,依序传入构造函数
],
'loader' => [
\OneBot\Config\Loader\DelegateLoader::class, // 配置加载器,须实现 \OneBot\Config\LoaderInterface 接口
[], // 传入的参数,依序传入构造函数
],
'source' => [
'extensions' => ['php', 'yaml', 'yml', 'json', 'toml'], // 配置文件扩展名
'paths' => [
SOURCE_ROOT_DIR . '/config', // 配置文件所在目录
// 可以添加多个配置文件目录
],
],
];

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ZM\Config;
use OneBot\Config\Config;
use OneBot\Config\Loader\LoaderInterface;
use OneBot\Util\Singleton;
use ZM\Exception\ConfigException;
use ZM\Framework;
@ -13,21 +14,11 @@ class ZMConfig
{
use Singleton;
/**
* @var array 支持的文件扩展名
*/
public const ALLOWED_FILE_EXTENSIONS = ['php', 'yaml', 'yml', 'json', 'toml'];
/**
* @var array 配置文件加载顺序,后覆盖前
*/
public const LOAD_ORDER = ['default', 'environment', 'patch'];
/**
* @var string 默认配置文件路径
*/
public const DEFAULT_CONFIG_PATH = SOURCE_ROOT_DIR . '/config';
/**
* @var string[] 环境别名
*/
@ -42,6 +33,11 @@ class ZMConfig
*/
private array $loaded_files = [];
/**
* @var array 配置文件扩展名
*/
private array $file_extensions = [];
/**
* @var array 配置文件路径
*/
@ -62,6 +58,9 @@ class ZMConfig
*/
private ?ConfigTracer $tracer = null;
/** @var LoaderInterface 配置加载器 */
private LoaderInterface $loader;
/**
* 构造配置实例
*
@ -70,16 +69,29 @@ class ZMConfig
*
* @throws ConfigException 配置文件加载出错
*/
public function __construct(array $config_paths = [], string $environment = 'uninitiated')
public function __construct(string $environment = 'uninitiated')
{
$this->config_paths = $config_paths ?: [self::DEFAULT_CONFIG_PATH];
$conf = $this->loadInitConfig();
$this->file_extensions = $conf['source']['extensions'];
$this->config_paths = $conf['source']['paths'];
$this->environment = self::$environment_alias[$environment] ?? $environment;
$this->holder = new Config([]);
// 初始化配置容器
$this->holder = new Config(
new ($conf['repository'][0])(...$conf['repository'][1]),
);
// 初始化配置加载器
$this->loader = new ($conf['loader'][0])(...$conf['loader'][1]);
// 调试模式下启用配置跟踪器
if (Framework::getInstance()->getArgv()['debug'] ?? false) {
$this->tracer = new ConfigTracer();
} else {
$this->tracer = null;
}
if ($environment !== 'uninitiated') {
$this->loadFiles();
}
@ -104,7 +116,7 @@ class ZMConfig
foreach ($files as $file) {
[, $ext, $load_type] = $this->getFileMeta($file);
// 略过不支持的文件
if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
if (!in_array($ext, $this->file_extensions, true)) {
continue;
}
@ -347,12 +359,14 @@ class ZMConfig
// 判断文件格式是否支持
[$group, $ext, $load_type, $env] = $this->getFileMeta($path);
if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
if (!in_array($ext, $this->file_extensions, true)) {
throw ConfigException::unsupportedFileType($path);
}
// 读取并解析配置
$content = file_get_contents($path);
// TODO: 使用 Loader 替代
// $config = $this->loader->load($path);
$config = [];
switch ($ext) {
case 'php':
@ -396,8 +410,11 @@ class ZMConfig
$this->merge($group, $config);
logger()->debug("已载入配置文件:{$path}");
if ($this->tracer !== null) {
$this->tracer->addTracesOf($group, $config, $path);
$this->tracer?->addTracesOf($group, $config, $path);
}
private function loadInitConfig(): array
{
return require SOURCE_ROOT_DIR . '/config/config.php';
}
}