boot.php 8.9 KB

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