From afa6411004897e6eedc78846409837d52ba7d911 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Tue, 23 Aug 2022 15:37:31 +0800 Subject: [PATCH] add array replace feature --- src/ZM/Config/RefactoredConfig.php | 4 +++- tests/ZM/Config/RefactoredConfigTest.php | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ZM/Config/RefactoredConfig.php b/src/ZM/Config/RefactoredConfig.php index 139e0c77..dbf8c4b3 100644 --- a/src/ZM/Config/RefactoredConfig.php +++ b/src/ZM/Config/RefactoredConfig.php @@ -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)); } /** diff --git a/tests/ZM/Config/RefactoredConfigTest.php b/tests/ZM/Config/RefactoredConfigTest.php index 1fcaab06..2e1ee9c5 100644 --- a/tests/ZM/Config/RefactoredConfigTest.php +++ b/tests/ZM/Config/RefactoredConfigTest.php @@ -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', - ' "yes"];' + ' "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')); + } }