mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
Merge pull request #225 from zhamao-robot/refactor-config-config
重构配置类配置
This commit is contained in:
commit
5cd28964e6
3
.gitignore
vendored
3
.gitignore
vendored
@ -82,3 +82,6 @@ package-lock.json
|
||||
/.tool-version
|
||||
|
||||
.DS_Store
|
||||
|
||||
### PHP CS Fixer ###
|
||||
.php-cs-fixer.cache
|
||||
|
||||
@ -71,5 +71,6 @@ return (new PhpCsFixer\Config())
|
||||
PhpCsFixer\Finder::create()
|
||||
->in(__DIR__ . '/src')
|
||||
->in(__DIR__ . '/tests')
|
||||
)
|
||||
->setUsingCache(false);
|
||||
->in(__DIR__ . '/config')
|
||||
->in(__DIR__ . '/bin')
|
||||
);
|
||||
|
||||
@ -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
28
config/config.php
Normal 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', // 配置文件所在目录
|
||||
// 可以添加多个配置文件目录
|
||||
],
|
||||
],
|
||||
];
|
||||
@ -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,24 +58,42 @@ class ZMConfig
|
||||
*/
|
||||
private ?ConfigTracer $tracer = null;
|
||||
|
||||
/**
|
||||
* @var LoaderInterface 配置加载器
|
||||
* @phpstan-ignore-next-line We will use this property in the future.
|
||||
*/
|
||||
private LoaderInterface $loader;
|
||||
|
||||
/**
|
||||
* 构造配置实例
|
||||
*
|
||||
* @param array $config_paths 配置文件路径
|
||||
* @param string $environment 环境
|
||||
* @param string $environment 环境
|
||||
*
|
||||
* @throws ConfigException 配置文件加载出错
|
||||
*/
|
||||
public function __construct(array $config_paths = [], string $environment = 'uninitiated')
|
||||
public function __construct(string $environment = 'uninitiated', array $init_config = null)
|
||||
{
|
||||
$this->config_paths = $config_paths ?: [self::DEFAULT_CONFIG_PATH];
|
||||
$conf = $init_config ?: $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 +118,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 +361,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 +412,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';
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,9 +56,9 @@ class ZMConfigTest extends TestCase
|
||||
]);
|
||||
|
||||
try {
|
||||
$config = new ZMConfig([
|
||||
$this->vfs->url(),
|
||||
], 'development');
|
||||
$init_conf = require SOURCE_ROOT_DIR . '/config/config.php';
|
||||
$init_conf['source']['paths'] = [$this->vfs->url()];
|
||||
$config = new ZMConfig('development', $init_conf);
|
||||
} catch (ConfigException $e) {
|
||||
$this->fail($e->getMessage());
|
||||
}
|
||||
|
||||
@ -24,7 +24,9 @@ class ZMRequestTest extends TestCase
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$r = ZMRequest::get('http://ip.zhamao.xin');
|
||||
$this->assertStringContainsString('114', $r);
|
||||
$r = ZMRequest::get('http://httpbin.org/get', [
|
||||
'X-Test' => '123',
|
||||
]);
|
||||
$this->assertStringContainsString('123', $r);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user