add array replace feature

This commit is contained in:
sunxyw 2022-08-23 15:37:31 +08:00
parent 5d8a1deb35
commit afa6411004
No known key found for this signature in database
GPG Key ID: F391C42B19AFFC98
2 changed files with 14 additions and 2 deletions

View File

@ -111,13 +111,15 @@ class RefactoredConfig
/**
* 合并传入的配置数组至指定的配置项
*
* 请注意内部实现是 array_replace_recursive而不是 array_merge
*
* @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));
$this->set($key, array_replace_recursive($original, $config));
}
/**

View File

@ -44,6 +44,9 @@ class RefactoredConfigTest extends TestCase
'b.c' => 'd',
],
'global' => 'yes',
'another array' => [
'foo', 'bar',
],
];
// 下方测试需要临时写入的文件
@ -58,7 +61,7 @@ class RefactoredConfigTest extends TestCase
);
file_put_contents(
$mock_dir . '/test.patch.php',
'<?php return ["patch" => "yes"];'
'<?php return ["patch" => "yes", "another array" => ["far", "baz"]];'
);
$config = new RefactoredConfig([
@ -158,4 +161,11 @@ class RefactoredConfigTest extends TestCase
'invalid' => ['test.patch.development', 'undefined'],
];
}
public function testArrayReplaceInsteadOfMerge(): void
{
// using of space inside config key is not an officially supported feature,
// it may be removed in the future, please avoid using it in your project.
$this->assertSame(['far', 'baz'], self::$config->get('test.another array'));
}
}