Files
kiri-core/kiri-engine/Abstracts/Config.php
T

129 lines
2.1 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:50
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2021-05-17 17:30:25 +08:00
2021-08-11 01:04:57 +08:00
namespace Kiri\Abstracts;
2020-08-31 01:27:08 +08:00
2021-08-11 01:04:57 +08:00
use Kiri\Exception\ConfigException;
2020-08-31 01:27:08 +08:00
/**
* Class Config
2021-08-11 01:04:57 +08:00
* @package Kiri\Kiri\Base
2020-08-31 01:27:08 +08:00
*/
class Config extends Component
{
2020-09-03 15:26:11 +08:00
const ERROR_MESSAGE = 'The not find %s in app configs.';
2020-08-31 01:27:08 +08:00
2021-12-27 15:24:29 +08:00
protected static mixed $data = [];
2020-09-03 15:23:11 +08:00
/**
* @return mixed
*/
2021-12-27 15:24:29 +08:00
public static function getData(): mixed
2020-09-03 15:23:11 +08:00
{
2021-12-27 15:24:29 +08:00
return static::$data;
2020-09-03 15:23:11 +08:00
}
2020-08-31 01:27:08 +08:00
2020-09-03 15:33:55 +08:00
/**
* @param $key
* @param $value
* @return mixed
*/
2021-12-27 15:24:29 +08:00
public static function setData($key, $value): mixed
2020-09-03 15:33:55 +08:00
{
2021-12-27 15:24:29 +08:00
return static::$data[$key] = $value;
2020-09-03 15:33:55 +08:00
}
2021-05-17 17:30:25 +08:00
/**
* @param array $configs
*/
public static function sets(array $configs)
{
2021-05-17 17:35:28 +08:00
if (empty($configs)) {
return;
}
2021-12-27 15:24:29 +08:00
static::$data = $configs;
2021-05-17 17:30:25 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $key
* @param bool $try
2021-07-13 16:57:57 +08:00
* @param mixed|null $default
2020-09-01 13:36:57 +08:00
* @return mixed
2021-12-27 15:24:29 +08:00
* @throws ConfigException
2020-08-31 01:27:08 +08:00
*/
2021-07-13 16:57:57 +08:00
public static function get($key, mixed $default = null, bool $try = FALSE): mixed
2020-08-31 01:27:08 +08:00
{
2021-12-27 15:24:29 +08:00
$instance = static::$data;
2020-12-17 14:09:14 +08:00
if (!str_contains($key, '.')) {
2021-07-13 16:57:57 +08:00
return $instance[$key] ?? $default;
2020-09-17 13:56:57 +08:00
}
2020-09-17 14:15:11 +08:00
foreach (explode('.', $key) as $value) {
2020-09-03 15:26:11 +08:00
if (empty($value)) {
continue;
}
2020-09-03 15:33:55 +08:00
if (!isset($instance[$value])) {
if ($try) {
throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key));
}
return $default;
2020-09-03 15:23:11 +08:00
}
2021-05-17 17:30:25 +08:00
if (!is_array($instance[$value])) {
2020-09-17 13:56:57 +08:00
return $instance[$value];
2020-08-31 22:54:41 +08:00
}
2020-09-17 13:56:57 +08:00
$instance = $instance[$value];
2020-08-31 22:54:41 +08:00
}
2020-09-03 15:26:11 +08:00
return empty($instance) ? $default : $instance;
2020-08-31 01:27:08 +08:00
}
/**
* @param $key
* @param $value
* @return mixed
*/
2020-12-17 14:09:14 +08:00
public static function set($key, $value): mixed
2020-08-31 01:27:08 +08:00
{
2022-01-06 19:05:15 +08:00
$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;
2020-08-31 01:27:08 +08:00
}
/**
* @param $key
* @param bool $must_not_null
* @return bool
*/
2021-07-13 16:57:57 +08:00
public static function has($key, bool $must_not_null = false): bool
2020-08-31 01:27:08 +08:00
{
2021-12-27 15:24:29 +08:00
if (!isset(static::$data[$key])) {
2020-08-31 01:27:08 +08:00
return false;
}
2021-12-27 15:24:29 +08:00
$config = static::$data[$key];
2020-08-31 01:27:08 +08:00
if ($must_not_null === false) {
return true;
}
return !empty($config);
}
}