boot.php 14 KB

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