123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <?php
- ini_set('display_errors', true);
- 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);
- if (defined('DEVER_SERVER')) {
- Dever::server();
- } else {
- Dever::run();
- }
- class Dever
- {
- protected static $instances = [];
- protected static $requestId = null;
- protected static $requestInstances = [];
- protected static $counter = 0;
- protected static $reflectionCache = [];
- protected static $bindings = [];
- protected static $commit = [];
- protected static $data = [];
- public static function server()
- {
- spl_autoload_register(['Dever', 'autoload']);
- $http_worker = new \Workerman\Worker("http://0.0.0.0:8085");
- // 启动4个进程对外提供服务
- $http_worker->count = 4;
- // 接收到浏览器发送的数据时回复hello world给浏览器
- $http_worker->onMessage = function(\Workerman\Connection\TcpConnection $connection, $data)
- {
- $route = Dever\Route::get();
- print_r($route);
- // 向浏览器发送hello world
- $connection->send('hello world');
- };
- /*
- $http_worker->onMessage = function($connection, $request) {
- try {
- // 这里直接调用 run()
- ob_start(); // 捕获 run() 的输出
- Dever::run();
- $output = ob_get_clean();
- // 返回给客户端
- $connection->send($output);
- } catch (\Throwable $e) {
- $connection->send(self::out()->error($e->getMessage()));
- Dever::log('server_error', $e->getMessage());
- }
- };*/
- \Workerman\Worker::runAll();
- }
- public static function run()
- {
- self::beginRequest();
- spl_autoload_register(['Dever', 'autoload']);
- $route = self::get(Dever\Route::class)->get();
- $out = self::out();
- self::get(Dever\Debug::class)->init();
- self::get(Dever\Project::class)->register();
- if (isset(self::config('setting')['cache'])) {
- $index = DEVER_APP_NAME . DIRECTORY_SEPARATOR . $route['l'];
- if (isset(self::config('setting')['cache'][$index])) {
- $expire = self::config('setting')['cache'][$index];
- if (isset($route['shell'])) {
- unset($route['shell']);
- }
- $key = md5(DEVER_APP_NAME . http_build_query($route));
- if ($result = self::cache($key)) {
- return self::out()->success($result);
- }
- }
- }
- if ($route['l'] && strpos($route['l'], '.')) {
- list($class, $method) = explode('.', $route['l']);
- $class = strtr(ucwords(strtr($class, '/', ' ')), ' ', '\\');
- if (strpos($class, 'Manage') === 0) {
- $class = str_replace('Manage\\', '', $class);
- $class = DEVER_APP_NAME . '\\Manage\\Api\\' . $class;
- } else {
- $class = DEVER_APP_NAME . '\\Api\\' . $class;
- }
- $result = self::out()->success(self::load($class)->loadDevelop($method, self::input(), true));
- } else {
- $result = self::out()->success('ok');
- }
- if (isset($expire)) {
- self::cache($key, $result, $expire);
- }
- }
- protected static function beginRequest()
- {
- self::$counter++;
- self::$requestId = 'req_' . getmypid() . '_' . self::$counter . '_' . microtime(true);
- self::$requestInstances[self::$requestId] = [];
- }
- public static function endRequest()
- {
- if (self::$requestId && isset(self::$requestInstances[self::$requestId])) {
- unset(self::$requestInstances[self::$requestId]);
- }
- self::$requestId = null;
- }
- public static function getCommit()
- {
- return empty(self::$commit[self::$requestId]);
- }
- public static function setCommit()
- {
- self::$commit[self::$requestId] = true;
- }
- public static function setData($key, $data)
- {
- return self::$data[self::$requestId][$key] = $data;
- }
- public static function getData($key)
- {
- return self::$data[self::$requestId][$key] ?? false;
- }
- protected static function resolve($class)
- {
- if (isset(self::$bindings[$class])) {
- $binding = self::$bindings[$class];
- $concrete = $binding['concrete'];
- if ($concrete instanceof \Closure) {
- $object = $concrete();
- } elseif (is_string($concrete)) {
- $object = self::make($concrete);
- } else {
- $object = $concrete;
- }
- if ($binding['shared']) {
- self::$instances[$class] = $object;
- }
- return $object;
- }
- return self::build($class);
- }
- protected static function build($class)
- {
- if (!isset(self::$reflectionCache[$class])) {
- self::$reflectionCache[$class] = new \ReflectionClass($class);
- }
- $refClass = self::$reflectionCache[$class];
- $constructor = $refClass->getConstructor();
- if (!$constructor || $constructor->getNumberOfParameters() === 0) {
- return new $class();
- }
- $dependencies = [];
- foreach ($constructor->getParameters() as $param) {
- $type = $param->getType();
- if ($type && !$type->isBuiltin()) {
- $dependencies[] = self::get($type->getName());
- } elseif ($param->isDefaultValueAvailable()) {
- $dependencies[] = $param->getDefaultValue();
- } else {
- throw new \Exception("无法解析 {$class} 的依赖参数 \${$param->getName()}");
- }
- }
- return $refClass->newInstanceArgs($dependencies);
- }
- public static function bind($abstract, $concrete, $shared = false)
- {
- self::$bindings[$abstract] = compact('concrete', 'shared');
- }
- public static function get($class, $requestScope = true, $key = null)
- {
- if ($key === null) $key = $class;
- if ($requestScope) {
- if (!self::$requestId) {
- self::beginRequest();
- }
- if (!isset(self::$requestInstances[self::$requestId][$key])) {
- self::$requestInstances[self::$requestId][$key] = self::resolve($class);
- }
- return self::$requestInstances[self::$requestId][$key];
- } else {
- if (!isset(self::$instances[$key])) {
- self::$instances[$key] = self::resolve($class);
- }
- return self::$instances[$key];
- }
- }
- public static function make($class)
- {
- return self::resolve($class);
- }
- public static function autoload($class)
- {
- if (strpos($class, 'Dever') === 0 || strpos($class, 'Workerman') === 0) {
- include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
- } else {
- self::load($class);
- }
- }
- public static function call($class, $param = [])
- {
- if (!is_array($param)) $param = [$param];
- if (strpos($class, '?')) {
- list($class, $temp) = explode('?', $class);
- parse_str($temp, $temp);
- foreach ($temp as $k => $v) {
- array_unshift($param, $v);
- }
- }
-
- list($class, $method) = explode('.', $class);
- $class = strtr($class, '/', '\\');
- return self::load($class)->$method(...$param);
- }
- public static function load($class)
- {
- return self::get(Dever\App::class, false, $class)->__initialize($class);
- }
- public static function db($table, $store = 'default', $partition = false, $path = 'table')
- {
- return self::get(Dever\Model::class, false, $table)->__initialize($table, $store, $partition, $path);
- }
- public static function option($table, $type = '', $where = [])
- {
- $data = Dever::db($table)->select($where);
- if ($type) {
- if (is_bool($type)) {
- $type = '不选择';
- }
- $default = [0 => ['id' => -1, 'name' => $type]];
- $data = array_merge($default, $data);
- }
- return $data;
- }
- public static function field($table, $id, $default = '无', $key = 'name')
- {
- if ($id && $id > 0) {
- $info = Dever::db($table)->find($id);
- return $info[$key];
- }
- return $default;
- }
- # 定义常用方法,这里不用__callStatic
- public static function input(...$args)
- {
- return self::get(Dever\Route::class)->input(...$args);
- }
- public static function url(...$args)
- {
- return self::get(Dever\Route::class)->url(...$args);
- }
- public static function host(...$args)
- {
- return self::get(Dever\Route::class)->host(...$args);
- }
- public static function debug(...$args)
- {
- return self::get(Dever\Debug::class)->add(...$args);
- }
- public static function page(...$args)
- {
- return self::get(Dever\Paginator::class)->get(...$args);
- }
- public static function config(...$args)
- {
- return self::get(Dever\Config::class)->get(...$args);
- }
- public static function project($app)
- {
- return self::get(Dever\Project::class)->load($app);
- }
- public static function log(...$args)
- {
- return self::get(Dever\Log::class)->add(...$args);
- }
- public static function out()
- {
- return self::get(Dever\Output::class);
- }
- public static function error(...$args)
- {
- return self::out()->error(...$args);
- }
- public static function apply($file)
- {
- [$app, $file] = explode('/', $file, 2);
- $project = Dever::project($app);
- require_once $project['path'] . $file . '.php';
- }
- public static function session(...$args)
- {
- return Dever\Session::oper(...$args);
- }
- public static function view(...$args)
- {
- return self::get(Dever\View::class)->show(...$args);
- }
- public static function file(...$args)
- {
- return self::get(Dever\File::class)->get(...$args);
- }
- public static function data()
- {
- return self::get(Dever\File::class)->data();
- }
-
- public static function rule(...$args)
- {
- return Dever\Helper\Rule::get(...$args);
- }
- public static function number(...$args)
- {
- return Dever\Helper\Math::format(...$args);
- }
- public static function math(...$args)
- {
- $key = $args[0];
- $args = array_slice($args, 1);
- return Dever\Helper\Math::$key(...$args);
- }
- public static function curl(...$args)
- {
- return self::get(Dever\Helper\Curl::class)->load(...$args);
- }
- public static function cache($key, $value = false)
- {
- if (isset(self::config('setting')['redis'])) {
- if ($value) {
- if ($value == 'delete') {
- return \Dever\Helper\Redis::delete($key);
- }
- return \Dever\Helper\Redis::set($key, self::json_encode($value));
- } else {
- return self::json_decode(\Dever\Helper\Redis::get($key));
- }
- }
- return false;
- }
- public static function shell($value)
- {
- return self::check(self::input('shell'), $value);
- }
- public static function store($store = 'default', $partition = false)
- {
- $setting = self::config('setting')['database'][$store];
- $class = 'Dever\\Store\\' . $setting['type'];
- return $class::getInstance($store, $setting, $partition);
- }
- public static function in_array($array, $value, $key = 'id', $show = 'name')
- {
- $column = array_column($array, $key);
- if ($column) {
- $index = array_search($value, $column);
- if ($index >= 0) {
- return $array[$index][$show];
- }
- }
- return false;
- }
- public static function issets($input, $value = false)
- {
- if (isset($input[$value])) {
- if (is_string($input[$value]) && !strlen($input[$value])) {
- return false;
- }
- return $input[$value];
- }
- return false;
- }
- public static function check($var, $find)
- {
- if (is_array($var)) {
- $var = implode(',', $var);
- }
- return strpos(',' . $var . ',', ',' . $find . ',') !== false;
- }
- public static function json_encode($value)
- {
- return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
- }
- public static function json_decode($value)
- {
- return json_decode($value, true);
- }
- public static function array_order($array, $key, $sort)
- {
- $reorder = array_column($array, $key);
- array_multisort($reorder, $sort, $array);
- return $array;
- }
- public static function uuid()
- {
- mt_srand((double)microtime() * 10000);
- $charid = strtoupper(self::id());
- $hyphen = chr(45);
- 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);
- }
- public static function id()
- {
- $charid = strtoupper(md5(uniqid(mt_rand(), true)));
- return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
- }
- public static function is_file($file)
- {
- if (strtolower(substr($file, 0, 4)) == 'http') {
- $header = get_headers($file, true);
- return isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'));
- } else {
- return is_file($file);
- }
- }
- public static function subdir($dir)
- {
- return array_filter(scandir($dir), function($file) use ($dir) {
- return is_dir($dir . '/' . $file) && $file !== '.' && $file !== '..';
- });
- }
- }
|