123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace crmeb\services;
- use app\admin\model\system\SystemConfig;
- class SystemConfigService
- {
- protected static $configList = null;
- const CACHE_SYSTEM = 'system_config';
-
- protected static function init()
- {
- $confingFun = function () {
- return self::getAll();
- };
- try {
- self::$configList = CacheService::get(self::CACHE_SYSTEM, $confingFun);
- } catch (\Throwable $e) {
- try {
- self::$configList = $confingFun();
- } catch (\Exception $e) {
- self::$configList = [];
- }
- }
- }
-
- public static function config($key)
- {
- self::init();
- if (self::$configList === null) self::$configList = self::getAll();
- return self::$configList[$key] ?? null;
- }
-
- public static function get($key, $default = '', bool $isCaChe = false)
- {
- if ($isCaChe) {
- try {
- return SystemConfig::getConfigValue($key);
- } catch (\Throwable $e) {
- return $default;
- }
- }
- self::init();
- return self::$configList[$key] ?? $default;
- }
-
- public static function more(array $keys, bool $isCaChe = false)
- {
- self::init();
- $callable = function () use ($keys) {
- try {
- $list = SystemConfig::getMore($keys);
- return self::getDefaultValue($keys, $list);
- } catch (\Exception $e) {
- return self::getDefaultValue($keys);
- }
- };
- if ($isCaChe)
- return $callable();
- try {
- return self::getDefaultValue($keys, self::$configList);
- } catch (\Throwable $e) {
- return $callable();
- }
- }
-
- public static function getDefaultValue(array $keys, array $configList = [])
- {
- $value = [];
- foreach ($keys as $val) {
- if (is_array($val)) {
- $k = $val[0] ?? '';
- $v = $val[1] ?? '';
- } else {
- $k = $val;
- $v = '';
- }
- $value[$k] = $configList[$k] ?? $v;
- }
- return $value;
- }
-
- public static function getAll()
- {
- return SystemConfig::getAllConfig() ?: [];
- }
- }
|