/ginkgo/core/config.class.php

https://github.com/baigoStudio/baigoSSO · PHP · 427 lines · 278 code · 60 blank · 89 comment · 129 complexity · 66f646c5fd50372c98c2fa53329a6562 MD5 · raw file

  1. <?php
  2. /*-----------------------------------------------------------------
  3. !!!!警告!!!!
  4. 以下为系统文件,请勿修改
  5. -----------------------------------------------------------------*/
  6. namespace ginkgo;
  7. // 不能非法包含或直接执行
  8. defined('IN_GINKGO') or exit('Access denied');
  9. // 配置管理类
  10. class Config {
  11. private static $range = ''; // 作用域
  12. private static $config = array(); // 配置值
  13. private static $init; // 是否初始化标志
  14. private static $count = 1; // 载入配置计数
  15. protected function __construct() {
  16. }
  17. protected function __clone() {
  18. }
  19. // 初始化
  20. public static function init() {
  21. self::loadSys(); // 载入系统配置
  22. self::$init = true; // 标识为已初始化
  23. }
  24. /** 设置, 获取作用域
  25. * range function.
  26. *
  27. * @access public
  28. * @static
  29. * @param string $range (default: '') 作用域
  30. * @return 如果参数为空则返回当前作用域, 否则无返回
  31. */
  32. public static function range($range = '') {
  33. if (Func::isEmpty(self::$init)) {
  34. self::init();
  35. }
  36. if (Func::isEmpty($range)) {
  37. return self::$range;
  38. } else {
  39. self::$range = $range;
  40. }
  41. }
  42. /** 添加配置 (冲突不覆盖)
  43. * add function.
  44. *
  45. * @access public
  46. * @static
  47. * @param string $name 配置名
  48. * @param string $value (default: '') 配置值
  49. * @param string $range (default: '') 作用域
  50. * @return void
  51. */
  52. public static function add($name, $value = '', $range = '') {
  53. $name = (string)$name;
  54. if (Func::isEmpty(self::$init)) {
  55. self::init();
  56. }
  57. $_mix_range = self::rangeProcess($range); // 作用域处理
  58. if (Func::isEmpty($_mix_range)) {
  59. if (!isset(self::$config[$name])) {
  60. self::$config[$name] = $value;
  61. }
  62. } else if (is_array($_mix_range)) {
  63. if (isset($_mix_range[1])) {
  64. if (!isset(self::$config[$_mix_range[0]][$_mix_range[1]][$name])) {
  65. self::$config[$_mix_range[0]][$_mix_range[1]][$name] = $value;
  66. }
  67. } else if (isset($_mix_range[0])) {
  68. if (!isset(self::$config[$_mix_range[0]][$name])) {
  69. self::$config[$_mix_range[0]][$name] = $value;
  70. }
  71. }
  72. } else if (is_string($_mix_range)) {
  73. if (!isset(self::$config[$_mix_range][$name])) {
  74. self::$config[$_mix_range][$name] = $value;
  75. }
  76. }
  77. }
  78. /** 设置配置 (冲突覆盖)
  79. * set function.
  80. *
  81. * @access public
  82. * @static
  83. * @param mixed $name 配置名
  84. * @param string $value (default: '') 配置值
  85. * @param string $range (default: '') 作用域
  86. * @return void
  87. */
  88. public static function set($name, $value = '', $range = '') {
  89. if (Func::isEmpty(self::$init)) {
  90. self::init();
  91. }
  92. $_mix_range = self::rangeProcess($range); // 作用域处理
  93. if (Func::isEmpty($_mix_range)) {
  94. if (is_array($name)) {
  95. self::$config = array_replace_recursive(self::$config, $name);
  96. } else if (is_string($name)) {
  97. if (isset(self::$config[$name]) && is_array(self::$config[$name]) && is_array($value)) {
  98. self::$config[$name] = array_replace_recursive(self::$config[$name], $value);
  99. } else {
  100. self::$config[$name] = $value;
  101. }
  102. }
  103. } else if (is_array($_mix_range)) {
  104. if (is_array($name)) {
  105. if (isset($_mix_range[1])) {
  106. if (isset(self::$config[$_mix_range[0]][$_mix_range[1]]) && is_array(self::$config[$_mix_range[0]][$_mix_range[1]])) {
  107. self::$config[$_mix_range[0]][$_mix_range[1]] = array_replace_recursive(self::$config[$_mix_range[0]][$_mix_range[1]], $name);
  108. } else {
  109. self::$config[$_mix_range[0]][$_mix_range[1]] = $name;
  110. }
  111. } else if (isset($_mix_range[0])) {
  112. if (isset(self::$config[$_mix_range[0]]) && is_array(self::$config[$_mix_range[0]])) {
  113. self::$config[$_mix_range[0]] = array_replace_recursive(self::$config[$_mix_range[0]], $name);
  114. } else {
  115. self::$config[$_mix_range[0]] = $name;
  116. }
  117. }
  118. } else if (is_string($name)) {
  119. if (isset($_mix_range[1])) {
  120. if (isset(self::$config[$_mix_range[0]][$_mix_range[1]][$name]) && is_array(self::$config[$_mix_range[0]][$_mix_range[1]][$name]) && is_array($value)) {
  121. self::$config[$_mix_range[0]][$_mix_range[1]][$name] = array_replace_recursive(self::$config[$_mix_range[0]][$_mix_range[1]][$name], $value);
  122. } else {
  123. self::$config[$_mix_range[0]][$_mix_range[1]][$name] = $value;
  124. }
  125. } else if (isset($_mix_range[0])) {
  126. if (isset(self::$config[$_mix_range[0]][$name]) && is_array(self::$config[$_mix_range[0]][$name]) && is_array($value)) {
  127. self::$config[$_mix_range[0]][$name] = array_replace_recursive(self::$config[$_mix_range[0]][$name], $value);
  128. } else {
  129. self::$config[$_mix_range[0]][$name] = $value;
  130. }
  131. }
  132. }
  133. } else if (is_string($_mix_range)) {
  134. if (is_array($name)) {
  135. if (isset(self::$config[$_mix_range]) && is_array(self::$config[$_mix_range])) {
  136. self::$config[$_mix_range] = array_replace_recursive(self::$config[$_mix_range], $name);
  137. } else {
  138. self::$config[$_mix_range] = $name;
  139. }
  140. } else {
  141. if (isset(self::$config[$_mix_range][$name]) && is_array(self::$config[$_mix_range][$name]) && is_array($value)) {
  142. self::$config[$_mix_range][$name] = array_replace_recursive(self::$config[$_mix_range][$name], $value);
  143. } else {
  144. self::$config[$_mix_range][$name] = $value;
  145. }
  146. }
  147. }
  148. }
  149. /** 读取配置
  150. * get function.
  151. *
  152. * @access public
  153. * @static
  154. * @param string $name (default: '') 配置名
  155. * @param string $range (default: '') 作用域
  156. * @return 配置值
  157. */
  158. public static function get($name = '', $range = '') {
  159. $name = (string)$name;
  160. if (Func::isEmpty(self::$init)) {
  161. self::init();
  162. }
  163. $_mix_range = self::rangeProcess($range);
  164. $_mix_return = '';
  165. if (Func::isEmpty($_mix_range)) {
  166. if (Func::isEmpty($name)) {
  167. $_mix_return = self::$config;
  168. } else {
  169. if (isset(self::$config[$name])) {
  170. $_mix_return = self::$config[$name];
  171. }
  172. }
  173. } else if (is_array($_mix_range)) {
  174. if (Func::isEmpty($name)) {
  175. if (isset($_mix_range[1])) {
  176. if (isset(self::$config[$_mix_range[0]][$_mix_range[1]])) {
  177. $_mix_return = self::$config[$_mix_range[0]][$_mix_range[1]];
  178. }
  179. } else if (isset($_mix_range[0])) {
  180. if (isset(self::$config[$_mix_range[0]])) {
  181. $_mix_return = self::$config[$_mix_range[0]];
  182. }
  183. }
  184. } else {
  185. if (isset($_mix_range[1])) {
  186. if (isset(self::$config[$_mix_range[0]][$_mix_range[1]][$name])) {
  187. $_mix_return = self::$config[$_mix_range[0]][$_mix_range[1]][$name];
  188. }
  189. } else if (isset($_mix_range[0])) {
  190. if (isset(self::$config[$_mix_range[0]][$name])) {
  191. $_mix_return = self::$config[$_mix_range[0]][$name];
  192. }
  193. }
  194. }
  195. } else if (is_string($_mix_range)) {
  196. if (Func::isEmpty($name)) {
  197. if (isset(self::$config[$_mix_range])) {
  198. $_mix_return = self::$config[$_mix_range];
  199. }
  200. } else {
  201. if (isset(self::$config[$_mix_range][$name])) {
  202. $_mix_return = self::$config[$_mix_range][$name];
  203. }
  204. }
  205. }
  206. //print_r(self::$config);
  207. return $_mix_return;
  208. }
  209. /** 删除配置
  210. * delete function.
  211. *
  212. * @access public
  213. * @static
  214. * @param string $name (default: '') 配置名
  215. * @param string $range (default: '') 作用域
  216. * @return void
  217. */
  218. public static function delete($name = '', $range = '') {
  219. $name = (string)$name;
  220. if (Func::isEmpty(self::$init)) {
  221. self::init();
  222. }
  223. $_mix_range = self::rangeProcess($range);
  224. if (Func::isEmpty($_mix_range)) {
  225. if (Func::isEmpty($name)) {
  226. unset(self::$config);
  227. } else {
  228. if (isset(self::$config[$name])) {
  229. unset(self::$config[$name]);
  230. }
  231. }
  232. } else if (is_array($_mix_range)) {
  233. if (Func::isEmpty($name)) {
  234. if (isset($_mix_range[1])) {
  235. if (isset(self::$config[$_mix_range[0]][$_mix_range[1]])) {
  236. unset(self::$config[$_mix_range[0]][$_mix_range[1]]);
  237. }
  238. } else if (isset($_mix_range[0])) {
  239. if (isset(self::$config[$_mix_range[0]])) {
  240. unset(self::$config[$_mix_range[0]]);
  241. }
  242. }
  243. } else {
  244. if (isset($_mix_range[1])) {
  245. if (isset(self::$config[$_mix_range[0]][$_mix_range[1]][$name])) {
  246. unset(self::$config[$_mix_range[0]][$_mix_range[1]][$name]);
  247. }
  248. } else if (isset($_mix_range[0])) {
  249. if (isset(self::$config[$_mix_range[0]][$name])) {
  250. unset(self::$config[$_mix_range[0]][$name]);
  251. }
  252. }
  253. }
  254. } else if (is_string($_mix_range)) {
  255. if (Func::isEmpty($name)) {
  256. if (isset(self::$config[$_mix_range])) {
  257. unset(self::$config[$_mix_range]);
  258. }
  259. } else {
  260. if (isset(self::$config[$_mix_range][$name])) {
  261. unset(self::$config[$_mix_range][$name]);
  262. }
  263. }
  264. }
  265. }
  266. /** 统计配置文件
  267. * count function.
  268. *
  269. * @access public
  270. * @static
  271. * @return 配置文件数
  272. */
  273. public static function count() {
  274. return self::$count;
  275. }
  276. /** 载入配置文件
  277. * load function.
  278. *
  279. * @access public
  280. * @static
  281. * @param string $path 路径
  282. * @param string $name (default: '') 配置名
  283. * @param string $range (default: '') 作用域
  284. * @return 配置文件返回值
  285. */
  286. public static function load($path, $name = '', $range = '') {
  287. if (Func::isEmpty(self::$init)) {
  288. self::init();
  289. }
  290. $_arr_config = array();
  291. if (Func::isFile($path)) {
  292. $_arr_config = Loader::load($path, 'include');
  293. ++self::$count;
  294. }
  295. if (is_array($_arr_config) && !Func::isEmpty($_arr_config)) {
  296. $_arr_config = array_change_key_case($_arr_config);
  297. self::set($name, $_arr_config, $range); // 设置配置变量
  298. }
  299. return $_arr_config;
  300. }
  301. /** 写入配置文件
  302. * write function.
  303. *
  304. * @access public
  305. * @static
  306. * @param string $path 路径
  307. * @param string $value (default: '') 配置内容
  308. * @return 写入字节数
  309. */
  310. public static function write($path, $value = '') {
  311. if (is_array($value)) {
  312. $_str_outPut = '<?php return ' . var_export($value, true) . ';';
  313. } else if (is_numeric($value)) {
  314. $_str_outPut = '<?php return ' . $value . ';';
  315. } else if (is_string($value)) {
  316. $_str_outPut = '<?php return \'' . $value . '\';';
  317. }
  318. return File::instance()->fileWrite($path, $_str_outPut);
  319. }
  320. // 载入系统配置
  321. private static function loadSys() {
  322. $_arr_convention = Loader::load(GK_PATH_FW . 'convention' . GK_EXT); // 配置规范 (默认值)
  323. $_str_pathBase = GK_APP_CONFIG . 'config' . GK_EXT_INC; // 全局配置
  324. $_str_pathDbconfig = GK_APP_CONFIG . 'dbconfig' . GK_EXT_INC; // 数据库配置
  325. $_arr_config = array();
  326. if (Func::isFile($_str_pathBase)) {
  327. $_arr_config = Loader::load($_str_pathBase);
  328. ++self::$count;
  329. }
  330. if (Func::isFile($_str_pathDbconfig)) {
  331. $_arr_config['dbconfig'] = Loader::load($_str_pathDbconfig);
  332. ++self::$count;
  333. }
  334. if (!Func::isEmpty($_arr_convention)) {
  335. $_arr_convention = array_change_key_case($_arr_convention);
  336. }
  337. if (!Func::isEmpty($_arr_config)) {
  338. $_arr_config = array_change_key_case($_arr_config);
  339. }
  340. self::$config = array_replace_recursive(self::$config, $_arr_convention, $_arr_config);
  341. }
  342. /** 作用域处理
  343. * rangeProcess function.
  344. *
  345. * @access private
  346. * @static
  347. * @param string $range (default: '') 作用域
  348. * @return 作用域数组
  349. */
  350. private static function rangeProcess($range = '') {
  351. if (Func::isEmpty($range)) {
  352. $_str_range = self::$range;
  353. } else {
  354. $_str_range = $range;
  355. }
  356. if (!is_string($_str_range)) {
  357. $_str_range = '';
  358. }
  359. $_mix_return = '';
  360. if (strpos($_str_range, '.')) {
  361. $_mix_return = explode('.', $_str_range);
  362. } else {
  363. $_mix_return = $_str_range;
  364. }
  365. return $_mix_return;
  366. }
  367. }