mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-18 05:04:51 +08:00
allow multiple set config
This commit is contained in:
parent
eb7e700e7c
commit
975d0e3540
@ -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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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 版本
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user