Register.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * 将键值保存到\Qii\Config\Register::$config中
  4. * 用于保存系统相关设置,保存项目相关的配置,请注意不出现key相互覆盖的情况,可以考虑加前缀
  5. *
  6. * @author Jinhui Zhu
  7. * @version 1.3
  8. *
  9. * Usage:
  10. * 保存值:
  11. * \Qii\Config\Register::set($key, $val);
  12. * 读取值:
  13. * \Qii\Config\Register::get($key, $default);
  14. * \Qii\Config\Register::$key($default);
  15. *
  16. *
  17. */
  18. namespace Qii\Config;
  19. use \Qii\Autoloader\Psr4;
  20. class Register
  21. {
  22. const VERSION = '1.3';
  23. /**
  24. * 存储键值的变量
  25. *
  26. * @var Array
  27. */
  28. public static $_cache;
  29. /**
  30. * 是否将内容写入到文件
  31. *
  32. * @var bool $isWrite 是否写入到文件
  33. */
  34. public static $isWrite = true;
  35. /**
  36. * 设置键值
  37. *
  38. * @param String $key
  39. * @param String $val
  40. * @param Bool overwrite 是否覆盖之前保存的值,如果之前保存了值,要想再保存需要额外设置它为true,否则不让保存
  41. */
  42. public static function set($key, $val, $overwrite = true)
  43. {
  44. if (!Register::isValid($key, $overwrite)) \Qii\Application::_e('Overwrite', $key, __LINE__);
  45. Register::$_cache[$key] = $val;
  46. }
  47. /**
  48. * 设置键
  49. *
  50. * @param String $index
  51. * @param String $key
  52. * @param String $val
  53. */
  54. public static function add($index, $key, $val)
  55. {
  56. $added = Register::get(Consts::APP_LOADED_FILE, array());
  57. $added[$index][$key] = $val;
  58. Register::$_cache[$index] = $added;
  59. }
  60. /**
  61. * 移除某一个key
  62. * @param String $key
  63. */
  64. public static function remove($key)
  65. {
  66. if (!isset(Register::$_cache[$key])) return;
  67. unset(Register::$_cache[$key]);
  68. }
  69. /**
  70. * 获取保存的值
  71. *
  72. * @param String $key
  73. * @param String $index
  74. * @param Mix $default
  75. * @return Mix
  76. */
  77. public static function get($key, $default = null)
  78. {
  79. if (!$key) throw new \Qii\Exceptions\Variable(\Qii::i(1003), __LINE__);
  80. //优先调用存在的get方法
  81. $method = 'get' . $key;
  82. if (method_exists('\Qii\Config\Register', $method)) return Register::$method();
  83. if (isset(Register::$_cache[$key])) {
  84. return Register::$_cache[$key];
  85. }
  86. return $default;
  87. }
  88. /**
  89. * 通过\Qii\Config\Register::$key($defaultVal)来获取内容
  90. *
  91. * @param String $method
  92. * @param Array $argvs
  93. * @return Mix
  94. */
  95. public static function __callStatic($method, $argvs)
  96. {
  97. $default = array_shift($argvs);
  98. return Register::get($method, $default);
  99. }
  100. /**
  101. * 整理数组,将0.key 最后会整理到 [0][key]中
  102. * @param Array $array
  103. * @return multitype:
  104. */
  105. public static function feval($array)
  106. {
  107. $data = array();
  108. foreach ($array AS $key => $value) {
  109. $keys = explode('.', $key);
  110. if (is_array($value)) {
  111. $string = "\$data['" . join("']['", $keys) . "']=" . var_export(Register::feval($value), true) . ";";
  112. } else {
  113. $string = "\$data['" . join("']['", $keys) . "']='" . $value . "';";
  114. }
  115. eval($string);
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 读取ini配置文件
  121. *
  122. * @param String $fileName
  123. * @return Array
  124. */
  125. public static function ini($fileName)
  126. {
  127. if (!$fileName) throw new \Qii\Exceptions\Variable(\Qii::i(1408), __LINE__);
  128. $ini = parse_ini_file($fileName, true);
  129. if (!$ini) throw new \Qii\Exceptions\InvalidFormat($fileName, __LINE__);
  130. $config = array();
  131. foreach ($ini AS $namespace => $properties) {
  132. $properties = Register::feval($properties);
  133. $name = $namespace;
  134. $namespaces = array();
  135. if (stristr($namespace, ':')) {
  136. $namespaces = explode(':', $namespace);
  137. $name = array_shift($namespaces);
  138. }
  139. $name = trim($name);
  140. if (count($namespaces) > 0) {
  141. foreach ($namespaces AS $space) {
  142. //如果space以“.”开头,与key的方式放在当前key下边如[dev:.space],那么生成后的数据就是这样的[dev][space]否则是[space+dev]
  143. if (substr($space, 0, 1) == '.') {
  144. $space = substr($space, 1);
  145. if (isset($config[$space])) $config[$name][$space] = array_merge($config[$space], $properties);
  146. continue;
  147. }
  148. if(stristr($space, '.')) {
  149. if (isset($config[$space])) $config[$name] = $config[$space];
  150. }else{
  151. $config[$name] = $config[$name] ?? [];
  152. if (isset($config[$space])) $config[$name] = array_merge($config[$space], $config[$name], $properties);
  153. }
  154. }
  155. }else{
  156. $config[$name] = $properties;
  157. }
  158. }
  159. return $config;
  160. }
  161. /**
  162. * 返回cache的名称
  163. * @param String $iniFile
  164. * @return String
  165. */
  166. public static function getCacheName($iniFile)
  167. {
  168. $cacheName = basename($iniFile);
  169. $environs = Register::get(Consts::APP_ENVIRONS, array());
  170. if (isset($environs[$cacheName])) {
  171. $environ = $environs[$cacheName];
  172. $cacheName = $environ . '.' . $cacheName;
  173. }
  174. return $cacheName;
  175. }
  176. /**
  177. * 覆盖/添加ini文件的key对应的值
  178. * @param String $iniFile ini文件名
  179. * @param String $key 需覆盖的key
  180. * @param String $val key对应的值
  181. */
  182. public static function rewriteConfig($iniFile, $key, $val)
  183. {
  184. $config = Register::getIniConfigure($iniFile);
  185. $cacheName = Register::getCacheName($iniFile);
  186. $config[$key] = $val;
  187. Register::set($cacheName, $config);
  188. }
  189. /**
  190. * 删除ini配置文件中对应的key
  191. * @param string $iniFile ini配置我呢见
  192. * @param string $key 陪删除的key
  193. */
  194. public static function removeAppConfigure($iniFile, $key)
  195. {
  196. $config = Register::getIniConfigure($iniFile);
  197. $cacheName = Register::getCacheName($iniFile);
  198. unset($config[$key]);
  199. Register::set($cacheName, $config);
  200. }
  201. /**
  202. * 合并ini文件生成的数组
  203. * @param String $iniFile ini文件名
  204. * @param Array $array
  205. */
  206. public static function mergeAppConfigure($iniFile, $array)
  207. {
  208. if (!is_array($array)) return;
  209. $config = Register::getIniConfigure($iniFile);
  210. $environs = Register::get(Consts::APP_ENVIRONS, array());
  211. $cacheName = basename($iniFile);
  212. if (isset($environs[$cacheName])) {
  213. $environ = $environs[$cacheName];
  214. $cacheName = $environ . '.' . $cacheName;
  215. }
  216. $config = array_merge($config, $array);
  217. Register::set($cacheName, $config);
  218. }
  219. /**
  220. * 获取配置ini文件
  221. * @param String $iniFile
  222. * @param String $environ
  223. * @return boolean
  224. */
  225. public static function setConfig($iniFile, $environ = 'product')
  226. {
  227. $cacheName = basename($iniFile);
  228. $environs = Register::get(Consts::APP_ENVIRONS, array());
  229. $environs[$cacheName] = $environ;
  230. Register::set(Consts::APP_ENVIRONS, $environs);
  231. $cacheName = $environ . '.' . $cacheName;
  232. if (!is_file($iniFile)) return false;
  233. $cacheFile = Psr4::getInstance()->getFileByPrefix(Register::get(Consts::APP_CACHE_PATH) . DS . $cacheName . '.php');
  234. if (self::$isWrite && Register::get(Consts::APP_CACHE_PATH)) {
  235. if (is_file($cacheFile)) {
  236. if (filemtime($cacheFile) == filemtime($iniFile)) {
  237. $common = include($cacheFile);
  238. Register::set($cacheName, $common);
  239. return $common;
  240. }
  241. }
  242. }
  243. $array = Register::ini($iniFile);
  244. if (!$array) return false;
  245. $common = $array['common'];
  246. if (isset($array[$environ])) {
  247. $environConfig = $array[$environ];
  248. $common = self::array_merge_recursive_distinct($common, $environConfig);
  249. }
  250. //如果文件不可写,touch就有问题,就不写缓存文件
  251. if (self::$isWrite && is_writeable($iniFile)) {
  252. file_put_contents($cacheFile, "<?php \n return " . var_export($common, true) . "\n?>", LOCK_EX);
  253. touch($iniFile);
  254. }
  255. Register::set($cacheName, $common);
  256. return $common;
  257. }
  258. /**
  259. * 递归的合并两个数组 array_merge_recursive会把字段转化成数组
  260. *
  261. * @param array $array1
  262. * @param array $array2
  263. * @return array
  264. */
  265. public static function array_merge_recursive_distinct ( array &$array1, array &$array2 )
  266. {
  267. $merged = $array1;
  268. foreach ( $array2 as $key => &$value )
  269. {
  270. if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
  271. {
  272. $merged [$key] = self::array_merge_recursive_distinct ( $merged [$key], $value );
  273. }
  274. else
  275. {
  276. $merged [$key] = $value;
  277. }
  278. }
  279. return $merged;
  280. }
  281. /**
  282. * 设置网站的配置文件
  283. *
  284. * @param String $iniFile 配置我呢见
  285. * @param string $environ 环境变量
  286. * @return Object self
  287. */
  288. public static function setAppConfigure($iniFile, $environ = 'product')
  289. {
  290. return Register::setConfig($iniFile, $environ);
  291. }
  292. /**
  293. * 获取配置ini文件相关信息
  294. * @param String $fileName 文件名
  295. * @return Ambigous <Mix, multitype:>
  296. */
  297. public static function getIniConfigure($fileName)
  298. {
  299. $cacheName = basename($fileName);
  300. $environs = Register::get(Consts::APP_ENVIRONS, array());
  301. if (isset($environs[$cacheName])) {
  302. $cacheName = $environs[$cacheName] . '.' . $cacheName;
  303. }
  304. return Register::get($cacheName);
  305. }
  306. /**
  307. * 获取网站的配置信息
  308. *
  309. * @return Array
  310. */
  311. public static function getAppConfigure($iniFile = Consts::APP_INI, $key = NULL)
  312. {
  313. $appConfigure = Register::getIniConfigure($iniFile);
  314. if ($key == null) return $appConfigure;
  315. return isset($appConfigure[$key]) ? $appConfigure[$key] : NULL;
  316. }
  317. /**
  318. * 验证是否之前已经保存过这个属性,如果保存过,不覆盖属性对应的值就不保存
  319. *
  320. * @param String $key
  321. * @param Bool $overwrite
  322. * @return Bool
  323. */
  324. public static function isValid($key, $overwrite = false)
  325. {
  326. if ($overwrite) return true;
  327. if (isset(Register::$_cache[$key])) return false;
  328. return true;
  329. }
  330. /**
  331. * 获取当前系统环境
  332. * @return Ambigous <Mix, multitype:>
  333. */
  334. public static function getAppEnviron()
  335. {
  336. return isset(Register::$_cache[Consts::APP_ENVIRON]) ?
  337. Register::$_cache[Consts::APP_ENVIRON]
  338. : Consts::APP_DEFAULT_ENVIRON;
  339. }
  340. public function __call($method, $argvs)
  341. {
  342. }
  343. }