boot.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. protected static $instances = [];
  11. protected static $requestId = null;
  12. protected static $requestInstances = [];
  13. protected static $counter = 0;
  14. protected static $reflectionCache = [];
  15. protected static $bindings = [];
  16. protected static $commit = [];
  17. protected static $data = [];
  18. public static function server()
  19. {
  20. spl_autoload_register(['Dever', 'autoload']);
  21. $http_worker = new \Workerman\Worker("http://0.0.0.0:8085");
  22. // 启动4个进程对外提供服务
  23. $http_worker->count = 4;
  24. // 接收到浏览器发送的数据时回复hello world给浏览器
  25. $http_worker->onMessage = function(\Workerman\Connection\TcpConnection $connection, $data)
  26. {
  27. $route = Dever\Route::get();
  28. print_r($route);
  29. // 向浏览器发送hello world
  30. $connection->send('hello world');
  31. };
  32. /*
  33. $http_worker->onMessage = function($connection, $request) {
  34. try {
  35. // 这里直接调用 run()
  36. ob_start(); // 捕获 run() 的输出
  37. Dever::run();
  38. $output = ob_get_clean();
  39. // 返回给客户端
  40. $connection->send($output);
  41. } catch (\Throwable $e) {
  42. $connection->send(self::out()->error($e->getMessage()));
  43. Dever::log('server_error', $e->getMessage());
  44. }
  45. };*/
  46. \Workerman\Worker::runAll();
  47. }
  48. public static function run()
  49. {
  50. self::beginRequest();
  51. spl_autoload_register(['Dever', 'autoload']);
  52. $route = self::get(Dever\Route::class)->get();
  53. $out = self::out();
  54. self::get(Dever\Debug::class)->init();
  55. self::get(Dever\Project::class)->register();
  56. if (isset(self::config('setting')['cache'])) {
  57. $index = DEVER_APP_NAME . DIRECTORY_SEPARATOR . $route['l'];
  58. if (isset(self::config('setting')['cache'][$index])) {
  59. $expire = self::config('setting')['cache'][$index];
  60. if (isset($route['shell'])) {
  61. unset($route['shell']);
  62. }
  63. $key = md5(DEVER_APP_NAME . http_build_query($route));
  64. if ($result = self::cache($key)) {
  65. return self::out()->success($result);
  66. }
  67. }
  68. }
  69. if ($route['l'] && strpos($route['l'], '.')) {
  70. list($class, $method) = explode('.', $route['l']);
  71. $class = strtr(ucwords(strtr($class, '/', ' ')), ' ', '\\');
  72. if (strpos($class, 'Manage') === 0) {
  73. $class = str_replace('Manage\\', '', $class);
  74. $class = DEVER_APP_NAME . '\\Manage\\Api\\' . $class;
  75. } else {
  76. $class = DEVER_APP_NAME . '\\Api\\' . $class;
  77. }
  78. $result = self::out()->success(self::load($class)->loadDevelop($method, self::input(), true));
  79. } else {
  80. $result = self::out()->success('ok');
  81. }
  82. if (isset($expire)) {
  83. self::cache($key, $result, $expire);
  84. }
  85. }
  86. protected static function beginRequest()
  87. {
  88. self::$counter++;
  89. self::$requestId = 'req_' . getmypid() . '_' . self::$counter . '_' . microtime(true);
  90. self::$requestInstances[self::$requestId] = [];
  91. }
  92. public static function endRequest()
  93. {
  94. if (self::$requestId && isset(self::$requestInstances[self::$requestId])) {
  95. unset(self::$requestInstances[self::$requestId]);
  96. }
  97. self::$requestId = null;
  98. }
  99. public static function getCommit()
  100. {
  101. return empty(self::$commit[self::$requestId]);
  102. }
  103. public static function setCommit()
  104. {
  105. self::$commit[self::$requestId] = true;
  106. }
  107. public static function setData($key, $data)
  108. {
  109. return self::$data[self::$requestId][$key] = $data;
  110. }
  111. public static function getData($key)
  112. {
  113. return self::$data[self::$requestId][$key] ?? false;
  114. }
  115. protected static function resolve($class)
  116. {
  117. if (isset(self::$bindings[$class])) {
  118. $binding = self::$bindings[$class];
  119. $concrete = $binding['concrete'];
  120. if ($concrete instanceof \Closure) {
  121. $object = $concrete();
  122. } elseif (is_string($concrete)) {
  123. $object = self::make($concrete);
  124. } else {
  125. $object = $concrete;
  126. }
  127. if ($binding['shared']) {
  128. self::$instances[$class] = $object;
  129. }
  130. return $object;
  131. }
  132. return self::build($class);
  133. }
  134. protected static function build($class)
  135. {
  136. if (!isset(self::$reflectionCache[$class])) {
  137. self::$reflectionCache[$class] = new \ReflectionClass($class);
  138. }
  139. $refClass = self::$reflectionCache[$class];
  140. $constructor = $refClass->getConstructor();
  141. if (!$constructor || $constructor->getNumberOfParameters() === 0) {
  142. return new $class();
  143. }
  144. $dependencies = [];
  145. foreach ($constructor->getParameters() as $param) {
  146. $type = $param->getType();
  147. if ($type && !$type->isBuiltin()) {
  148. $dependencies[] = self::get($type->getName());
  149. } elseif ($param->isDefaultValueAvailable()) {
  150. $dependencies[] = $param->getDefaultValue();
  151. } else {
  152. throw new \Exception("无法解析 {$class} 的依赖参数 \${$param->getName()}");
  153. }
  154. }
  155. return $refClass->newInstanceArgs($dependencies);
  156. }
  157. public static function bind($abstract, $concrete, $shared = false)
  158. {
  159. self::$bindings[$abstract] = compact('concrete', 'shared');
  160. }
  161. public static function get($class, $requestScope = true, $key = null)
  162. {
  163. if ($key === null) $key = $class;
  164. if ($requestScope) {
  165. if (!self::$requestId) {
  166. self::beginRequest();
  167. }
  168. if (!isset(self::$requestInstances[self::$requestId][$key])) {
  169. self::$requestInstances[self::$requestId][$key] = self::resolve($class);
  170. }
  171. return self::$requestInstances[self::$requestId][$key];
  172. } else {
  173. if (!isset(self::$instances[$key])) {
  174. self::$instances[$key] = self::resolve($class);
  175. }
  176. return self::$instances[$key];
  177. }
  178. }
  179. public static function make($class)
  180. {
  181. return self::resolve($class);
  182. }
  183. public static function autoload($class)
  184. {
  185. if (strpos($class, 'Dever') === 0 || strpos($class, 'Workerman') === 0) {
  186. include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
  187. } else {
  188. self::load($class);
  189. }
  190. }
  191. public static function call($class, $param = [])
  192. {
  193. if (!is_array($param)) $param = [$param];
  194. if (strpos($class, '?')) {
  195. list($class, $temp) = explode('?', $class);
  196. parse_str($temp, $temp);
  197. foreach ($temp as $k => $v) {
  198. array_unshift($param, $v);
  199. }
  200. }
  201. list($class, $method) = explode('.', $class);
  202. $class = strtr($class, '/', '\\');
  203. return self::load($class)->$method(...$param);
  204. }
  205. public static function load($class)
  206. {
  207. return self::get(Dever\App::class, false, $class)->__initialize($class);
  208. }
  209. public static function db($table, $store = 'default', $partition = false, $path = 'table')
  210. {
  211. return self::get(Dever\Model::class, false, $table)->__initialize($table, $store, $partition, $path);
  212. }
  213. public static function option($table, $type = '', $where = [])
  214. {
  215. $data = Dever::db($table)->select($where);
  216. if ($type) {
  217. if (is_bool($type)) {
  218. $type = '不选择';
  219. }
  220. $default = [0 => ['id' => -1, 'name' => $type]];
  221. $data = array_merge($default, $data);
  222. }
  223. return $data;
  224. }
  225. public static function field($table, $id, $default = '无', $key = 'name')
  226. {
  227. if ($id && $id > 0) {
  228. $info = Dever::db($table)->find($id);
  229. return $info[$key];
  230. }
  231. return $default;
  232. }
  233. # 定义常用方法,这里不用__callStatic
  234. public static function input(...$args)
  235. {
  236. return self::get(Dever\Route::class)->input(...$args);
  237. }
  238. public static function url(...$args)
  239. {
  240. return self::get(Dever\Route::class)->url(...$args);
  241. }
  242. public static function host(...$args)
  243. {
  244. return self::get(Dever\Route::class)->host(...$args);
  245. }
  246. public static function debug(...$args)
  247. {
  248. return self::get(Dever\Debug::class)->add(...$args);
  249. }
  250. public static function page(...$args)
  251. {
  252. return self::get(Dever\Paginator::class)->get(...$args);
  253. }
  254. public static function config(...$args)
  255. {
  256. return self::get(Dever\Config::class)->get(...$args);
  257. }
  258. public static function project($app)
  259. {
  260. return self::get(Dever\Project::class)->load($app);
  261. }
  262. public static function log(...$args)
  263. {
  264. return self::get(Dever\Log::class)->add(...$args);
  265. }
  266. public static function out()
  267. {
  268. return self::get(Dever\Output::class);
  269. }
  270. public static function error(...$args)
  271. {
  272. return self::out()->error(...$args);
  273. }
  274. public static function success(...$args)
  275. {
  276. return self::out()->success(...$args);
  277. }
  278. public static function apply($file)
  279. {
  280. [$app, $file] = explode('/', $file, 2);
  281. $project = Dever::project($app);
  282. require_once $project['path'] . $file . '.php';
  283. }
  284. public static function session(...$args)
  285. {
  286. return Dever\Session::oper(...$args);
  287. }
  288. public static function view(...$args)
  289. {
  290. return self::get(Dever\View::class)->show(...$args);
  291. }
  292. public static function file(...$args)
  293. {
  294. return self::get(Dever\File::class)->get(...$args);
  295. }
  296. public static function data()
  297. {
  298. return self::get(Dever\File::class)->data();
  299. }
  300. public static function rule(...$args)
  301. {
  302. return Dever\Helper\Rule::get(...$args);
  303. }
  304. public static function number(...$args)
  305. {
  306. return Dever\Helper\Math::format(...$args);
  307. }
  308. public static function math(...$args)
  309. {
  310. $key = $args[0];
  311. $args = array_slice($args, 1);
  312. return Dever\Helper\Math::$key(...$args);
  313. }
  314. public static function curl(...$args)
  315. {
  316. return self::get(Dever\Helper\Curl::class)->load(...$args);
  317. }
  318. public static function cache($key, $value = false)
  319. {
  320. if (isset(self::config('setting')['redis'])) {
  321. if ($value) {
  322. if ($value == 'delete') {
  323. return \Dever\Helper\Redis::delete($key);
  324. }
  325. return \Dever\Helper\Redis::set($key, self::json_encode($value));
  326. } else {
  327. return self::json_decode(\Dever\Helper\Redis::get($key));
  328. }
  329. }
  330. return false;
  331. }
  332. public static function shell($value)
  333. {
  334. return self::check(self::input('shell'), $value);
  335. }
  336. public static function store($store = 'default', $partition = false)
  337. {
  338. $setting = self::config('setting')['database'][$store];
  339. $class = 'Dever\\Store\\' . $setting['type'];
  340. return $class::getInstance($store, $setting, $partition);
  341. }
  342. public static function in_array($array, $value, $key = 'id', $show = 'name')
  343. {
  344. $column = array_column($array, $key);
  345. if ($column) {
  346. $index = array_search($value, $column);
  347. if ($index >= 0) {
  348. return $array[$index][$show];
  349. }
  350. }
  351. return false;
  352. }
  353. public static function issets($input, $value = false)
  354. {
  355. if (isset($input[$value])) {
  356. if (is_string($input[$value]) && !strlen($input[$value])) {
  357. return false;
  358. }
  359. return $input[$value];
  360. }
  361. return false;
  362. }
  363. public static function check($var, $find)
  364. {
  365. if (is_array($var)) {
  366. $var = implode(',', $var);
  367. }
  368. return strpos(',' . $var . ',', ',' . $find . ',') !== false;
  369. }
  370. public static function json_encode($value)
  371. {
  372. return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  373. }
  374. public static function json_decode($value)
  375. {
  376. return json_decode($value, true);
  377. }
  378. public static function array_order($array, $key, $sort)
  379. {
  380. $reorder = array_column($array, $key);
  381. array_multisort($reorder, $sort, $array);
  382. return $array;
  383. }
  384. public static function uuid()
  385. {
  386. mt_srand((double)microtime() * 10000);
  387. $charid = strtoupper(self::id());
  388. $hyphen = chr(45);
  389. 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);
  390. }
  391. public static function id()
  392. {
  393. $charid = strtoupper(md5(uniqid(mt_rand(), true)));
  394. return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
  395. }
  396. public static function is_file($file)
  397. {
  398. if (strtolower(substr($file, 0, 4)) == 'http') {
  399. $header = get_headers($file, true);
  400. return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
  401. } else {
  402. return is_file($file);
  403. }
  404. }
  405. public static function subdir($dir)
  406. {
  407. return array_filter(scandir($dir), function($file) use ($dir) {
  408. return is_dir($dir . '/' . $file) && $file !== '.' && $file !== '..';
  409. });
  410. }
  411. }