boot.php 10 KB

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