示例#1
0
 public static function route($server)
 {
     $ctrl = $server->getCtrl();
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . $ctrl;
     $class = Factory::getInstance($action);
     if (!$class instanceof IController) {
         throw new \Exception("ctrl error");
     }
     $class->setServer($server);
     $before = $class->_before();
     $exception = null;
     if ($before) {
         try {
             $method = $server->getMethod();
             if (\method_exists($class, $method)) {
                 $class->{$method}();
             } else {
                 throw new \Exception("no method {$method}");
             }
         } catch (\Exception $e) {
             $exception = $e;
         }
     }
     $class->_after();
     if ($exception !== null) {
         $exception->e_no = $exception->getCode() < 0 ? 0 : $exception->getCode();
         $exception->e_msg = $exception->getMessage();
         $exception->e_data = $class->data;
         throw $exception;
     }
     //正确返回
     $result = array('e_no' => $class->e_no, 'e_msg' => '', 'api' => $ctrl, 'data' => $class->data);
     return $server->display($result);
 }
示例#2
0
 public static function route()
 {
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . Request::getCtrl();
     $view = null;
     try {
         $class = Factory::getInstance($action);
         if (!$class instanceof IController) {
             throw new \Exception("ctrl error");
         } else {
             $class->_before();
             $method = Request::getMethod();
             if (!method_exists($class, $method)) {
                 throw new \Exception("method error");
             }
             $view = $class->{$method}();
             $class->_after();
             if (null === $view) {
                 return null;
             }
             return Response::display($view);
         }
     } catch (\Exception $e) {
         if (Request::isLongServer()) {
             return \call_user_func(Config::getField('project', 'exception_handler', 'ZPHP\\ZPHP::exceptionHandler'), $e);
         }
         throw $e;
     }
 }
示例#3
0
 public function main()
 {
     //        $this->params = array(
     //            'a' => "user\\login",
     //            'p' => array(
     //                'uid' => 1,
     //                'params' => array(),
     //            ),
     //        );
     if (!$this->uid) {
         throw new common\error('错误的用户登陆');
     }
     $uInfo = $this->userModel->getUserById($this->uid);
     if (!$uInfo) {
         $initUserConfig = ZConfig::getField('init', 'user');
         $d = array('id' => $this->uid, 'coin' => $initUserConfig['coin'], 'created' => time());
         $this->userModel->addUser($d);
     }
     $uConnectInfo = $this->connection->get($this->uid);
     if (!$uConnectInfo) {
         $this->connection->add($this->uid, $this->fd);
         $this->connection->addFd($this->fd, $this->uid);
     } else {
         common\connection::close($uConnectInfo['fd']);
         $this->connection->add($this->uid, $this->fd);
         $this->connection->addFd($this->fd, $this->uid);
     }
     //        common\connection::sendOne($this->fd,'login', 'test send one');
     //        common\connection::sendToChannel('login', 'test send all');
     $this->data = array('global' => array('serverTime' => time(), 'nextRoundTime' => common\game::getNextRunTime(), 'currentRound' => common\game::getRuncount()), 'positionList' => common\game::getPositionList(), 'user' => $uInfo ? $uInfo : $d, 'map' => ZConfig::get('map'), 'item' => ZConfig::get('item'));
 }
示例#4
0
 public static function route($server)
 {
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . $server->getCtrl();
     $class = Factory::getInstance($action);
     if (!$class instanceof IController) {
         throw new \Exception("ctrl error");
     }
     $class->setServer($server);
     $before = $class->_before();
     $view = $exception = null;
     if ($before) {
         try {
             $method = $server->getMethod();
             if (\method_exists($class, $method)) {
                 $view = $class->{$method}();
             } else {
                 throw new \Exception("no method {$method}");
             }
         } catch (\Exception $e) {
             $exception = $e;
         }
     }
     $class->_after();
     if ($exception !== null) {
         throw $exception;
     }
     if (null === $view) {
         return;
     }
     return $server->display($view);
 }
示例#5
0
文件: Factory.php 项目: jixm/zphp
 public static function start($sessionType = '', $config = '')
 {
     if (false === self::$isStart) {
         if (empty($config)) {
             $config = ZConfig::get('session');
         }
         if (!empty($config['adapter'])) {
             $sessionType = $config['adapter'];
         }
         $lifetime = 0;
         if (!empty($config['cache_expire'])) {
             \session_cache_expire($config['cache_expire']);
             $lifetime = $config['cache_expire'] * 60;
         }
         $path = empty($config['path']) ? '/' : $config['path'];
         $domain = empty($config['domain']) ? '' : $config['domain'];
         $secure = empty($config['secure']) ? false : $config['secure'];
         $httponly = empty($config['httponly']) ? true : $config['httponly'];
         \session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
         $sessionName = empty($config['session_name']) ? 'ZPHPSESSID' : $config['session_name'];
         \session_name($sessionName);
         if (!empty($_GET[$sessionName])) {
             \session_id($_GET[$sessionName]);
         } elseif (!empty($_SERVER[$sessionName])) {
             \session_id($_SERVER[$sessionName]);
         }
         if (!empty($sessionType)) {
             $handler = self::getInstance($sessionType, $config);
             \session_set_save_handler(array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc'));
         }
         \session_start();
         self::$isStart = true;
     }
 }
示例#6
0
文件: Json.php 项目: xifat/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         \header("Content-Type: application/json; charset=utf-8");
     }
     echo \json_encode($this->model);
 }
示例#7
0
文件: Http.php 项目: 446244451/zphp
 /**
  * 直接 parse $_REQUEST
  * @param $_data
  * @return bool
  */
 public function parse($data)
 {
     $this->_ctrl = Config::getField('project', 'default_ctrl_name', 'main\\main');
     $this->_method = Config::getField('project', 'default_method_name', 'main');
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($data[$apn])) {
         $this->_ctrl = \str_replace('/', '\\', $data[$apn]);
     }
     if (isset($data[$mpn])) {
         $this->_method = $data[$mpn];
     }
     if (!empty($_SERVER['PATH_INFO'])) {
         $routeMap = ZRoute::match(Config::get('route', false), $_SERVER['PATH_INFO']);
         if (is_array($routeMap)) {
             $this->_ctrl = $routeMap[0];
             $this->_method = $routeMap[1];
             if (!empty($routeMap[2]) && is_array($routeMap[2])) {
                 //参数优先
                 $data = $data + $routeMap[2];
             }
         }
     }
     $this->_params = $data;
     $this->_tpl_file = str_replace('\\', DS, $this->_ctrl) . DS . $this->_method . '.php';
     return true;
 }
示例#8
0
文件: Http.php 项目: heesey/zphp
 /**
  * 直接 parse $_REQUEST
  * @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');
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($data[$apn])) {
         $ctrlName = \str_replace('/', '\\', $data[$apn]);
     }
     if (isset($data[$mpn])) {
         $methodName = $data[$mpn];
     }
     if (!empty($_SERVER['PATH_INFO'])) {
         //swoole_http模式 需要在onRequest里,设置一下 $_SERVER['PATH_INFO'] = $request->server['path_info']
         $routeMap = ZRoute::match(Config::get('route', false), $_SERVER['PATH_INFO']);
         if (is_array($routeMap)) {
             $ctrlName = \str_replace('/', '\\', $routeMap[0]);
             $methodName = $routeMap[1];
             if (!empty($routeMap[2]) && is_array($routeMap[2])) {
                 //参数优先
                 $data = $data + $routeMap[2];
             }
         }
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Php'));
     return true;
 }
示例#9
0
文件: Xml.php 项目: xifat/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         \header("Content-Type:text/xml; charset=utf-8");
     }
     echo $this->xmlEncode();
 }
示例#10
0
 public static function getConnection()
 {
     if (empty(self::$connection)) {
         $config = ZConfig::get('connection');
         self::$connection = CFactory::getInstance($config['adapter'], $config);
     }
     return self::$connection;
 }
示例#11
0
 private function getConnection()
 {
     if (empty($this->connection)) {
         $config = ZConfig::get('connection');
         $this->connection = CFactory::getInstance($config['adapter'], $config);
     }
     return $this->connection;
 }
示例#12
0
文件: Amf.php 项目: 446244451/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         Utils::header('Content-Type', 'application/amf; charset=utf-8');
         echo \amf3_encode($this->model);
     } else {
         return \amf3_encode($this->model);
     }
 }
示例#13
0
文件: Zpack.php 项目: xifat/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         \header("Content-Type: application/zpack; charset=utf-8");
     }
     $pack = new MessagePacker();
     $pack->writeString(json_encode($this->model));
     echo $pack->getData();
 }
示例#14
0
文件: Xml.php 项目: 446244451/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         Utils::header("Content-Type", "text/xml; charset=utf-8");
         echo $this->xmlEncode();
         return null;
     }
     return $this->xmlEncode();
 }
示例#15
0
文件: Swoole.php 项目: qai41/zphp
 public function onWorkerStart($server, $workerId)
 {
     if ($workerId >= ZConfig::getField('socket', 'worker_num')) {
         swoole_set_process_name(ZConfig::get('project_name') . " server task  num: {$server->worker_id} pid " . $server->worker_pid);
     } else {
         swoole_set_process_name(ZConfig::get('project_name') . " server worker  num: {$server->worker_id} pid " . $server->worker_pid);
     }
     if (function_exists('opcache_reset')) {
         opcache_reset();
     }
 }
示例#16
0
文件: Zpack.php 项目: 446244451/zphp
 public function display()
 {
     $pack = new MessagePacker();
     $pack->writeString(json_encode($this->model));
     if (Config::get('server_mode') == 'Http') {
         Utils::header("Content-Type", "application/zpack; charset=utf-8");
         echo $pack->getData();
     } else {
         return array($this->model, $pack->getData);
     }
 }
示例#17
0
文件: Socket.php 项目: xifat/zphp
 public function run()
 {
     $config = Config::get('socket');
     if (empty($config)) {
         throw new \Exception("socket config empty");
     }
     $socket = SFactory::getInstance($config['adapter'], $config);
     $client = CFactory::getInstance($config['client_class']);
     $socket->setClient($client);
     $socket->run();
 }
示例#18
0
文件: Base.php 项目: jxw7733/zphpdemo
 public function useDb()
 {
     if (empty($this->_db)) {
         $config = ZConfig::get('pdo');
         $this->_db = new ZPdo($config, $this->entity, $config['dbname']);
         $this->_db->setClassName($this->entity);
     } else {
         $this->_db->checkPing();
     }
     return $this->_db;
 }
示例#19
0
文件: String.php 项目: xifat/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         \header("Content-Type: text/plain; charset=utf-8");
     }
     if (\is_string($this->model)) {
         echo $this->model;
     } else {
         print_r($this->model, true);
     }
 }
示例#20
0
文件: Factory.php 项目: xifat/zphp
 public static function getInstance($adapter = 'Redis', $config = null)
 {
     if (empty($config)) {
         $config = ZConfig::get('rank');
         if (!empty($config['adapter'])) {
             $adapter = $config['adapter'];
         }
     }
     $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     return CFactory::getInstance($className, $config);
 }
示例#21
0
文件: String.php 项目: 446244451/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         Utils::header("Content-Type", "text/plain; charset=utf-8");
         if (\is_string($this->model)) {
             echo $this->model;
         } else {
             echo json_encode($this->model);
         }
         return null;
     }
     return $this->model;
 }
示例#22
0
文件: Swoole.php 项目: imdaqian/zphp
 public function onWorkerStart($server, $workerId)
 {
     $workNum = ZConfig::getField('socket', 'worker_num');
     if ($workerId >= $workNum) {
         swoole_set_process_name(ZConfig::get('project_name') . " server tasker  num: " . ($server->worker_id - $workNum) . " pid " . $server->worker_pid);
     } else {
         swoole_set_process_name(ZConfig::get('project_name') . " server worker  num: {$server->worker_id} pid " . $server->worker_pid);
     }
     if (function_exists('opcache_reset')) {
         opcache_reset();
     }
     Protocol\Request::setSocket($server);
 }
示例#23
0
文件: Log.php 项目: liyu9092/zphp
 public static function info($type, $params = array())
 {
     $t = \date("Ymd");
     $logPath = Config::getField('project', 'log_path', '');
     if (empty($logPath)) {
         $dir = ZPHP::getRootPath() . DS . 'log' . DS . $t;
     } else {
         $dir = $logPath . DS . $t;
     }
     Dir::make($dir);
     $str = \date('Y-m-d H:i:s', Config::get('now_time', time())) . self::SEPARATOR . \implode(self::SEPARATOR, array_map('ZPHP\\Common\\Log::myJson', $params));
     $logFile = $dir . \DS . $type . '.log';
     \file_put_contents($logFile, $str . "\n", FILE_APPEND | LOCK_EX);
 }
示例#24
0
文件: Log.php 项目: xifat/zphp
 public static function info($type, $params = array())
 {
     $t = \date("Ymd");
     $logPath = Config::get('log_path', '');
     if (empty($logPath)) {
         $dir = ZPHP::getRootPath() . DS . 'log' . DS . $t;
     } else {
         $dir = $logPath . DS . $t;
     }
     Dir::make($dir);
     $str = \date('Y-m-d H:i:s', Config::get('now_time', time())) . self::SEPARATOR . \implode(self::SEPARATOR, array_map('json_encode', $params));
     $logFile = $dir . \DS . $type . '.log';
     \error_log($str . "\n", 3, $logFile);
 }
示例#25
0
文件: Swoole.php 项目: phpdn/zphp
 public static function start($sessionType, $config)
 {
     if (null !== self::$_sid) {
         return;
     }
     //判断参数里是否有sessid
     if (empty($config)) {
         $config = ZConfig::get('session');
     }
     if (!empty($config['adapter'])) {
         $sessionType = $config['adapter'];
     }
     self::$_config = $config;
     self::$_sessionType = $sessionType;
     $request = Request::getRequest();
     $sessionName = empty($config['session_name']) ? 'ZPHPSESSID' : $config['session_name'];
     $sid = null;
     if (!empty($request->cookie[$sessionName])) {
         $sid = $request->cookie[$sessionName];
     }
     if (!$sid && !empty($request->get[$sessionName])) {
         $sid = $request->get[$sessionName];
     }
     if (!$sid && !empty($request->post[$sessionName])) {
         $sid = $request->post[$sessionName];
     }
     if ($sid) {
         $handler = Factory::getInstance($sessionType, $config);
         $data = $handler->read($sid);
         if (!empty($data)) {
             $_SESSION = unserialize($data);
         } else {
             $_SESSION = array();
         }
     } else {
         $sid = sha1($request->header['user-agent'] . $request->server['remote_addr'] . uniqid(Request::getSocket()->worker_pid . '_'));
         $path = empty($config['path']) ? '/' : $config['path'];
         $domain = empty($config['domain']) ? '' : $config['domain'];
         $secure = empty($config['secure']) ? false : $config['secure'];
         $httponly = !isset($config['httponly']) ? true : $config['httponly'];
         $lifetime = 0;
         if (!empty($config['cache_expire'])) {
             $lifetime = time() + $config['cache_expire'] * 60;
         }
         Response::getResponse()->cookie($sessionName, $sid, $lifetime, $path, $domain, $secure, $httponly);
         $_SESSION = array();
     }
     self::$_sid = $sid;
 }
示例#26
0
文件: Factory.php 项目: heesey/zphp
 public static function getInstance($adapter = 'Swoole', $config = null)
 {
     if (empty($config)) {
         $config = ZConfig::get('socket');
         if (!empty($config['adapter'])) {
             $adapter = $config['adapter'];
         }
     }
     if (is_file(__DIR__ . DS . 'Adapter' . DS . $adapter . '.php')) {
         $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     } else {
         $className = $adapter;
     }
     return CFactory::getInstance($className, $config);
 }
示例#27
0
文件: Json.php 项目: 446244451/zphp
 public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         $data = \json_encode($this->model);
         if (isset($_GET['jsoncallback'])) {
             Utils::header("Content-Type", 'application/x-javascript; charset=utf-8');
             echo $_GET['jsoncallback'] . '(' . $data . ')';
         } else {
             Utils::header("Content-Type", "application/json; charset=utf-8");
             echo $data;
         }
     } else {
         return \json_encode($this->model);
     }
 }
示例#28
0
 public function run()
 {
     $config = Config::get('socket');
     if (empty($config)) {
         throw new \Exception("socket config empty");
     }
     $socket = SFactory::getInstance($config['adapter'], $config);
     if (method_exists($socket, 'setClient')) {
         $client = CFactory::getInstance($config['client_class']);
         $socket->setClient($client);
     }
     Request::setServer(ZProtocol::getInstance(Config::getField('socket', 'protocol')));
     Request::setLongServer();
     Request::setHttpServer(0);
     $socket->run();
 }
示例#29
0
 /**
  * 包格式: 包总长+命令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;
 }
示例#30
0
文件: ZRpack.php 项目: 446244451/zphp
 public function display()
 {
     $jsonData = \json_encode($this->model);
     $data = gzencode($jsonData);
     $pack = new MessagePacker();
     $len = strlen($data);
     $pack->writeInt($len + 16);
     $pack->writeInt($this->model['cmd']);
     $pack->writeInt($this->model['rid']);
     $pack->writeString($data, $len);
     if (Config::get('server_mode') == 'Http') {
         Utils::header("Content-Type", "application/zrpack; charset=utf-8");
         echo $pack->getData();
     } else {
         return array($jsonData, $pack->getData());
     }
 }