add config patch support

This commit is contained in:
sunxyw
2022-08-22 17:02:50 +08:00
parent 488acc3e69
commit b09d12aad5
2 changed files with 27 additions and 20 deletions

View File

@@ -102,7 +102,7 @@ class RefactoredConfig
// 按照加载顺序加载配置文件 // 按照加载顺序加载配置文件
foreach (self::LOAD_ORDER as $load_type) { foreach (self::LOAD_ORDER as $load_type) {
foreach ($stages[$load_type] as $file_path) { foreach ($stages[$load_type] as $file_path) {
logger()->info('Loading config file: ' . $file_path); logger()->info("加载配置文件:{$file_path}");
$this->loadConfigFromPath($file_path); $this->loadConfigFromPath($file_path);
} }
} }
@@ -196,7 +196,6 @@ class RefactoredConfig
*/ */
private function getFileLoadType(string $name): string private function getFileLoadType(string $name): string
{ {
// TODO: 对于多段名称的处理,如 test.patch.development
// 传入此处的 name 参数有三种可能的格式: // 传入此处的 name 参数有三种可能的格式:
// 1. 纯文件名:如 test此时加载类型为 global // 1. 纯文件名:如 test此时加载类型为 global
// 2. 文件名.环境:如 test.development此时加载类型为 environment // 2. 文件名.环境:如 test.development此时加载类型为 environment
@@ -223,20 +222,20 @@ class RefactoredConfig
private function shouldLoadFile(string $path): bool private function shouldLoadFile(string $path): bool
{ {
$name = pathinfo($path, PATHINFO_FILENAME); $name = pathinfo($path, PATHINFO_FILENAME);
// 传入此处的 name 参数有两种可能的格式: // 对于 `global` 和 `patch`,任何情况下均应加载
// 1. 纯文件名:如 test // 对于 `environment`,只有当环境与当前环境相同时才加载
// 2. 文件名.环境:如 test.development // 对于其他情况,则不加载
// 对于第一种格式,在任何情况下均应该加载 $type = $this->getFileLoadType($name);
// 对于第二种格式,只有当环境与当前环境相同时才加载 if ($type === 'global' || $type === 'patch') {
// 至于其他的格式,则为未定义行为
if (strpos($name, '.') === false) {
return true; return true;
} }
$name_and_env = explode('.', $name); if ($type === 'environment') {
if (count($name_and_env) !== 2) { $name_and_env = explode('.', $name);
return false; if ($name_and_env[1] === $this->environment) {
return true;
}
} }
return $name_and_env[1] === $this->environment; return false;
} }
/** /**

View File

@@ -43,20 +43,23 @@ class RefactoredConfigTest extends TestCase
'a' => [ 'a' => [
'b.c' => 'd', 'b.c' => 'd',
], ],
'from' => 'global', 'global' => 'yes',
]; ];
// 下方测试需要临时写入的文件 // 下方测试需要临时写入的文件
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( file_put_contents(
$mock_dir . '/test.development.php', $mock_dir . '/test.development.php',
'<?php return ["from" => "environment", "env" => "development"];' '<?php return ["environment" => "yes", "env" => "development"];'
); );
file_put_contents( file_put_contents(
$mock_dir . '/test.production.php', $mock_dir . '/test.production.php',
'<?php return ["from" => "environment", "env" => "production"];' '<?php return ["environment" => "yes", "env" => "production"];'
);
file_put_contents(
$mock_dir . '/test.patch.php',
'<?php return ["patch" => "yes"];'
); );
file_put_contents($mock_dir . '/test.invalid.php', '<?php return ["from" => "invalid"];');
$config = new RefactoredConfig([ $config = new RefactoredConfig([
__DIR__ . '/config_mock', __DIR__ . '/config_mock',
@@ -126,10 +129,15 @@ class RefactoredConfigTest extends TestCase
public function testGetEnvironmentSpecifiedValue(): void public function testGetEnvironmentSpecifiedValue(): void
{ {
$this->assertSame('environment', self::$config->get('test.from')); $this->assertSame('yes', self::$config->get('test.environment'));
$this->assertSame('development', self::$config->get('test.env')); $this->assertSame('development', self::$config->get('test.env'));
} }
public function testGetPatchSpecifiedValue(): void
{
$this->assertSame('yes', self::$config->get('test.patch'));
}
/** /**
* @dataProvider providerTestGetFileLoadType * @dataProvider providerTestGetFileLoadType
*/ */
@@ -145,9 +153,9 @@ class RefactoredConfigTest extends TestCase
return [ return [
'global' => ['test', 'global'], 'global' => ['test', 'global'],
'environment' => ['test.development', 'environment'], 'environment' => ['test.development', 'environment'],
'undefined' => ['test.dev.inv', 'undefined'],
'patch' => ['test.patch', 'patch'], 'patch' => ['test.patch', 'patch'],
// 'complex' => ['test.patch.development', 'patch'], // complex case are not supported yet
'invalid' => ['test.patch.development', 'undefined'],
]; ];
} }
} }