add config trace (#159)

* add config trace

* fix config tracer bug
This commit is contained in:
sunxyw
2022-08-28 02:19:26 +08:00
committed by GitHub
parent f341c8585e
commit cf9db4b0d4
2 changed files with 130 additions and 41 deletions

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace ZM\Config;
class ConfigTracer
{
/**
* @var array 配置加载跟踪
*/
private array $traces = [];
/**
* 添加配置跟踪路径
*
* @param string $group 配置组
* @param array $traces 配置项
* @param string $source 配置源
*/
public function addTracesOf(string $group, array $traces, string $source): void
{
$this->traces = array_merge($this->traces, $this->flatten($group, $traces, $source));
}
/**
* 获取配置项的来源
*
* @param string $key 配置项
* @return null|string 来源,如果没有找到,返回 null
*/
public function getTraceOf(string $key): ?string
{
return $this->traces[$key] ?? null;
}
/**
* 扁平化配置
*
* @param string $prefix 前缀
* @param array $array 数组
* @param string $source 来源
* @return array 扁平化后的数组,键名为 $prefix . $key键值为 $source
*/
private function flatten(string $prefix, array $array, string $source): array
{
$result = [];
$prefix = $prefix ? $prefix . '.' : '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$result += $this->flatten($prefix . $key, $value, $source);
} else {
$result[$prefix . $key] = $source;
}
}
return $result;
}
}