ready to replace legacy config

This commit is contained in:
sunxyw
2022-08-23 17:51:20 +08:00
parent 6cbde80a1c
commit 18892a14c2
5 changed files with 39 additions and 174 deletions

View File

@@ -7,6 +7,7 @@ use OneBot\Driver\Coroutine\CoroutineInterface;
use OneBot\Driver\Process\ExecutionResult;
use OneBot\V12\Object\MessageSegment;
use Psr\Log\LoggerInterface;
use ZM\Config\ZMConfig;
use ZM\Container\Container;
use ZM\Container\ContainerInterface;
use ZM\Context\Context;
@@ -141,8 +142,8 @@ function container(): ContainerInterface
/**
* 解析类实例(使用容器)
*
* @template T
* @param class-string<T> $abstract
* @template T
* @param class-string<T> $abstract
* @return Closure|mixed|T
* @noinspection PhpDocMissingThrowsInspection
*/
@@ -156,7 +157,7 @@ function resolve(string $abstract, array $parameters = [])
* 获取容器实例
*
* @template T
* @param null|class-string<T> $abstract
* @param null|class-string<T> $abstract
* @return Closure|ContainerInterface|mixed|T
*/
function app(string $abstract = null, array $parameters = [])
@@ -187,3 +188,25 @@ function mysql_builder(string $name = '')
{
return (new MySQLWrapper($name))->createQueryBuilder();
}
/**
* 获取 / 设置配置项
*
* 传入键名和(或)默认值,获取配置项
* 传入数组,设置配置项
* 不传参数,返回配置容器
*
* @param null|string|array $key 键名
* @param mixed $default 默认值
* @return mixed|ZMConfig
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return resolve('config');
}
if (is_array($key)) {
return resolve('config')->set(...$key);
}
return resolve('config')->get($key, $default);
}

View File

@@ -7,7 +7,7 @@ namespace ZM\Config;
use OneBot\V12\Config\Config;
use ZM\Exception\ConfigException;
class RefactoredConfig
class ZMConfig
{
/**
* @var array 支持的文件扩展名

View File

@@ -29,7 +29,7 @@ class ContainerServicesProvider
* connection: open, close, message
* ```
*
* @param string $scope 作用域
* @param string $scope 作用域
* @throws ConfigException
*/
public function registerServices(string $scope, ...$params): void
@@ -81,6 +81,12 @@ class ContainerServicesProvider
// 注册logger
$container->instance(LoggerInterface::class, logger());
// 注册config
$container->instance(ZMConfig::class, new ZMConfig([
SOURCE_ROOT_DIR . '/config',
]));
$container->alias(ZMConfig::class, 'config');
}
/**

View File

@@ -5,15 +5,15 @@ declare(strict_types=1);
namespace Tests\ZM\Config;
use PHPUnit\Framework\TestCase;
use ZM\Config\RefactoredConfig;
use ZM\Config\ZMConfig;
use ZM\Utils\ReflectionUtil;
/**
* @internal
*/
class RefactoredConfigTest extends TestCase
class ZMConfigTest extends TestCase
{
private static RefactoredConfig $config;
private static ZMConfig $config;
public static function setUpBeforeClass(): void
{
@@ -64,7 +64,7 @@ class RefactoredConfigTest extends TestCase
'<?php return ["patch" => "yes", "another array" => ["far", "baz"]];'
);
$config = new RefactoredConfig([
$config = new ZMConfig([
__DIR__ . '/config_mock',
], 'development');
self::$config = $config;
@@ -146,7 +146,7 @@ class RefactoredConfigTest extends TestCase
*/
public function testGetFileLoadType(string $name, string $type): void
{
$method = ReflectionUtil::getMethod(RefactoredConfig::class, 'getFileLoadType');
$method = ReflectionUtil::getMethod(ZMConfig::class, 'getFileLoadType');
$actual = $method->invokeArgs(self::$config, [$name]);
$this->assertSame($type, $actual);
}

View File

@@ -1,164 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\ZM\Config;
use PHPUnit\Framework\TestCase;
use ZM\Config\ZMConfig;
use ZM\Console\Console;
use ZM\Exception\ConfigException;
use ZM\Utils\DataProvider;
/**
* @internal
*/
class ZMConfigTest extends TestCase
{
public static function setUpBeforeClass(): void
{
$mock_dir = __DIR__ . '/config_mock';
ZMConfig::reload();
ZMConfig::setDirectory(__DIR__ . '/config_mock');
if (!is_dir($mock_dir)) {
mkdir($mock_dir, 0755, true);
}
// 下方测试需要临时写入的文件
file_put_contents($mock_dir . '/global.patch.php', '<?php return ["port" => 30055];');
file_put_contents($mock_dir . '/php_exception.php', '<?php return true;');
file_put_contents($mock_dir . '/json_exception.json', '"string"');
file_put_contents($mock_dir . '/global.development.patch.php', '<?php return ["port" => 30055];');
file_put_contents($mock_dir . '/global.invalid.development.php', '<?php return ["port" => 30055];');
file_put_contents($mock_dir . '/fake.development.json', '{"multi":{"level":"test"}}');
file_put_contents($mock_dir . '/no_main_only_patch.patch.json', '{"multi":{"level":"test"}}');
}
public static function tearDownAfterClass(): void
{
ZMConfig::reload();
ZMConfig::restoreDirectory();
foreach (DataProvider::scanDirFiles(__DIR__ . '/config_mock', true, false) as $file) {
unlink($file);
}
rmdir(__DIR__ . '/config_mock');
}
/**
* @throws ConfigException
*/
public function testReload()
{
$this->markTestIncomplete('logger level change in need');
$this->expectOutputRegex('/没读取过,正在从文件加载/');
$this->assertEquals('0.0.0.0', ZMConfig::get('global.host'));
ZMConfig::reload();
Console::setLevel(4);
$this->assertEquals('0.0.0.0', ZMConfig::get('global.host'));
Console::setLevel(0);
}
public function testSetAndRestoreDirectory()
{
$origin = ZMConfig::getDirectory();
ZMConfig::setDirectory('.');
$this->assertEquals('.', ZMConfig::getDirectory());
ZMConfig::restoreDirectory();
$this->assertEquals($origin, ZMConfig::getDirectory());
}
public function testSetAndGetEnv()
{
$this->expectException(ConfigException::class);
ZMConfig::setEnv('production');
$this->assertEquals('production', ZMConfig::getEnv());
ZMConfig::setEnv();
ZMConfig::setEnv('reee');
}
/**
* @dataProvider providerTestGet
* @param mixed $expected
* @throws ConfigException
*/
public function testGet(array $data_params, $expected)
{
$this->assertEquals($expected, ZMConfig::get(...$data_params));
}
public function providerTestGet(): array
{
return [
'get port' => [['global.port'], 30055],
'get port key 2' => [['global', 'port'], 30055],
'get invalid key' => [['global', 'invalid'], null],
'get another environment' => [['fake.multi.level'], 'test'],
];
}
public function testGetPhpException()
{
$this->expectException(ConfigException::class);
ZMConfig::get('php_exception');
}
public function testGetJsonException()
{
$this->expectException(ConfigException::class);
ZMConfig::get('json_exception');
}
public function testOnlyPatchException()
{
$this->expectException(ConfigException::class);
ZMConfig::get('no_main_only_patch.test');
}
public function testSmartPatch()
{
$array = [
'key-1-1' => 'value-1-1',
'key-1-2' => [
'key-2-1' => [
'key-3-1' => [
'value-3-1',
'value-3-2',
],
],
],
'key-1-3' => [
'key-4-1' => 'value-4-1',
],
];
$patch = [
'key-1-2' => [
'key-2-1' => [
'key-3-1' => [
'value-3-3',
],
],
],
'key-1-3' => [
'key-4-2' => [
'key-5-1' => 'value-5-1',
],
],
];
$expected = [
'key-1-1' => 'value-1-1',
'key-1-2' => [
'key-2-1' => [
'key-3-1' => [
'value-3-3',
],
],
],
'key-1-3' => [
'key-4-1' => 'value-4-1',
'key-4-2' => [
'key-5-1' => 'value-5-1',
],
],
];
$this->assertEquals($expected, ZMConfig::smartPatch($array, $patch));
}
}