Skip to content

Commit

Permalink
Merge pull request #225 from zhamao-robot/refactor-config-config
Browse files Browse the repository at this point in the history
重构配置类配置
  • Loading branch information
crazywhalecc authored Dec 31, 2022
2 parents d87b7dc + 3f26648 commit 5cd2896
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ package-lock.json
/.tool-version

.DS_Store

### PHP CS Fixer ###
.php-cs-fixer.cache
5 changes: 3 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
)
->setUsingCache(false);
->in(__DIR__ . '/config')
->in(__DIR__ . '/bin')
);
2 changes: 1 addition & 1 deletion captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -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', // 配置文件所在目录
// 可以添加多个配置文件目录
],
],
];
59 changes: 39 additions & 20 deletions src/ZM/Config/ZMConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ZM\Config;

use OneBot\Config\Config;
use OneBot\Config\Loader\LoaderInterface;
use OneBot\Util\Singleton;
use ZM\Exception\ConfigException;
use ZM\Framework;
Expand All @@ -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[] 环境别名
*/
Expand All @@ -42,6 +33,11 @@ class ZMConfig
*/
private array $loaded_files = [];

/**
* @var array 配置文件扩展名
*/
private array $file_extensions = [];

/**
* @var array 配置文件路径
*/
Expand All @@ -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();
}
Expand All @@ -104,7 +118,7 @@ public function loadFiles(): void
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;
}

Expand Down Expand Up @@ -347,12 +361,14 @@ private function loadConfigFromPath(string $path): void

// 判断文件格式是否支持
[$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':
Expand Down Expand Up @@ -396,8 +412,11 @@ private function loadConfigFromPath(string $path): void
$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';
}
}
6 changes: 3 additions & 3 deletions tests/ZM/Config/ZMConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ protected function setUp(): void
]);

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());
}
Expand Down
6 changes: 4 additions & 2 deletions tests/ZM/Utils/ZMRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public function testPost()

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);
}
}

0 comments on commit 5cd2896

Please sign in to comment.