Register.php 8.6 KB

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