fix code styles

This commit is contained in:
sunxyw 2022-08-20 18:40:54 +08:00
parent 90282b858e
commit 1a1c65afce
No known key found for this signature in database
GPG Key ID: F391C42B19AFFC98
2 changed files with 73 additions and 64 deletions

View File

@ -42,8 +42,8 @@ class RefactoredConfig
/**
* 构造配置实例
*
* @param array $config_paths 配置文件路径
* @param string $environment 环境
* @param array $config_paths 配置文件路径
* @param string $environment 环境
*
* @throws ConfigException 配置文件加载出错
*/
@ -107,6 +107,63 @@ class RefactoredConfig
}
}
/**
* 合并传入的配置数组至指定的配置项
*
* @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();
}
/**
* 获取文件元信息
*
@ -224,61 +281,4 @@ class RefactoredConfig
// 加入配置
$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

@ -1,10 +1,15 @@
<?php
declare(strict_types=1);
namespace Tests\ZM\Config;
use ZM\Config\RefactoredConfig;
use PHPUnit\Framework\TestCase;
use ZM\Config\RefactoredConfig;
/**
* @internal
*/
class RefactoredConfigTest extends TestCase
{
private static $config;
@ -42,10 +47,14 @@ class RefactoredConfigTest extends TestCase
// 下方测试需要临时写入的文件
file_put_contents($mock_dir . '/test.php', '<?php return ' . var_export($test_config, true) . ';');
file_put_contents($mock_dir . '/test.development.php',
'<?php return ["from" => "environment", "env" => "development"];');
file_put_contents($mock_dir . '/test.production.php',
'<?php return ["from" => "environment", "env" => "production"];');
file_put_contents(
$mock_dir . '/test.development.php',
'<?php return ["from" => "environment", "env" => "development"];'
);
file_put_contents(
$mock_dir . '/test.production.php',
'<?php return ["from" => "environment", "env" => "production"];'
);
file_put_contents($mock_dir . '/test.invalid.php', '<?php return ["from" => "invalid"];');
$config = new RefactoredConfig([