boot.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 = array();
  11. public static $commit = true;
  12. public static function server()
  13. {
  14. spl_autoload_register(array('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(array('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 = array(), $path = '')
  69. {
  70. if (!is_array($param)) $param = array($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 = array(0 => array('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. public static function config()
  132. {
  133. return Dever\Config::get(...func_get_args());
  134. }
  135. public static function apply()
  136. {
  137. return Dever\Library::apply(...func_get_args());
  138. }
  139. public static function success()
  140. {
  141. return Dever\Output::success(...func_get_args());
  142. }
  143. public static function error()
  144. {
  145. return Dever\Output::error(...func_get_args());
  146. }
  147. public static function out()
  148. {
  149. return Dever\Output::out(...func_get_args());
  150. }
  151. public static function input()
  152. {
  153. return Dever\Route::input(...func_get_args());
  154. }
  155. public static function url()
  156. {
  157. return Dever\Route::url(...func_get_args());
  158. }
  159. public static function host()
  160. {
  161. return Dever\Route::host(...func_get_args());
  162. }
  163. public static function project()
  164. {
  165. return Dever\Project::load(...func_get_args());
  166. }
  167. public static function log()
  168. {
  169. return Dever\Log::add(...func_get_args());
  170. }
  171. public static function debug()
  172. {
  173. return Dever\Debug::add(...func_get_args());
  174. }
  175. public static function session()
  176. {
  177. return Dever\Session::oper(...func_get_args());
  178. }
  179. public static function view()
  180. {
  181. return Dever\View::show(...func_get_args());
  182. }
  183. public static function file()
  184. {
  185. return Dever\File::get(...func_get_args());
  186. }
  187. public static function data()
  188. {
  189. return Dever\File::data();
  190. }
  191. public static function page()
  192. {
  193. return Dever\Paginator::get(...func_get_args());
  194. }
  195. public static function rule()
  196. {
  197. return Dever\Helper\Rule::get(...func_get_args());
  198. }
  199. public static function curl()
  200. {
  201. $curl = new Dever\Helper\Curl();
  202. return $curl->load(...func_get_args());
  203. }
  204. public static function cache($key, $value = false)
  205. {
  206. if (isset(self::config('setting')['redis'])) {
  207. if ($value) {
  208. if ($value == 'delete') {
  209. return \Dever\Helper\Redis::delete($key);
  210. }
  211. return \Dever\Helper\Redis::set($key, self::json_encode($value));
  212. } else {
  213. return self::json_decode(\Dever\Helper\Redis::get($key));
  214. }
  215. }
  216. return false;
  217. }
  218. public static function shell($value)
  219. {
  220. return Dever::check(Dever\Route::input('shell'), $value);
  221. }
  222. public static function store($store = 'default', $partition = false)
  223. {
  224. $setting = Dever\Config::get('setting')['database'][$store];
  225. $class = 'Dever\\Store\\' . $setting['type'];
  226. return $class::getInstance($store, $setting, $partition);
  227. }
  228. public static function in_array($array, $value, $key = 'id', $show = 'name')
  229. {
  230. $column = array_column($array, $key);
  231. if ($column) {
  232. $index = array_search($value, $column);
  233. if ($index >= 0) {
  234. return $array[$index][$show];
  235. }
  236. }
  237. return false;
  238. }
  239. public static function issets($input, $value = false)
  240. {
  241. if (isset($input[$value])) {
  242. if (is_string($input[$value]) && !strlen($input[$value])) {
  243. return false;
  244. }
  245. return $input[$value];
  246. }
  247. return false;
  248. }
  249. public static function check($var, $find)
  250. {
  251. if (is_array($var)) {
  252. $var = implode(',', $var);
  253. }
  254. $var = ',' . $var . ',';
  255. $find = ',' . $find . ',';
  256. return strpos($var, $find) !== false;
  257. }
  258. public static function json_encode($value)
  259. {
  260. //$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION);
  261. $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  262. /*
  263. if (strpos($value, '&lt;')) {
  264. $value = Dever\Helper\Secure::xss($value);
  265. }*/
  266. return $value;
  267. }
  268. public static function json_decode($value)
  269. {
  270. return json_decode($value, true);
  271. }
  272. public static function array_order($array, $key, $sort)
  273. {
  274. $reorder = array_column($array, $key);
  275. array_multisort($reorder, $sort, $array);
  276. return $array;
  277. }
  278. public static function number($number, $num = 2, $type = 1)
  279. {
  280. if ($type == 2) {
  281. return number_format($number, $num);
  282. }
  283. if ($type == 3) {
  284. return sprintf("%.".$num."f", $number);
  285. }
  286. return round($number + 0.000001, $num);
  287. }
  288. public static function uuid()
  289. {
  290. mt_srand((double)microtime() * 10000);
  291. $charid = strtoupper(self::id());
  292. $hyphen = chr(45);
  293. 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);
  294. }
  295. public static function id()
  296. {
  297. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  298. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  299. }
  300. public static function is_file($file)
  301. {
  302. if (strtolower(substr($file, 0, 4)) == 'http') {
  303. $header = get_headers($file, true);
  304. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  305. } else {
  306. return is_file($file);
  307. }
  308. }
  309. public static function subdir($dir)
  310. {
  311. return array_filter(scandir($dir), function($file) use ($dir) {
  312. return is_dir($dir . '/' . $file) && $file !== '.' && $file !== '..';
  313. });
  314. }
  315. }