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');
};
\Workerman\Worker::runAll();
}
public static function run()
{
spl_autoload_register(['Dever', 'autoload']);
$route = Dever\Route::get();
Dever\Debug::init();
Dever\Project::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 Dever\Output::success($result);
}
}
}
if ($route['l'] && strpos($route['l'], '.')) {
list($class, $method) = explode('.', $route['l']);
if (strstr($class, 'manage/') && !strstr($class, 'api')) {
Dever\Output::error('route error');
}
$result = Dever\Output::success(self::load($class, DEVER_APP_NAME, 'api')->loadDevelop($method, Dever\Route::input(), true));
} else {
$result = Dever\Output::success('ok');
}
if (isset($expire)) {
self::cache($key, $result, $expire);
}
}
public static function autoload($class)
{
if (strpos($class, 'Dever') === 0 || strpos($class, 'Workerman') === 0) {
include DEVER_PATH . 'src/' . str_replace('\\', '/', $class) . '.php';
} else {
Dever\Library::autoload($class);
}
}
public static function call($class, $param = [], $path = '')
{
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);
}
}
if (strpos($class, '.')) {
list($class, $method) = explode('.', $class);
$call = 'load';
if (!$path) $path = 'lib';
} elseif (strpos($class, '-')) {
list($class, $method) = explode('-', $class);
$call = 'db';
if (!$path) $path = true;
}
return self::$call($class, '', $path)->$method(...$param);
}
public static function load($class, $app = '', $path = 'lib')
{
if (strpos($class, '/') && !$app) {
list($app, $class) = explode('/', $class, 2);
}
$index = $app . $path . $class;
if (empty(self::$data[$index])) {
self::$data[$index] = new Dever\Import($class, $app, $path);
}
return self::$data[$index];
}
public static function db($table, $app = '', $cache = true, $store = 'default', $partition = false, $path = 'table')
{
if (strpos($table, '/') && !$app) {
list($app, $table) = explode('/', $table);
}
$index = $app . $path . $table;
if (empty(self::$data[$index]) || !$cache) {
self::$data[$index] = new Dever\Model($table, $app, $store, $partition, $path);
}
return self::$data[$index];
}
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 config(...$args)
{
return Dever\Config::get(...$args);
}
public static function apply(...$args)
{
return Dever\Library::apply(...$args);
}
public static function success(...$args)
{
return Dever\Output::success(...$args);
}
public static function error(...$args)
{
return Dever\Output::error(...$args);
}
public static function out(...$args)
{
return Dever\Output::out(...$args);
}
public static function input(...$args)
{
return Dever\Route::input(...$args);
}
public static function url(...$args)
{
return Dever\Route::url(...$args);
}
public static function host(...$args)
{
return Dever\Route::host(...$args);
}
public static function project(...$args)
{
return Dever\Project::load(...$args);
}
public static function log(...$args)
{
return Dever\Log::add(...$args);
}
public static function debug(...$args)
{
return Dever\Debug::add(...$args);
}
public static function session(...$args)
{
return Dever\Session::oper(...$args);
}
public static function view(...$args)
{
return Dever\View::show(...$args);
}
public static function file(...$args)
{
return Dever\File::get(...$args);
}
public static function data()
{
return Dever\File::data();
}
public static function page(...$args)
{
return Dever\Paginator::get(...$args);
}
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)
{
$curl = new Dever\Helper\Curl();
return $curl->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 Dever::check(Dever\Route::input('shell'), $value);
}
public static function store($store = 'default', $partition = false)
{
$setting = Dever\Config::get('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);
}
$var = ',' . $var . ',';
$find = ',' . $find . ',';
return strpos($var, $find) !== false;
}
public static function json_encode($value)
{
//$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION);
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
/*
if (strpos($value, '<')) {
$value = Dever\Helper\Secure::xss($value);
}*/
return $value;
}
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 !== '..';
});
}
}