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
|
|
|
|
|
|
|
|
use Exception;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Kiri\Kiri;
|
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-08-09 02:46:26 +08:00
|
|
|
protected mixed $data = [];
|
2020-09-03 15:23:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getData(): mixed
|
2020-09-03 15:23:11 +08:00
|
|
|
{
|
|
|
|
|
return $this->data;
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-03 15:33:55 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $key
|
|
|
|
|
* @param $value
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function setData($key, $value): mixed
|
2020-09-03 15:33:55 +08:00
|
|
|
{
|
|
|
|
|
return $this->data[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 17:30:25 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $configs
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static function sets(array $configs)
|
|
|
|
|
{
|
2021-08-11 01:04:57 +08:00
|
|
|
$config = Kiri::app()->getConfig();
|
2021-05-17 17:35:28 +08:00
|
|
|
if (empty($configs)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-03 13:12:21 +08:00
|
|
|
$config->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
|
2020-08-31 01:27:08 +08:00
|
|
|
* @throws
|
|
|
|
|
*/
|
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-08-11 01:04:57 +08:00
|
|
|
$instance = Kiri::app()->getConfig()->getData();
|
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
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function set($key, $value): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-08-11 01:04:57 +08:00
|
|
|
$config = Kiri::app()->getConfig();
|
2020-09-03 15:33:55 +08:00
|
|
|
return $config->setData($key, $value);
|
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
|
|
|
* @throws Exception
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
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-08-11 01:04:57 +08:00
|
|
|
$config = Kiri::app()->getConfig();
|
2020-08-31 01:27:08 +08:00
|
|
|
if (!isset($config->data[$key])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$config = $config->data[$key];
|
|
|
|
|
if ($must_not_null === false) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return !empty($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|