zhamao-framework/src/ZM/Config/ZMConfig.php

387 lines
11 KiB
PHP
Raw Normal View History

2022-08-19 23:10:43 +08:00
<?php
declare(strict_types=1);
namespace ZM\Config;
2022-09-23 22:11:26 +08:00
use OneBot\Config\Config;
2022-12-31 20:12:20 +08:00
use OneBot\Config\Loader\LoaderInterface;
2022-08-23 18:18:20 +08:00
use OneBot\Util\Singleton;
2022-08-19 23:10:43 +08:00
use ZM\Exception\ConfigException;
2023-02-24 16:49:24 +08:00
use ZM\Kernel;
2022-08-19 23:10:43 +08:00
class ZMConfig
2022-08-19 23:10:43 +08:00
{
2022-08-23 18:18:20 +08:00
use Singleton;
2022-08-20 15:58:46 +08:00
/**
* @var array 配置文件加载顺序,后覆盖前
*/
public const LOAD_ORDER = ['default', 'environment', 'patch'];
2022-08-20 15:58:46 +08:00
2022-08-27 01:11:37 +08:00
/**
* @var string[] 环境别名
*/
public static array $environment_alias = [
'dev' => 'development',
'test' => 'testing',
'prod' => 'production',
];
2022-08-19 23:10:43 +08:00
/**
* @var array 已加载的配置文件
*/
2022-08-22 16:29:27 +08:00
private array $loaded_files = [];
2022-08-19 23:10:43 +08:00
2022-12-31 20:12:20 +08:00
/**
* @var array 配置文件扩展名
*/
private array $file_extensions = [];
2022-08-19 23:10:43 +08:00
/**
* @var array 配置文件路径
*/
2022-08-22 16:29:27 +08:00
private array $config_paths;
2022-08-19 23:10:43 +08:00
/**
* @var Config 内部配置容器
*/
2022-08-22 16:29:27 +08:00
private Config $holder;
2022-08-19 23:10:43 +08:00
/**
* @var null|ConfigTracer 配置跟踪器
*/
private ?ConfigTracer $tracer = null;
2022-12-31 20:22:55 +08:00
/**
* @var LoaderInterface 配置加载器
* @phpstan-ignore-next-line We will use this property in the future.
*/
2022-12-31 20:12:20 +08:00
private LoaderInterface $loader;
2022-08-19 23:10:43 +08:00
/**
* 构造配置实例
*
* @throws ConfigException 配置文件加载出错
*/
2023-02-24 16:49:24 +08:00
public function __construct(array $init_config = null)
2022-08-19 23:10:43 +08:00
{
2023-02-24 16:49:24 +08:00
// 合并初始化配置,构造传入优先
$conf = array_merge_recursive($this->loadInitConfig(), $init_config ?? []);
2022-12-31 20:12:20 +08:00
$this->file_extensions = $conf['source']['extensions'];
$this->config_paths = $conf['source']['paths'];
// 初始化配置容器
$this->holder = new Config(
new ($conf['repository'][0])(...$conf['repository'][1]),
);
// 初始化配置加载器
$this->loader = new ($conf['loader'][0])(...$conf['loader'][1]);
2023-02-18 20:56:47 +08:00
// 启用配置跟踪器
if ($conf['trace'] ?? false) {
$this->tracer = new ConfigTracer();
} else {
$this->tracer = null;
}
2022-12-31 20:12:20 +08:00
2023-02-24 16:49:24 +08:00
$this->loadFiles();
2022-08-19 23:10:43 +08:00
}
/**
2022-08-20 15:58:46 +08:00
* 加载配置文件
*
* @throws ConfigException
2022-08-19 23:10:43 +08:00
*/
2022-08-20 15:58:46 +08:00
public function loadFiles(): void
2022-08-19 23:10:43 +08:00
{
2022-08-20 15:58:46 +08:00
$stages = [
'default' => [],
2022-08-20 15:58:46 +08:00
'environment' => [],
'patch' => [],
];
// 遍历所有需加载的文件,并按加载类型进行分组
foreach ($this->config_paths as $config_path) {
$files = scandir($config_path);
foreach ($files as $file) {
2022-09-09 18:59:46 +08:00
[, $ext, $load_type] = $this->getFileMeta($file);
2022-08-20 17:43:06 +08:00
// 略过不支持的文件
2022-12-31 20:12:20 +08:00
if (!in_array($ext, $this->file_extensions, true)) {
2022-08-20 15:58:46 +08:00
continue;
}
2022-08-20 17:43:06 +08:00
2022-08-23 16:00:55 +08:00
$file_path = zm_dir($config_path . '/' . $file);
2022-08-20 15:58:46 +08:00
if (is_dir($file_path)) {
// TODO: 支持子目录(待定)
continue;
}
2022-08-20 17:43:06 +08:00
// 略过不应加载的文件
if (!$this->shouldLoadFile($file)) {
continue;
}
// 略过加载顺序未知的文件
2022-08-20 15:58:46 +08:00
if (!in_array($load_type, self::LOAD_ORDER, true)) {
continue;
}
2022-08-20 17:43:06 +08:00
// 将文件加入到对应的加载阶段
2022-08-20 15:58:46 +08:00
$stages[$load_type][] = $file_path;
}
}
// 按照加载顺序加载配置文件
foreach (self::LOAD_ORDER as $load_type) {
foreach ($stages[$load_type] as $file_path) {
2022-08-20 17:43:06 +08:00
$this->loadConfigFromPath($file_path);
2022-08-20 15:58:46 +08:00
}
}
}
2022-08-20 18:40:54 +08:00
/**
* 合并传入的配置数组至指定的配置项
*
2022-08-23 15:37:31 +08:00
* 请注意内部实现是 array_replace_recursive而不是 array_merge
*
2022-08-20 18:40:54 +08:00
* @param string $key 目标配置项,必须为数组
* @param array $config 要合并的配置数组
*/
public function merge(string $key, array $config): void
{
$original = $this->get($key, []);
2022-08-23 15:37:31 +08:00
$this->set($key, array_replace_recursive($original, $config));
2022-08-20 18:40:54 +08:00
}
/**
* 获取配置项
*
* @param string $key 配置项名称,可使用.访问数组
* @param mixed $default 默认值
*
* @return null|array|mixed
*/
public function get(string $key, mixed $default = null)
2022-08-20 18:40:54 +08:00
{
return $this->holder->get($key, $default);
}
/**
* 设置配置项
* 仅在本次运行期间生效,不会保存到配置文件中哦
*
2022-08-23 18:18:20 +08:00
* 如果传入的是数组,则会将键名作为配置项名称,并将值作为配置项的值
* 顺带一提,数组支持批量设置
*
* @param array|string $key 配置项名称,可使用.访问数组
* @param mixed $value 要写入的值,传入 null 会进行删除
2022-08-20 18:40:54 +08:00
*/
public function set(array|string $key, mixed $value = null): void
2022-08-20 18:40:54 +08:00
{
2022-08-23 18:18:20 +08:00
$keys = is_array($key) ? $key : [$key => $value];
foreach ($keys as $i_key => $i_val) {
$this->holder->set($i_key, $i_val);
}
2022-08-20 18:40:54 +08:00
}
/**
* 添加配置文件路径
*
* @param string $path 路径
2022-08-20 18:40:54 +08:00
*/
public function addConfigPath(string $path): void
2022-08-20 18:40:54 +08:00
{
if (!in_array($path, $this->config_paths, true)) {
$this->config_paths[] = $path;
}
}
2022-08-20 18:40:54 +08:00
/**
* 重载配置文件
* 运行期间新增的配置文件不会被加载哟~
*
* @throws ConfigException
*/
public function reload(): void
{
$this->holder = new Config([]);
2022-08-27 00:38:55 +08:00
$this->loaded_files = [];
2022-08-20 18:40:54 +08:00
$this->loadFiles();
}
/**
* 获取内部配置容器
*/
public function getHolder(): Config
{
return $this->holder;
}
/**
* 获取配置项的来源
*
* @param string $key 配置项
* @return null|string 来源,如果没有找到,返回 null
*/
public function getTrace(string $key): ?string
{
if ($this->tracer === null) {
logger()->warning('你正在获取配置项的来源,但没有开启配置来源追踪功能');
return null;
}
return $this->tracer->getTraceOf($key);
}
2022-08-20 15:58:46 +08:00
/**
2022-08-20 18:28:22 +08:00
* 获取文件元信息
2022-08-19 23:10:43 +08:00
*
2022-08-20 18:28:22 +08:00
* @param string $name 文件名
2022-08-19 23:10:43 +08:00
*
2022-08-20 18:28:22 +08:00
* @return array 文件元信息,数组元素按次序为:配置组名/扩展名/加载类型/环境类型
2022-08-19 23:10:43 +08:00
*/
2022-08-20 18:28:22 +08:00
private function getFileMeta(string $name): array
2022-08-19 23:10:43 +08:00
{
2022-08-20 18:28:22 +08:00
$basename = pathinfo($name, PATHINFO_BASENAME);
$parts = explode('.', $basename);
$ext = array_pop($parts);
$load_type = $this->getFileLoadType(implode('.', $parts));
if ($load_type === 'default') {
2022-08-20 18:28:22 +08:00
$env = null;
} else {
$env = array_pop($parts);
2022-08-27 01:11:37 +08:00
$env = self::$environment_alias[$env] ?? $env;
2022-08-20 18:28:22 +08:00
}
$group = implode('.', $parts);
return [$group, $ext, $load_type, $env];
2022-08-20 15:58:46 +08:00
}
/**
* 获取文件加载类型
*
2022-08-21 16:13:16 +08:00
* @param string $name 文件名,不带扩展名
2022-08-20 15:58:46 +08:00
*
* @return string 可能为default, environment, patch
2022-08-20 15:58:46 +08:00
*/
private function getFileLoadType(string $name): string
{
// 传入此处的 name 参数有三种可能的格式:
// 1. 纯文件名:如 test此时加载类型为 default
2022-08-20 15:58:46 +08:00
// 2. 文件名.环境:如 test.development此时加载类型为 environment
// 3. 文件名.patch如 test.patch此时加载类型为 patch
// 至于其他的格式,则为未定义行为
if (!str_contains($name, '.')) {
return 'default';
2022-08-20 15:58:46 +08:00
}
$name_and_env = explode('.', $name);
if (count($name_and_env) !== 2) {
return 'undefined';
}
if ($name_and_env[1] === 'patch') {
return 'patch';
2022-08-19 23:10:43 +08:00
}
2022-08-20 15:58:46 +08:00
return 'environment';
2022-08-19 23:10:43 +08:00
}
/**
2022-08-20 15:58:46 +08:00
* 判断是否应该加载配置文件
2022-08-19 23:10:43 +08:00
*
2022-08-20 18:28:22 +08:00
* @param string $path 文件名,包含扩展名
2022-08-19 23:10:43 +08:00
*/
2022-08-20 18:28:22 +08:00
private function shouldLoadFile(string $path): bool
2022-08-19 23:10:43 +08:00
{
2022-08-20 18:28:22 +08:00
$name = pathinfo($path, PATHINFO_FILENAME);
// 对于 `default` 和 `patch`,任何情况下均应加载
2022-08-22 17:02:50 +08:00
// 对于 `environment`,只有当环境与当前环境相同时才加载
// 对于其他情况,则不加载
$type = $this->getFileLoadType($name);
if ($type === 'default' || $type === 'patch') {
2022-08-20 15:58:46 +08:00
return true;
}
2022-08-22 17:02:50 +08:00
if ($type === 'environment') {
$name_and_env = explode('.', $name);
2023-02-24 16:49:24 +08:00
if (Kernel::getInstance()->environment($name_and_env[1])) {
2022-08-22 17:02:50 +08:00
return true;
}
2022-08-20 15:58:46 +08:00
}
2022-08-22 17:02:50 +08:00
return false;
2022-08-19 23:10:43 +08:00
}
/**
* 从传入的路径加载配置文件
*
* @param string $path 配置文件路径
*
* @throws ConfigException 传入的配置文件不支持
*/
2022-08-20 15:58:46 +08:00
private function loadConfigFromPath(string $path): void
2022-08-19 23:10:43 +08:00
{
2022-08-20 15:58:46 +08:00
if (in_array($path, $this->loaded_files, true)) {
2022-08-19 23:10:43 +08:00
return;
}
$this->loaded_files[] = $path;
// 判断文件格式是否支持
2022-08-20 18:28:22 +08:00
[$group, $ext, $load_type, $env] = $this->getFileMeta($path);
2022-12-31 20:12:20 +08:00
if (!in_array($ext, $this->file_extensions, true)) {
2022-08-22 16:29:27 +08:00
throw ConfigException::unsupportedFileType($path);
2022-08-19 23:10:43 +08:00
}
// 读取并解析配置
$content = file_get_contents($path);
2022-12-31 20:12:20 +08:00
// TODO: 使用 Loader 替代
// $config = $this->loader->load($path);
2022-08-19 23:10:43 +08:00
$config = [];
switch ($ext) {
case 'php':
$config = require $path;
break;
case 'json':
2022-08-22 16:29:27 +08:00
try {
$config = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw ConfigException::loadConfigFailed($path, $e->getMessage());
}
2022-08-19 23:10:43 +08:00
break;
case 'yaml':
case 'yml':
2022-08-22 17:15:10 +08:00
$yaml_parser_class = 'Symfony\Component\Yaml\Yaml';
if (!class_exists($yaml_parser_class)) {
throw ConfigException::loadConfigFailed($path, 'YAML 解析器未安装');
}
try {
$config = $yaml_parser_class::parse($content);
} catch (\RuntimeException $e) {
throw ConfigException::loadConfigFailed($path, $e->getMessage());
}
2022-08-19 23:10:43 +08:00
break;
case 'toml':
2022-08-22 17:15:10 +08:00
$toml_parser_class = 'Yosymfony\Toml\Toml';
if (!class_exists($toml_parser_class)) {
throw ConfigException::loadConfigFailed($path, 'TOML 解析器未安装');
}
try {
$config = $toml_parser_class::parse($content);
} catch (\RuntimeException $e) {
throw ConfigException::loadConfigFailed($path, $e->getMessage());
}
2022-08-19 23:10:43 +08:00
break;
default:
2022-08-22 16:29:27 +08:00
throw ConfigException::unsupportedFileType($path);
2022-08-19 23:10:43 +08:00
}
// 加入配置
2022-08-20 18:28:22 +08:00
$this->merge($group, $config);
2022-08-27 00:38:55 +08:00
logger()->debug("已载入配置文件:{$path}");
2022-12-31 20:12:20 +08:00
$this->tracer?->addTracesOf($group, $config, $path);
}
private function loadInitConfig(): array
{
return require SOURCE_ROOT_DIR . '/config/config.php';
2022-08-20 18:28:22 +08:00
}
2022-08-19 23:10:43 +08:00
}