diff --git a/src/Globals/global_functions.php b/src/Globals/global_functions.php index 3f4df067..862741d1 100644 --- a/src/Globals/global_functions.php +++ b/src/Globals/global_functions.php @@ -196,17 +196,19 @@ function mysql_builder(string $name = '') * 传入数组,设置配置项 * 不传参数,返回配置容器 * - * @param null|array|string $key 键名 - * @param mixed $default 默认值 - * @return mixed|ZMConfig + * @param null|array|string $key 键名 + * @param mixed $default 默认值 + * @return mixed|void|ZMConfig */ function config($key = null, $default = null) { + $config = ZMConfig::getInstance(); if (is_null($key)) { - return resolve('config'); + return $config; } if (is_array($key)) { - return resolve('config')->set(...$key); + $config->set($key); + return; } - return resolve('config')->get($key, $default); + return $config->get($key, $default); } diff --git a/src/ZM/Config/ZMConfig.php b/src/ZM/Config/ZMConfig.php index 3b759ffc..faf0b1c5 100644 --- a/src/ZM/Config/ZMConfig.php +++ b/src/ZM/Config/ZMConfig.php @@ -4,11 +4,14 @@ declare(strict_types=1); namespace ZM\Config; +use OneBot\Util\Singleton; use OneBot\V12\Config\Config; use ZM\Exception\ConfigException; class ZMConfig implements \ArrayAccess { + use Singleton; + /** * @var array 支持的文件扩展名 */ @@ -19,6 +22,11 @@ class ZMConfig implements \ArrayAccess */ public const LOAD_ORDER = ['default', 'environment', 'patch']; + /** + * @var string 默认配置文件路径 + */ + public const DEFAULT_CONFIG_PATH = SOURCE_ROOT_DIR . '/config'; + /** * @var array 已加载的配置文件 */ @@ -47,9 +55,9 @@ class ZMConfig implements \ArrayAccess * * @throws ConfigException 配置文件加载出错 */ - public function __construct(array $config_paths, string $environment = 'development') + public function __construct(array $config_paths = [], string $environment = 'development') { - $this->config_paths = $config_paths; + $this->config_paths = $config_paths ?: [self::DEFAULT_CONFIG_PATH]; $this->environment = $environment; $this->holder = new Config([]); $this->loadFiles(); @@ -166,12 +174,18 @@ class ZMConfig implements \ArrayAccess * 设置配置项 * 仅在本次运行期间生效,不会保存到配置文件中哦 * - * @param string $key 配置项名称,可使用.访问数组 - * @param mixed $value 要写入的值,传入 null 会进行删除 + * 如果传入的是数组,则会将键名作为配置项名称,并将值作为配置项的值 + * 顺带一提,数组支持批量设置 + * + * @param array|string $key 配置项名称,可使用.访问数组 + * @param mixed $value 要写入的值,传入 null 会进行删除 */ - public function set(string $key, $value): void + public function set($key, $value = null): void { - $this->holder->set($key, $value); + $keys = is_array($key) ? $key : [$key => $value]; + foreach ($keys as $i_key => $i_val) { + $this->holder->set($i_key, $i_val); + } } /** @@ -199,6 +213,7 @@ class ZMConfig implements \ArrayAccess return $this->get($offset) !== null; } + #[\ReturnTypeWillChange] public function offsetGet($offset) { return $this->get($offset); diff --git a/src/ZM/Container/ContainerServicesProvider.php b/src/ZM/Container/ContainerServicesProvider.php index 1f2c5ad3..94d65d65 100644 --- a/src/ZM/Container/ContainerServicesProvider.php +++ b/src/ZM/Container/ContainerServicesProvider.php @@ -10,10 +10,8 @@ use OneBot\Driver\Event\Http\HttpRequestEvent; use OneBot\Driver\Process\ProcessManager; use Psr\Http\Message\ServerRequestInterface; use Psr\Log\LoggerInterface; -use ZM\Config\ZMConfig; use ZM\Context\Context; use ZM\Context\ContextInterface; -use ZM\Exception\ConfigException; use ZM\Framework; class ContainerServicesProvider @@ -29,8 +27,7 @@ class ContainerServicesProvider * connection: open, close, message * ``` * - * @param string $scope 作用域 - * @throws ConfigException + * @param string $scope 作用域 */ public function registerServices(string $scope, ...$params): void { @@ -63,8 +60,6 @@ class ContainerServicesProvider /** * 注册全局服务 - * - * @throws ConfigException */ private function registerGlobalServices(ContainerInterface $container): void { @@ -81,12 +76,6 @@ 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/src/ZM/Framework.php b/src/ZM/Framework.php index 7fde6191..d4d5ef67 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -328,7 +328,7 @@ class Framework // 打印驱动 $properties['driver'] = config('global.driver'); // 打印logger显示等级 - $properties['log_level'] = $this->argv['log-level'] ?? config('global', 'log_level') ?? 'info'; + $properties['log_level'] = $this->argv['log-level'] ?? config('global.log_level') ?? 'info'; // 打印框架版本 $properties['version'] = self::VERSION . (LOAD_MODE === 0 ? (' (build ' . ZM_VERSION_ID . ')') : ''); // 打印 PHP 版本