127 lines
2.2 KiB
PHP
127 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: whwyy
|
|
* Date: 2018/5/24 0024
|
|
* Time: 11:50
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace Kiri\Abstracts;
|
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
|
/**
|
|
* Class Config
|
|
* @package Kiri\Base
|
|
*/
|
|
class Config extends Component
|
|
{
|
|
|
|
const ERROR_MESSAGE = 'The not find %s in app configs.';
|
|
|
|
protected static mixed $data = [];
|
|
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public static function getData(): mixed
|
|
{
|
|
return static::$data;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $key
|
|
* @param $value
|
|
* @return mixed
|
|
*/
|
|
public static function setData($key, $value): mixed
|
|
{
|
|
return static::$data[$key] = $value;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param array $configs
|
|
*/
|
|
public static function sets(array $configs)
|
|
{
|
|
if (empty($configs)) {
|
|
return;
|
|
}
|
|
static::$data = $configs;
|
|
}
|
|
|
|
/**
|
|
* @param $key
|
|
* @param bool $try
|
|
* @param mixed|null $default
|
|
* @return mixed
|
|
* @throws
|
|
*/
|
|
public static function get($key, mixed $default = null, bool $try = FALSE): mixed
|
|
{
|
|
if (!str_contains($key, '.')) {
|
|
return static::$data[$key] ?? $default;
|
|
}
|
|
$array = explode('.', $key);
|
|
$data = static::$data[array_shift($array)] ?? null;
|
|
if (($data == null || !is_array($data)) && count($array) > 0) {
|
|
return $default;
|
|
}
|
|
foreach ($array as $value) {
|
|
$data = $data[$value] ?? null;
|
|
if ($data === null) {
|
|
if ($try) {
|
|
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
|
|
}
|
|
return $default;
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param $key
|
|
* @param $value
|
|
* @return mixed
|
|
*/
|
|
public static function set($key, $value): mixed
|
|
{
|
|
$explode = explode('.', $key);
|
|
$parent = &static::$data;
|
|
foreach ($explode as $item) {
|
|
if (!isset($parent[$item])) {
|
|
$parent[$item] = [];
|
|
}
|
|
$parent = &$parent[$item];
|
|
}
|
|
$parent = $value;
|
|
|
|
unset($parent);
|
|
|
|
return static::$data;
|
|
}
|
|
|
|
/**
|
|
* @param $key
|
|
* @param bool $must_not_null
|
|
* @return bool
|
|
*/
|
|
public static function has($key, bool $must_not_null = false): bool
|
|
{
|
|
if (!isset(static::$data[$key])) {
|
|
return false;
|
|
}
|
|
$config = static::$data[$key];
|
|
if ($must_not_null === false) {
|
|
return true;
|
|
}
|
|
return !empty($config);
|
|
}
|
|
|
|
}
|