boot.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 $app = array();
  8. public static function run()
  9. {
  10. spl_autoload_register(array('Dever', 'autoload'));
  11. $route = Dever\Route::get();
  12. Dever\Debug::init();
  13. Dever\Project::register();
  14. if (isset(self::config('setting')['cache'])) {
  15. $index = DEVER_APP_NAME . DIRECTORY_SEPARATOR . $route['l'];
  16. if (isset(self::config('setting')['cache'][$index])) {
  17. $expire = self::config('setting')['cache'][$index];
  18. if (isset($route['shell'])) {
  19. unset($route['shell']);
  20. }
  21. $key = md5(DEVER_APP_NAME . http_build_query($route));
  22. if ($result = self::cache($key)) {
  23. return Dever\Output::success($result);
  24. }
  25. }
  26. }
  27. self::$app[] = DEVER_APP_NAME;
  28. if ($route['l']) {
  29. $result = Dever\Output::success(self::load($route['l'], DEVER_APP_NAME, 'api'));
  30. } else {
  31. $result = Dever\Output::success('ok');
  32. }
  33. if (isset($expire)) {
  34. self::cache($key, $result, $expire);
  35. }
  36. }
  37. public static function autoload($class)
  38. {
  39. if (strpos($class, 'Dever') === 0) {
  40. include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
  41. } else {
  42. Dever\Library::autoload($class);
  43. }
  44. }
  45. public static function call($class, $param = array(), $path = '')
  46. {
  47. if (strpos($class, '.')) {
  48. list($class, $method) = explode('.', $class);
  49. $call = 'load';
  50. if (!$path) $path = 'lib';
  51. } elseif (strpos($class, '-')) {
  52. list($class, $method) = explode('-', $class);
  53. $call = 'db';
  54. if (!$path) $path = 'default';
  55. }
  56. return self::$call($class, '', $path)->$method(...$param);
  57. }
  58. public static function load($class, $app = '', $path = 'lib')
  59. {
  60. if (strpos($class, '/') && !$app) {
  61. list($app, $class) = explode('/', $class);
  62. }
  63. if ($app) {
  64. array_unshift(self::$app, $app);
  65. } else {
  66. $app = self::$app[0];
  67. }
  68. $index = $app . $path . $class;
  69. if (empty(self::$data[$index])) {
  70. self::$data[$index] = new Dever\Import($class, $app, $path);
  71. }
  72. return self::$data[$index]->loadDevelop();
  73. }
  74. public static function db($table, $app = '', $store = 'default', $partition = false, $path = 'table')
  75. {
  76. if (strpos($table, '/') && !$app) {
  77. list($app, $table) = explode('/', $table);
  78. }
  79. if ($app) {
  80. array_unshift(self::$app, $app);
  81. } else {
  82. $app = self::$app[0];
  83. }
  84. $index = $app . $path . $table;
  85. if (empty(self::$data[$index])) {
  86. self::$data[$index] = new Dever\Model($table, $app, $store, $partition, $path);
  87. }
  88. return self::$data[$index];
  89. }
  90. public static function reset()
  91. {
  92. if (count(self::$app) > 1) {
  93. array_shift(self::$app);
  94. }
  95. }
  96. public static function config()
  97. {
  98. return Dever\Config::get(...func_get_args());
  99. }
  100. public static function apply()
  101. {
  102. return Dever\Library::apply(...func_get_args());
  103. }
  104. public static function success()
  105. {
  106. return Dever\Output::success(...func_get_args());
  107. }
  108. public static function error()
  109. {
  110. return Dever\Output::error(...func_get_args());
  111. }
  112. public static function out()
  113. {
  114. return Dever\Output::out(...func_get_args());
  115. }
  116. public static function input()
  117. {
  118. return Dever\Route::input(...func_get_args());
  119. }
  120. public static function url()
  121. {
  122. return Dever\Route::url(...func_get_args());
  123. }
  124. public static function host()
  125. {
  126. return Dever\Route::host(...func_get_args());
  127. }
  128. public static function project()
  129. {
  130. return Dever\Project::load(...func_get_args());
  131. }
  132. public static function log()
  133. {
  134. return Dever\Log::add(...func_get_args());
  135. }
  136. public static function debug()
  137. {
  138. return Dever\Debug::add(...func_get_args());
  139. }
  140. public static function session()
  141. {
  142. return Dever\Session::oper(...func_get_args());
  143. }
  144. public static function view()
  145. {
  146. return Dever\View::show(...func_get_args());
  147. }
  148. public static function file()
  149. {
  150. return Dever\File::get(...func_get_args());
  151. }
  152. public static function data()
  153. {
  154. return Dever\File::data();
  155. }
  156. public static function page()
  157. {
  158. return Dever\Paginator::get(...func_get_args());
  159. }
  160. public static function rule()
  161. {
  162. return Dever\Helper\Rule::get(...func_get_args());
  163. }
  164. public static function curl()
  165. {
  166. $curl = new Dever\Helper\Curl();
  167. return $curl->load(...func_get_args());
  168. }
  169. public static function cache($key, $value = false)
  170. {
  171. if (isset(self::config('setting')['redis'])) {
  172. if ($value) {
  173. if ($value == 'delete') {
  174. return \Dever\Helper\Redis::delete($key);
  175. }
  176. return \Dever\Helper\Redis::set($key, self::json_encode($value));
  177. } else {
  178. return self::json_decode(\Dever\Helper\Redis::get($key));
  179. }
  180. }
  181. return false;
  182. }
  183. public static function shell($value)
  184. {
  185. return Dever::check(Dever\Route::input('shell'), $value);
  186. }
  187. public static function store($store = 'default', $partition = false)
  188. {
  189. $setting = Dever\Config::get('setting')['database'][$store];
  190. $class = 'Dever\\Store\\' . $setting['type'];
  191. return $class::getInstance($store, $setting, $partition);
  192. }
  193. public static function in_array($array, $value, $key = 'id', $show = 'name')
  194. {
  195. $column = array_column($array, $key);
  196. if ($column) {
  197. $index = array_search($value, $column);
  198. if ($index >= 0) {
  199. return $array[$index][$show];
  200. }
  201. }
  202. return false;
  203. }
  204. public static function isset($input, $value = false)
  205. {
  206. if (isset($input[$value])) {
  207. if (is_string($input[$value]) && !strlen($input[$value])) {
  208. return false;
  209. }
  210. return $input[$value];
  211. }
  212. return false;
  213. }
  214. public static function check($var, $find)
  215. {
  216. if (is_array($var)) {
  217. $var = implode(',', $var);
  218. }
  219. $var = ',' . $var . ',';
  220. $find = ',' . $find . ',';
  221. return strpos($var, $find) !== false;
  222. }
  223. public static function json_encode($value)
  224. {
  225. $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
  226. if (strpos($value, '&lt;')) {
  227. $value = Dever\Helper\Secure::xss($value);
  228. }
  229. return $value;
  230. }
  231. public static function json_decode($value)
  232. {
  233. return json_decode($value, true);
  234. }
  235. public static function array_order($array, $key, $sort)
  236. {
  237. $reorder = array_column($array, $key);
  238. array_multisort($reorder, $sort, $array);
  239. return $array;
  240. }
  241. public static function number($number, $num = 2, $type = 1)
  242. {
  243. if ($type == 2) {
  244. return number_format($number, $num);
  245. }
  246. if ($type == 3) {
  247. return round($number, $num);
  248. }
  249. return sprintf("%.".$num."f", $number);
  250. }
  251. public static function uuid()
  252. {
  253. mt_srand((double)microtime() * 10000);
  254. $charid = strtoupper(self::id());
  255. $hyphen = chr(45);
  256. 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);
  257. }
  258. public static function id()
  259. {
  260. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  261. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  262. }
  263. public static function is_file($file)
  264. {
  265. if (strtolower(substr($file, 0, 4)) == 'http') {
  266. $header = get_headers($file, true);
  267. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  268. } else {
  269. return is_file($file);
  270. }
  271. }
  272. }