Files
zhamao-framework/src/ZM/Config/ConfigTracer.php
sunxyw cf9db4b0d4 add config trace (#159)
* add config trace

* fix config tracer bug
2022-08-28 02:19:26 +08:00

59 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}