mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-02 14:25:38 +08:00
add array replace feature
This commit is contained in:
@@ -111,13 +111,15 @@ class RefactoredConfig
|
|||||||
/**
|
/**
|
||||||
* 合并传入的配置数组至指定的配置项
|
* 合并传入的配置数组至指定的配置项
|
||||||
*
|
*
|
||||||
|
* 请注意内部实现是 array_replace_recursive,而不是 array_merge
|
||||||
|
*
|
||||||
* @param string $key 目标配置项,必须为数组
|
* @param string $key 目标配置项,必须为数组
|
||||||
* @param array $config 要合并的配置数组
|
* @param array $config 要合并的配置数组
|
||||||
*/
|
*/
|
||||||
public function merge(string $key, array $config): void
|
public function merge(string $key, array $config): void
|
||||||
{
|
{
|
||||||
$original = $this->get($key, []);
|
$original = $this->get($key, []);
|
||||||
$this->set($key, array_merge($original, $config));
|
$this->set($key, array_replace_recursive($original, $config));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ class RefactoredConfigTest extends TestCase
|
|||||||
'b.c' => 'd',
|
'b.c' => 'd',
|
||||||
],
|
],
|
||||||
'global' => 'yes',
|
'global' => 'yes',
|
||||||
|
'another array' => [
|
||||||
|
'foo', 'bar',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
// 下方测试需要临时写入的文件
|
// 下方测试需要临时写入的文件
|
||||||
@@ -58,7 +61,7 @@ class RefactoredConfigTest extends TestCase
|
|||||||
);
|
);
|
||||||
file_put_contents(
|
file_put_contents(
|
||||||
$mock_dir . '/test.patch.php',
|
$mock_dir . '/test.patch.php',
|
||||||
'<?php return ["patch" => "yes"];'
|
'<?php return ["patch" => "yes", "another array" => ["far", "baz"]];'
|
||||||
);
|
);
|
||||||
|
|
||||||
$config = new RefactoredConfig([
|
$config = new RefactoredConfig([
|
||||||
@@ -158,4 +161,11 @@ class RefactoredConfigTest extends TestCase
|
|||||||
'invalid' => ['test.patch.development', 'undefined'],
|
'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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user