public function offline() { // echo 'offline start'.PHP_EOL; ZCache::getInstance('Redis', ZConfig::getField('cache', 'net'))->delete($this->params['fd']); $this->boardcast(['cmd' => 'offline', 'fd' => $this->params['fd'], 'from' => 0, 'channal' => 0], false); // echo 'offline end'.PHP_EOL; }
public static function getCache() { if (empty(self::$cache)) { $config = ZConfig::getField('cache', 'net'); self::$cache = ZCache::getInstance($config['adapter'], $config); } return self::$cache; }
public function __construct($config) { if (empty($this->yac)) { $this->yac = ZCache::getInstance($config['adapter'], $config); if (!$this->yac->enable()) { throw new \Exception("Yac no enable"); } } }
public static function setToken($uid) { $token = uniqid(); $config = ZConfig::getField('cache', 'net'); $cacheHelper = ZCache::getInstance($config['adapter'], $config); $key = "{$uid}_t_" . ZConfig::getField('project', 'name'); if ($cacheHelper->set($key, $token)) { return $token; } throw new \Exception("token set error", ERROR::TOKEN_ERROR); }
/** * 包格式: 包总长+命令id+请求id+数据 * * @param $_data * @return bool */ public function parse($_data) { $ctrlName = Config::getField('project', 'default_ctrl_name', 'main\\main'); $methodName = Config::getField('project', 'default_method_name', 'main'); if (empty($this->_cache)) { $this->_cache = ZCache::getInstance('Php'); } $fd = Request::getFd(); $cacheData = $this->_cache->get($fd); if (!empty($cacheData)) { $_data = $cacheData . $_data; $this->_cache->delete($fd); } if (empty($_data)) { return false; } $packData = new MessagePacker($_data); $packLen = $packData->readInt(); $dataLen = \strlen($_data); if ($packLen > $dataLen) { $this->_cache->set($fd, $_data); return false; } elseif ($packLen < $dataLen) { $this->_cache->set($fd, \substr($_data, $packLen, $dataLen - $packLen)); } $packData->resetOffset(4); $data = []; $data['_cmd'] = $packData->readInt(); $pathinfo = Config::getField('cmdlist', $data['_cmd']); $data['_rid'] = $packData->readInt(); $params = $packData->readString(); $unpackData = \json_decode(gzdecode($params), true); if (!empty($unpackData) && \is_array($unpackData)) { $data += $unpackData; } $routeMap = ZRoute::match(Config::get('route', false), $pathinfo); if (is_array($routeMap)) { $ctrlName = $routeMap[0]; $methodName = $routeMap[1]; if (!empty($routeMap[2]) && is_array($routeMap[2])) { //参数优先 $data = $data + $routeMap[2]; } } Request::init($ctrlName, $methodName, $data); return true; }
/** * 返回友好的url * param $ctrl //ctrl class * param $method //所要执行的method * param $params //额外参数 * return * 如果是静态路由,直接返回 路由的key值 * 如果是动态路由,会根据匹配到配置的友好url进行格式化处理 * examples: * config/route.php * return array( * 'static'=>array( * 'reg'=>array( * 'main\\main', 'reg' * ), * ) * 'dynamic'=>array( * '/^\/product\/(\d+)$/iU''=>array( //匹配 /product/123 将被匹配 * 'main\\product', //ctrl class * 'show', //ctrl method * array('id'), //匹配参数 //名为id的参数将被赋值 123 * '/product/{id}' //格式化 * ), * ) * * * ) * 如果配置了route: * 调用 \ZPHP\Common\Route::makeUrl('main\\main', 'reg'), 将生成url http://host/reg * 调用 \ZPHP\Common\Route::makeUrl('main\\product', 'show', array("id"=>123, "uid"=>321)), 将生成url http://host/product/123?uid=321 */ public static function makeUrl($ctrl, $method, $params = array()) { $appUrl = ZConfig::getField('project', 'app_host', ""); $ctrlName = ZConfig::getField('project', 'ctrl_name', 'a'); $methodName = ZConfig::getField('project', 'method_name', 'm'); if (empty($appUrl)) { $appUrl = '/'; } else { if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { $appUrl = 'https://' . $appUrl; } else { $appUrl = 'http://' . $appUrl; } } $routes = ZConfig::get('route', false); if (!empty($routes)) { if (isset($routes['cache'])) { if (!empty($route['cache'])) { $config = ZConfig::getField('cache', 'locale', array()); if (!empty($config)) { $cache = ZCache::getInstance($config['adapter'], $config); $cacheKey = self::getKey($ctrl, $method, $params); $result = $cache->get($cacheKey); if (!empty($result)) { return $result; } } } unset($routes['cache']); } $ext = ''; if (!empty($routes['ext'])) { $ext = $routes['ext']; unset($routes['ext']); } $result = false; foreach ($routes as $type => $rules) { foreach ($rules as $path => $rule) { if ($rule[0][0] == '{' || $rule[0] == str_replace('/', '\\', $ctrl)) { if ($rule[1][0] != '{' && $rule[1] != $method) { continue; } if ('static' == $type) { if (empty($params)) { if ('' == $path || '/' == $path) { $result = $appUrl . $path; } else { $result = $appUrl . $path . $ext; } } else { $result = $appUrl . $path . $ext . '?' . http_build_query($params); } } else { $realPath = $rule[3]; $realPath = str_replace(array('{c}', '{m}'), array($ctrl, $method), $realPath); if (!empty($rule[2])) { foreach ($rule[2] as $key) { if (isset($params[$key])) { $realPath = str_replace("{{$key}}", $params[$key], $realPath); unset($params[$key]); } } } if (empty($params)) { $result = $appUrl . $realPath . $ext; } else { $result = $appUrl . $realPath . $ext . '?' . http_build_query($params); } } if ($result) { if (isset($cacheKey)) { $cache->set($cacheKey, $result); } return $result; } } } } } if (empty($params)) { return $appUrl . "?{$ctrlName}={$ctrl}&{$methodName}={$method}"; } return $appUrl . "?{$ctrlName}={$ctrl}&{$methodName}={$method}&" . http_build_query($params); }