mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-22 16:15:34 +08:00
fix code styles
This commit is contained in:
@@ -42,8 +42,8 @@ class RefactoredConfig
|
|||||||
/**
|
/**
|
||||||
* 构造配置实例
|
* 构造配置实例
|
||||||
*
|
*
|
||||||
* @param array $config_paths 配置文件路径
|
* @param array $config_paths 配置文件路径
|
||||||
* @param string $environment 环境
|
* @param string $environment 环境
|
||||||
*
|
*
|
||||||
* @throws ConfigException 配置文件加载出错
|
* @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);
|
$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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Tests\ZM\Config;
|
namespace Tests\ZM\Config;
|
||||||
|
|
||||||
use ZM\Config\RefactoredConfig;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use ZM\Config\RefactoredConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
class RefactoredConfigTest extends TestCase
|
class RefactoredConfigTest extends TestCase
|
||||||
{
|
{
|
||||||
private static $config;
|
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.php', '<?php return ' . var_export($test_config, true) . ';');
|
||||||
file_put_contents($mock_dir . '/test.development.php',
|
file_put_contents(
|
||||||
'<?php return ["from" => "environment", "env" => "development"];');
|
$mock_dir . '/test.development.php',
|
||||||
file_put_contents($mock_dir . '/test.production.php',
|
'<?php return ["from" => "environment", "env" => "development"];'
|
||||||
'<?php return ["from" => "environment", "env" => "production"];');
|
);
|
||||||
|
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"];');
|
file_put_contents($mock_dir . '/test.invalid.php', '<?php return ["from" => "invalid"];');
|
||||||
|
|
||||||
$config = new RefactoredConfig([
|
$config = new RefactoredConfig([
|
||||||
|
|||||||
Reference in New Issue
Block a user