From 18892a14c21b1cf2c266b7f89acf44d2b3c80c26 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Tue, 23 Aug 2022 17:51:20 +0800 Subject: [PATCH] ready to replace legacy config --- src/Globals/global_functions.php | 29 +++- .../{RefactoredConfig.php => ZMConfig.php} | 2 +- .../Container/ContainerServicesProvider.php | 8 +- ...actoredConfigTest.php => ZMConfigTest.php} | 10 +- tests_old/ZM/Config/ZMConfigTest.php | 164 ------------------ 5 files changed, 39 insertions(+), 174 deletions(-) rename src/ZM/Config/{RefactoredConfig.php => ZMConfig.php} (99%) rename tests/ZM/Config/{RefactoredConfigTest.php => ZMConfigTest.php} (95%) delete mode 100644 tests_old/ZM/Config/ZMConfigTest.php diff --git a/src/Globals/global_functions.php b/src/Globals/global_functions.php index 15c76222..982ddf7b 100644 --- a/src/Globals/global_functions.php +++ b/src/Globals/global_functions.php @@ -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 $abstract + * @template T + * @param class-string $abstract * @return Closure|mixed|T * @noinspection PhpDocMissingThrowsInspection */ @@ -156,7 +157,7 @@ function resolve(string $abstract, array $parameters = []) * 获取容器实例 * * @template T - * @param null|class-string $abstract + * @param null|class-string $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); +} diff --git a/src/ZM/Config/RefactoredConfig.php b/src/ZM/Config/ZMConfig.php similarity index 99% rename from src/ZM/Config/RefactoredConfig.php rename to src/ZM/Config/ZMConfig.php index 2a5ab46a..73780924 100644 --- a/src/ZM/Config/RefactoredConfig.php +++ b/src/ZM/Config/ZMConfig.php @@ -7,7 +7,7 @@ namespace ZM\Config; use OneBot\V12\Config\Config; use ZM\Exception\ConfigException; -class RefactoredConfig +class ZMConfig { /** * @var array 支持的文件扩展名 diff --git a/src/ZM/Container/ContainerServicesProvider.php b/src/ZM/Container/ContainerServicesProvider.php index 26a79e85..7a80df07 100644 --- a/src/ZM/Container/ContainerServicesProvider.php +++ b/src/ZM/Container/ContainerServicesProvider.php @@ -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'); } /** diff --git a/tests/ZM/Config/RefactoredConfigTest.php b/tests/ZM/Config/ZMConfigTest.php similarity index 95% rename from tests/ZM/Config/RefactoredConfigTest.php rename to tests/ZM/Config/ZMConfigTest.php index 41541530..a2ca06fa 100644 --- a/tests/ZM/Config/RefactoredConfigTest.php +++ b/tests/ZM/Config/ZMConfigTest.php @@ -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 ' "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); } diff --git a/tests_old/ZM/Config/ZMConfigTest.php b/tests_old/ZM/Config/ZMConfigTest.php deleted file mode 100644 index 477c286c..00000000 --- a/tests_old/ZM/Config/ZMConfigTest.php +++ /dev/null @@ -1,164 +0,0 @@ - 30055];'); - file_put_contents($mock_dir . '/php_exception.php', ' 30055];'); - file_put_contents($mock_dir . '/global.invalid.development.php', ' 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)); - } -}