boot.php 9.3 KB

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