fix inappropriate config group name

This commit is contained in:
sunxyw
2022-08-20 18:28:22 +08:00
parent f819922d8a
commit 90282b858e
2 changed files with 89 additions and 64 deletions

View File

@@ -42,8 +42,8 @@ class RefactoredConfig
/** /**
* 构造配置实例 * 构造配置实例
* *
* @param array $config_paths 配置文件路径 * @param array $config_paths 配置文件路径
* @param string $environment 环境 * @param string $environment 环境
* *
* @throws ConfigException 配置文件加载出错 * @throws ConfigException 配置文件加载出错
*/ */
@@ -72,8 +72,9 @@ class RefactoredConfig
foreach ($this->config_paths as $config_path) { foreach ($this->config_paths as $config_path) {
$files = scandir($config_path); $files = scandir($config_path);
foreach ($files as $file) { 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; continue;
} }
@@ -83,15 +84,12 @@ class RefactoredConfig
continue; continue;
} }
$file = pathinfo($file, PATHINFO_FILENAME);
// 略过不应加载的文件 // 略过不应加载的文件
if (!$this->shouldLoadFile($file)) { if (!$this->shouldLoadFile($file)) {
continue; continue;
} }
// 略过加载顺序未知的文件 // 略过加载顺序未知的文件
$load_type = $this->getFileLoadType($file);
if (!in_array($load_type, self::LOAD_ORDER, true)) { if (!in_array($load_type, self::LOAD_ORDER, true)) {
continue; continue;
} }
@@ -110,60 +108,25 @@ class RefactoredConfig
} }
/** /**
* 合并传入的配置数组至指定的配置项 * 获取文件元信息
* *
* @param string $key 目标配置项,必须为数组 * @param string $name 文件名
* @param array $config 要合并的配置数组
*/
public function merge(string $key, array $config): void
{
$original = $this->get($key, []);
$this->set($key, array_merge($original, $config));
}
/**
* 获取配置项
* *
* @param string $key 配置项名称,可使用.访问数组 * @return array 文件元信息,数组元素按次序为:配置组名/扩展名/加载类型/环境类型
* @param mixed $default 默认值
*
* @return null|array|mixed
*/ */
public function get(string $key, $default = null) private function getFileMeta(string $name): array
{ {
return $this->holder->get($key, $default); $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 {
* @param string $key 配置项名称,可使用.访问数组 $env = array_pop($parts);
* @param mixed $value 要写入的值,传入 null 会进行删除 }
*/ $group = implode('.', $parts);
public function set(string $key, $value): void return [$group, $ext, $load_type, $env];
{
$this->holder->set($key, $value);
}
/**
* 获取内部配置容器
*/
public function getHolder(): Config
{
return $this->holder;
}
/**
* 重载配置文件
* 运行期间新增的配置文件不会被加载哟~
*
* @throws ConfigException
*/
public function reload(): void
{
$this->holder = new Config([]);
$this->loadFiles();
} }
/** /**
@@ -196,10 +159,11 @@ class RefactoredConfig
/** /**
* 判断是否应该加载配置文件 * 判断是否应该加载配置文件
* *
* @param string $name 文件 * @param string $path 文件名,包含扩展
*/ */
private function shouldLoadFile(string $name): bool private function shouldLoadFile(string $path): bool
{ {
$name = pathinfo($path, PATHINFO_FILENAME);
// 传入此处的 name 参数有两种可能的格式: // 传入此处的 name 参数有两种可能的格式:
// 1. 纯文件名:如 test // 1. 纯文件名:如 test
// 2. 文件名.环境:如 test.development // 2. 文件名.环境:如 test.development
@@ -231,9 +195,7 @@ class RefactoredConfig
$this->loaded_files[] = $path; $this->loaded_files[] = $path;
// 判断文件格式是否支持 // 判断文件格式是否支持
$info = pathinfo($path); [$group, $ext, $load_type, $env] = $this->getFileMeta($path);
$name = $info['filename'];
$ext = $info['extension'];
if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) { if (!in_array($ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
throw new ConfigException('E00079', "不支持的配置文件格式:{$ext}"); throw new ConfigException('E00079', "不支持的配置文件格式:{$ext}");
} }
@@ -260,6 +222,63 @@ class RefactoredConfig
} }
// 加入配置 // 加入配置
$this->merge($name, $config); $this->merge($group, $config);
}
/**
* 合并传入的配置数组至指定的配置项
*
* @param string $key 目标配置项,必须为数组
* @param array $config 要合并的配置数组
*/
public function merge(string $key, array $config): void
{
$original = $this->get($key, []);
$this->set($key, array_merge($original, $config));
}
/**
* 获取配置项
*
* @param string $key 配置项名称,可使用.访问数组
* @param mixed $default 默认值
*
* @return null|array|mixed
*/
public function get(string $key, $default = null)
{
return $this->holder->get($key, $default);
}
/**
* 设置配置项
* 仅在本次运行期间生效,不会保存到配置文件中哦
*
* @param string $key 配置项名称,可使用.访问数组
* @param mixed $value 要写入的值,传入 null 会进行删除
*/
public function set(string $key, $value): void
{
$this->holder->set($key, $value);
}
/**
* 获取内部配置容器
*/
public function getHolder(): Config
{
return $this->holder;
}
/**
* 重载配置文件
* 运行期间新增的配置文件不会被加载哟~
*
* @throws ConfigException
*/
public function reload(): void
{
$this->holder = new Config([]);
$this->loadFiles();
} }
} }

View File

@@ -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.php', '<?php return ' . var_export($test_config, true) . ';');
file_put_contents($mock_dir . '/test.development.php', 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', 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"];'); file_put_contents($mock_dir . '/test.invalid.php', '<?php return ["from" => "invalid"];');
$config = new RefactoredConfig([ $config = new RefactoredConfig([
@@ -98,4 +98,10 @@ class RefactoredConfigTest extends TestCase
$this->assertSame(['a', 'b'], self::$config->get('array')); $this->assertSame(['a', 'b'], self::$config->get('array'));
$this->assertSame('a', self::$config->get('array.0')); $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'));
}
} }