boot.php 9.3 KB

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