mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-02 14:25:38 +08:00
fix inappropriate config group name
This commit is contained in:
@@ -72,8 +72,9 @@ class RefactoredConfig
|
||||
foreach ($this->config_paths as $config_path) {
|
||||
$files = scandir($config_path);
|
||||
foreach ($files as $file) {
|
||||
[, $ext, $load_type,] = $this->getFileMeta($file);
|
||||
// 略过不支持的文件
|
||||
if (!in_array(pathinfo($file, PATHINFO_EXTENSION), self::ALLOWED_FILE_EXTENSIONS, true)) {
|
||||
if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -83,15 +84,12 @@ class RefactoredConfig
|
||||
continue;
|
||||
}
|
||||
|
||||
$file = pathinfo($file, PATHINFO_FILENAME);
|
||||
|
||||
// 略过不应加载的文件
|
||||
if (!$this->shouldLoadFile($file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 略过加载顺序未知的文件
|
||||
$load_type = $this->getFileLoadType($file);
|
||||
if (!in_array($load_type, self::LOAD_ORDER, true)) {
|
||||
continue;
|
||||
}
|
||||
@@ -109,6 +107,124 @@ class RefactoredConfig
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件元信息
|
||||
*
|
||||
* @param string $name 文件名
|
||||
*
|
||||
* @return array 文件元信息,数组元素按次序为:配置组名/扩展名/加载类型/环境类型
|
||||
*/
|
||||
private function getFileMeta(string $name): array
|
||||
{
|
||||
$basename = pathinfo($name, PATHINFO_BASENAME);
|
||||
$parts = explode('.', $basename);
|
||||
$ext = array_pop($parts);
|
||||
$load_type = $this->getFileLoadType(implode('.', $parts));
|
||||
if ($load_type === 'global') {
|
||||
$env = null;
|
||||
} else {
|
||||
$env = array_pop($parts);
|
||||
}
|
||||
$group = implode('.', $parts);
|
||||
return [$group, $ext, $load_type, $env];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件加载类型
|
||||
*
|
||||
* @param string $name 文件名
|
||||
*
|
||||
* @return string 可能为:global, environment, patch
|
||||
*/
|
||||
private function getFileLoadType(string $name): string
|
||||
{
|
||||
// 传入此处的 name 参数有三种可能的格式:
|
||||
// 1. 纯文件名:如 test,此时加载类型为 global
|
||||
// 2. 文件名.环境:如 test.development,此时加载类型为 environment
|
||||
// 3. 文件名.patch:如 test.patch,此时加载类型为 patch
|
||||
// 至于其他的格式,则为未定义行为
|
||||
if (strpos($name, '.') === false) {
|
||||
return 'global';
|
||||
}
|
||||
$name_and_env = explode('.', $name);
|
||||
if (count($name_and_env) !== 2) {
|
||||
return 'undefined';
|
||||
}
|
||||
if ($name_and_env[1] === 'patch') {
|
||||
return 'patch';
|
||||
}
|
||||
return 'environment';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否应该加载配置文件
|
||||
*
|
||||
* @param string $path 文件名,包含扩展名
|
||||
*/
|
||||
private function shouldLoadFile(string $path): bool
|
||||
{
|
||||
$name = pathinfo($path, PATHINFO_FILENAME);
|
||||
// 传入此处的 name 参数有两种可能的格式:
|
||||
// 1. 纯文件名:如 test
|
||||
// 2. 文件名.环境:如 test.development
|
||||
// 对于第一种格式,在任何情况下均应该加载
|
||||
// 对于第二种格式,只有当环境与当前环境相同时才加载
|
||||
// 至于其他的格式,则为未定义行为
|
||||
if (strpos($name, '.') === false) {
|
||||
return true;
|
||||
}
|
||||
$name_and_env = explode('.', $name);
|
||||
if (count($name_and_env) !== 2) {
|
||||
return false;
|
||||
}
|
||||
return $name_and_env[1] === $this->environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从传入的路径加载配置文件
|
||||
*
|
||||
* @param string $path 配置文件路径
|
||||
*
|
||||
* @throws ConfigException 传入的配置文件不支持
|
||||
*/
|
||||
private function loadConfigFromPath(string $path): void
|
||||
{
|
||||
if (in_array($path, $this->loaded_files, true)) {
|
||||
return;
|
||||
}
|
||||
$this->loaded_files[] = $path;
|
||||
|
||||
// 判断文件格式是否支持
|
||||
[$group, $ext, $load_type, $env] = $this->getFileMeta($path);
|
||||
if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
|
||||
throw new ConfigException('E00079', "不支持的配置文件格式:{$ext}");
|
||||
}
|
||||
|
||||
// 读取并解析配置
|
||||
$content = file_get_contents($path);
|
||||
$config = [];
|
||||
switch ($ext) {
|
||||
case 'php':
|
||||
$config = require $path;
|
||||
break;
|
||||
case 'json':
|
||||
$config = json_decode($content, true);
|
||||
break;
|
||||
case 'yaml':
|
||||
case 'yml':
|
||||
// TODO: 实现yaml解析
|
||||
break;
|
||||
case 'toml':
|
||||
// TODO: 实现toml解析
|
||||
break;
|
||||
default:
|
||||
throw new ConfigException('E00079', "不支持的配置文件格式:{$ext}");
|
||||
}
|
||||
|
||||
// 加入配置
|
||||
$this->merge($group, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并传入的配置数组至指定的配置项
|
||||
*
|
||||
@@ -165,101 +281,4 @@ class RefactoredConfig
|
||||
$this->holder = new Config([]);
|
||||
$this->loadFiles();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件加载类型
|
||||
*
|
||||
* @param string $name 文件名
|
||||
*
|
||||
* @return string 可能为:global, environment, patch
|
||||
*/
|
||||
private function getFileLoadType(string $name): string
|
||||
{
|
||||
// 传入此处的 name 参数有三种可能的格式:
|
||||
// 1. 纯文件名:如 test,此时加载类型为 global
|
||||
// 2. 文件名.环境:如 test.development,此时加载类型为 environment
|
||||
// 3. 文件名.patch:如 test.patch,此时加载类型为 patch
|
||||
// 至于其他的格式,则为未定义行为
|
||||
if (strpos($name, '.') === false) {
|
||||
return 'global';
|
||||
}
|
||||
$name_and_env = explode('.', $name);
|
||||
if (count($name_and_env) !== 2) {
|
||||
return 'undefined';
|
||||
}
|
||||
if ($name_and_env[1] === 'patch') {
|
||||
return 'patch';
|
||||
}
|
||||
return 'environment';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否应该加载配置文件
|
||||
*
|
||||
* @param string $name 文件名
|
||||
*/
|
||||
private function shouldLoadFile(string $name): bool
|
||||
{
|
||||
// 传入此处的 name 参数有两种可能的格式:
|
||||
// 1. 纯文件名:如 test
|
||||
// 2. 文件名.环境:如 test.development
|
||||
// 对于第一种格式,在任何情况下均应该加载
|
||||
// 对于第二种格式,只有当环境与当前环境相同时才加载
|
||||
// 至于其他的格式,则为未定义行为
|
||||
if (strpos($name, '.') === false) {
|
||||
return true;
|
||||
}
|
||||
$name_and_env = explode('.', $name);
|
||||
if (count($name_and_env) !== 2) {
|
||||
return false;
|
||||
}
|
||||
return $name_and_env[1] === $this->environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从传入的路径加载配置文件
|
||||
*
|
||||
* @param string $path 配置文件路径
|
||||
*
|
||||
* @throws ConfigException 传入的配置文件不支持
|
||||
*/
|
||||
private function loadConfigFromPath(string $path): void
|
||||
{
|
||||
if (in_array($path, $this->loaded_files, true)) {
|
||||
return;
|
||||
}
|
||||
$this->loaded_files[] = $path;
|
||||
|
||||
// 判断文件格式是否支持
|
||||
$info = pathinfo($path);
|
||||
$name = $info['filename'];
|
||||
$ext = $info['extension'];
|
||||
if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
|
||||
throw new ConfigException('E00079', "不支持的配置文件格式:{$ext}");
|
||||
}
|
||||
|
||||
// 读取并解析配置
|
||||
$content = file_get_contents($path);
|
||||
$config = [];
|
||||
switch ($ext) {
|
||||
case 'php':
|
||||
$config = require $path;
|
||||
break;
|
||||
case 'json':
|
||||
$config = json_decode($content, true);
|
||||
break;
|
||||
case 'yaml':
|
||||
case 'yml':
|
||||
// TODO: 实现yaml解析
|
||||
break;
|
||||
case 'toml':
|
||||
// TODO: 实现toml解析
|
||||
break;
|
||||
default:
|
||||
throw new ConfigException('E00079', "不支持的配置文件格式:{$ext}");
|
||||
}
|
||||
|
||||
// 加入配置
|
||||
$this->merge($name, $config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,9 +43,9 @@ class RefactoredConfigTest extends TestCase
|
||||
// 下方测试需要临时写入的文件
|
||||
file_put_contents($mock_dir . '/test.php', '<?php return ' . var_export($test_config, true) . ';');
|
||||
file_put_contents($mock_dir . '/test.development.php',
|
||||
'<?php return ["from" => "environment", "env" => "dev"];');
|
||||
'<?php return ["from" => "environment", "env" => "development"];');
|
||||
file_put_contents($mock_dir . '/test.production.php',
|
||||
'<?php return ["from" => "environment", "env" => "prod"];');
|
||||
'<?php return ["from" => "environment", "env" => "production"];');
|
||||
file_put_contents($mock_dir . '/test.invalid.php', '<?php return ["from" => "invalid"];');
|
||||
|
||||
$config = new RefactoredConfig([
|
||||
@@ -98,4 +98,10 @@ class RefactoredConfigTest extends TestCase
|
||||
$this->assertSame(['a', 'b'], self::$config->get('array'));
|
||||
$this->assertSame('a', self::$config->get('array.0'));
|
||||
}
|
||||
|
||||
public function testGetEnvironmentSpecifiedValue(): void
|
||||
{
|
||||
$this->assertSame('environment', self::$config->get('test.from'));
|
||||
$this->assertSame('development', self::$config->get('test.env'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user