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

137 lines
2.4 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
2020-09-04 01:05:33 +08:00
namespace Snowflake\Abstracts;
2020-08-31 01:27:08 +08:00
use Exception;
2021-05-18 10:28:04 +08:00
use Snowflake\Core\ArrayAccess;
2020-08-31 01:27:08 +08:00
use Snowflake\Exception\ConfigException;
2020-09-04 01:09:40 +08:00
use Snowflake\Snowflake;
2020-08-31 01:27:08 +08:00
/**
* Class Config
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\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
2020-09-03 15:33:55 +08:00
protected $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)
{
$config = Snowflake::app()->getConfig();
2021-05-17 17:35:28 +08:00
if (empty($configs)) {
return;
}
2021-05-18 11:15:40 +08:00
if (!empty($config->data)) {
$config->data = ArrayAccess::merge($config->data, $configs);
} else {
$config->data = $configs;
}
2021-05-17 17:30:25 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $key
* @param bool $try
* @param mixed $default
2020-09-01 13:36:57 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
* @throws
*/
2021-04-04 03:01:12 +08:00
public static function get($key, $default = null, $try = FALSE): mixed
2020-08-31 01:27:08 +08:00
{
2021-02-20 13:08:54 +08:00
$instance = Snowflake::app()->getConfig()->getData();
2020-12-17 14:09:14 +08:00
if (!str_contains($key, '.')) {
2020-09-17 13:56:57 +08:00
return isset($instance[$key]) ? $instance[$key] : $default;
}
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-02-20 13:08:54 +08:00
$config = Snowflake::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
*/
2020-12-17 14:12:44 +08:00
public static function has($key, $must_not_null = false): bool
2020-08-31 01:27:08 +08:00
{
2021-02-20 13:08:54 +08:00
$config = Snowflake::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);
}
/**
* @param $name
* @param $value
*/
public function __set($name, $value)
{
$this->data[$name] = $value;
}
}