boot.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');date_default_timezone_set("PRC");define('DEVER_TIME', $_SERVER['REQUEST_TIME']);define('DEVER_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  3. Dever::run();
  4. class Dever
  5. {
  6. public static $data = array();
  7. public static function run()
  8. {
  9. spl_autoload_register(array('Dever', 'loadClass'));
  10. $route = Dever\Route::get();
  11. Dever\Debug::init();
  12. Dever\Project::register();
  13. if (isset(self::config('setting')['cache'])) {
  14. $index = DEVER_APP_NAME . DIRECTORY_SEPARATOR . $route['l'];
  15. if (isset(self::config('setting')['cache'][$index])) {
  16. $expire = self::config('setting')['cache'][$index];
  17. if (isset($route['shell'])) {
  18. unset($route['shell']);
  19. }
  20. $key = md5(DEVER_APP_NAME . http_build_query($route));
  21. if ($result = self::cache($key)) {
  22. return Dever\Output::success($result);
  23. }
  24. }
  25. }
  26. $result = Dever\Output::success(self::load($route['l'], DEVER_APP_NAME, 'api'));
  27. if (isset($expire)) {
  28. self::cache($key, $result, $expire);
  29. }
  30. }
  31. public static function loadClass($class)
  32. {
  33. include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
  34. }
  35. public static function load($class, $app = '', $path = 'lib')
  36. {
  37. if (!$app) {
  38. $app = DEVER_APP_NAME;
  39. }
  40. $index = $app . $path . $class;
  41. if (empty(self::$data[$index])) {
  42. self::$data[$index] = new Dever\Import($class, $app, $path);
  43. }
  44. return self::$data[$index]->loadDevelop();
  45. }
  46. public static function db($table, $app = '', $store = 'default', $partition = false, $path = 'table')
  47. {
  48. if (!$app) {
  49. $app = DEVER_APP_NAME;
  50. }
  51. $index = $app . $path . $table;
  52. if (empty(self::$data[$index])) {
  53. self::$data[$index] = new Dever\Model($table, $app, $store, $partition, $path);
  54. }
  55. return self::$data[$index];
  56. }
  57. public static function config()
  58. {
  59. return Dever\Config::get(...func_get_args());
  60. }
  61. public static function apply()
  62. {
  63. return Library::get()->apply(...func_get_args());
  64. }
  65. public static function success()
  66. {
  67. return Dever\Output::success(...func_get_args());
  68. }
  69. public static function error()
  70. {
  71. return Dever\Output::error(...func_get_args());
  72. }
  73. public static function out()
  74. {
  75. return Dever\Output::out(...func_get_args());
  76. }
  77. public static function input()
  78. {
  79. return Dever\Route::input(...func_get_args());
  80. }
  81. public static function url()
  82. {
  83. return Dever\Route::url(...func_get_args());
  84. }
  85. public static function project()
  86. {
  87. return Dever\Project::load(...func_get_args());
  88. }
  89. public static function log()
  90. {
  91. return Dever\Log::add(...func_get_args());
  92. }
  93. public static function debug()
  94. {
  95. return Dever\Debug::add(...func_get_args());
  96. }
  97. public static function session()
  98. {
  99. return Dever\Session::oper(...func_get_args());
  100. }
  101. public static function view()
  102. {
  103. return Dever\View::show(...func_get_args());
  104. }
  105. public static function path()
  106. {
  107. return Dever\Path::get(...func_get_args());
  108. }
  109. public static function curl()
  110. {
  111. $curl = new Dever\Helper\Curl();
  112. return $curl->log(true)->load(...func_get_args());
  113. }
  114. public static function cache($key, $value = false)
  115. {
  116. if (isset(self::config('setting')['redis'])) {
  117. if ($value) {
  118. if ($value == 'delete') {
  119. return \Dever\Helper\Redis::delete($key);
  120. }
  121. return \Dever\Helper\Redis::set($key, self::json_encode($value));
  122. } else {
  123. return self::json_decode(\Dever\Helper\Redis::get($key));
  124. }
  125. }
  126. return false;
  127. }
  128. public static function shell($value)
  129. {
  130. return Dever::check(Dever\Route::input('shell'), $value);
  131. }
  132. public static function store($store = 'default', $partition = false)
  133. {
  134. $setting = Dever\Config::get('setting')['database'][$store];
  135. $class = 'Dever\\Store\\' . $setting['type'];
  136. return $class::getInstance($store, $setting, $partition);
  137. }
  138. public static function check($var, $find)
  139. {
  140. if (is_array($var)) {
  141. $var = implode(',', $var);
  142. }
  143. $var = ',' . $var . ',';
  144. $find = ',' . $find . ',';
  145. return strpos($var, $find) !== false;
  146. }
  147. public static function json_encode($value)
  148. {
  149. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  150. return $value;
  151. }
  152. public static function json_decode($value)
  153. {
  154. return json_decode($value, true);
  155. }
  156. public static function array_order($array, $key, $sort)
  157. {
  158. $reorder = array_column($array, $key);
  159. array_multisort($reorder, $sort, $array);
  160. return $array;
  161. }
  162. public static function number($number, $num = 2, $type = 1)
  163. {
  164. if ($type == 2) {
  165. return number_format($number, $num);
  166. }
  167. if ($type == 3) {
  168. return round($number, $num);
  169. }
  170. return sprintf("%.".$num."f", $number);
  171. }
  172. public static function uuid()
  173. {
  174. mt_srand((double)microtime() * 10000);
  175. $charid = strtoupper(self::id());
  176. $hyphen = chr(45);
  177. return chr(123).substr($charid, 0, 8).$hyphen.substr($charid, 8, 4).$hyphen.substr($charid,12, 4).$hyphen.substr($charid,16, 4).$hyphen.substr($charid,20,12).chr(125);
  178. }
  179. public static function id()
  180. {
  181. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  182. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  183. }
  184. }