boot.php 11 KB

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