boot.php 9.2 KB

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