示例#1
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;
 }
示例#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 display($model)
 {
     ($viewMode = $this->_view_mode) || ($viewMode = Config::getField('project', 'view_mode', ''));
     if (is_array($model) && !empty($model['_view_mode'])) {
         $viewMode = $model['_view_mode'];
         unset($model['_view_mode']);
     }
     $this->_view_mode = '';
     if (empty($viewMode)) {
         if (ZUtils::isAjax()) {
             $viewMode = 'Json';
         } else {
             $viewMode = 'Php';
         }
     }
     $view = View\Factory::getInstance($viewMode);
     if ('Php' === $viewMode) {
         if (is_array($model) && !empty($model['_tpl_file'])) {
             $view->setTpl($model['_tpl_file']);
             unset($model['_tpl_file']);
         } else {
             if (!empty($this->_tpl_file)) {
                 $view->setTpl($this->_tpl_file);
                 $this->_tpl_file = null;
             } else {
                 throw new \Exception("tpl file empty");
             }
         }
     }
     $view->setModel($model);
     return $view->display();
 }
示例#4
0
文件: chat.php 项目: jxw7733/zphpdemo
 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;
 }
示例#5
0
文件: FCGI.php 项目: xifat/zphp
 public function run($data, $fd = null)
 {
     if ($this->_client === null) {
         $this->_client = new Fcgi\Client(ZConfig::getField('socket', 'fcgi_host', '127.0.0.1'), ZConfig::getField('socket', 'fcgi_port', 9000));
     }
     return $this->_client->request($data);
 }
示例#6
0
文件: ZPHP.php 项目: heesey/zphp
 public function run($data, $fd = null)
 {
     $server = Protocol\Factory::getInstance(Core\Config::getField('socket', 'protocol', 'Http'));
     $server->setFd($fd);
     $server->parse($data);
     return Core\Route::route($server);
 }
示例#7
0
文件: Zpack.php 项目: heesey/zphp
 /**
  * client包格式: writeString(json_encode(array("a"='main/main',"m"=>'main', 'k1'=>'v1')));
  * server包格式:包总长+数据(json_encode)
  * @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');
     $fd = Request::getFd();
     if (!empty($this->_buffer[$fd])) {
         $_data = $this->_buffer . $_data;
     }
     $packData = new MessagePacker($_data);
     $packLen = $packData->readInt();
     $dataLen = \strlen($_data);
     if ($packLen > $dataLen) {
         $this->_buffer[$fd] = $_data;
         return false;
     } elseif ($packLen < $dataLen) {
         $this->_buffer[$fd] = \substr($_data, $packLen, $dataLen - $packLen);
     } else {
         if (!empty($this->_buffer[$fd])) {
             unset($this->_buffer[$fd]);
         }
     }
     $packData->resetOffset();
     $params = $packData->readString();
     $data = \json_decode($params, true);
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($params[$apn])) {
         $ctrlName = \str_replace('/', '\\', $params[$apn]);
     }
     if (isset($params[$mpn])) {
         $methodName = $params[$mpn];
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Zpack'));
     return true;
 }
示例#8
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'));
 }
示例#9
0
文件: RPC.php 项目: xifat/zphp
 public function run($data, $fd = null)
 {
     if ($this->_rpc === null) {
         $this->_rpc = new \Yar_Client(ZConfig::getField('socket', 'rpc_host'));
     }
     return $this->_rpc->api($data);
 }
示例#10
0
 /**
  * Send print to terminal.
  */
 private static function _log($msgType, $args)
 {
     if (!Config::getField('project', 'debug_mode', 0)) {
         return;
     }
     if (count($args) == 1) {
         $msg = is_scalar($args[0]) ? $args[0] : self::dump($args[0]);
     } else {
         $msg = self::dump($args);
     }
     if (self::$DEBUG_TRACE) {
         $trace = self::getTrace();
     } else {
         $trace = array();
     }
     if ($msgType == 'debug') {
         Terminal::drawStr($msg, 'magenta');
     } else {
         if ($msgType == 'error') {
             Terminal::drawStr($msg, 'red');
         } else {
             if ($msgType == 'info') {
                 Terminal::drawStr($msg, 'brown');
             } else {
                 Terminal::drawStr($msg, 'default');
             }
         }
     }
     //echo "\n";
     !empty($trace) && Terminal::drawStr("\t" . implode(" <-- ", $trace) . "\n");
 }
示例#11
0
文件: Base.php 项目: ymnl007/zphpdemo
 public function _before()
 {
     $ehConfig = ZConfig::getField('project', 'exception_handler');
     if (!empty($ehConfig)) {
         \set_exception_handler($ehConfig);
     }
     return true;
 }
示例#12
0
 public static function getRankCache()
 {
     if (empty(self::$rankCache)) {
         $config = ZConfig::getField('cache', 'net');
         self::$rankCache = ZRank::getInstance($config['adapter'], $config);
     }
     return self::$rankCache;
 }
示例#13
0
 public function main()
 {
     //        $this->params = array(
     //            'a' => "user\\ante",
     //            'p' => array(
     //                'uid' => 1,
     //                'params' => array(
     //                    'ante' => 'apple',
     //                    'type' => 1   //1:1 2:100
     //                ),
     //            ),
     //        );
     $this->checkLogin();
     //
     $nextRunTime = common\game::getNextRunTime();
     if (time() > $nextRunTime) {
         throw new common\error('非法押注.');
     }
     $anteName = $this->params['ante'];
     if (isset($this->params['type'])) {
         $anteType = intval($this->params['type']);
         if ($anteType == 1) {
             $anteRate = 1;
         } elseif ($anteType == 2) {
             $anteRate = 100;
         } else {
             $anteRate = ZConfig::getField('init', 'anteRate');
         }
     } else {
         $anteRate = ZConfig::getField('init', 'anteRate');
     }
     $currentGamecount = common\game::getRuncount();
     //check coin
     $userInfo = $this->userModel->getUserById($this->uid);
     $leftCoin = $userInfo['coin'] - $anteRate;
     if ($leftCoin < 0) {
         throw new common\error('押注不够.');
     }
     //        $this->userModel->updUserById($this->uid, array('coin' => $leftCoin));
     $returnDate = 0;
     if (!($userAnte = $this->useranteModel->getAnteByUidGamecount($this->uid, $currentGamecount))) {
         $_d = array('uid' => $this->uid, $anteName => $anteRate, 'gameCount' => $currentGamecount, 'created' => time());
         $this->useranteModel->addAnte($_d);
         $returnDate = $anteRate;
     } else {
         $val = $userAnte[$anteName] + $anteRate;
         if ($val > 999) {
             throw new common\error('最大押注为999.');
         }
         //update
         $_d = array($anteName => $val);
         $this->useranteModel->updAnteById($userAnte['id'], $_d, $userAnte);
         $returnDate = $_d[$anteName];
     }
     $this->userModel->updUserById($this->uid, array('coin' => $leftCoin));
     $this->data = array($anteName => $returnDate, 'coin' => $leftCoin);
     common\game::sendCurrentAnte();
 }
示例#14
0
文件: Json.php 项目: 446244451/zphp
 public function sendMaster(array $_params = null)
 {
     if (!empty($_params)) {
         $this->_data = $this->_data + $_params;
     }
     $host = Config::getField('socket', 'host');
     $port = Config::getField('socket', 'port');
     $client = new ZSClient($host, $port);
     $client->send($this->getData());
 }
示例#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
文件: main.php 项目: jxw7733/zphpdemo
 public function main()
 {
     $project = Config::getField('project', 'name', 'zphp');
     $data = $project . " runing!\n";
     $params = $this->_server->getParams();
     if (!empty($params)) {
         foreach ($params as $key => $val) {
             $data .= "key:{$key}=>{$val}\n";
         }
     }
     return $data;
 }
示例#17
0
 private function route($data, $fd)
 {
     if (empty($this->_route)) {
         $this->_route = Route::getInstance(ZConfig::getField('socket', 'call_mode', 'ZPHP'));
     }
     try {
         return $this->_route->run($data, $fd);
     } catch (\Exception $e) {
         //$result =  Formater::exception($e);
         return null;
     }
 }
示例#18
0
 public function display($model)
 {
     if (empty($this->_view_mode)) {
         $viewMode = Config::getField('project', 'view_mode', 'String');
     } else {
         $viewMode = $this->_view_mode;
     }
     $this->_view_mode = '';
     $view = View\Factory::getInstance($viewMode);
     $view->setModel($model);
     $view->display();
 }
示例#19
0
 public function main()
 {
     $project = Config::getField('project', 'name', 'zphp');
     $data = $project . " runing!\n";
     $params = Request::getParams();
     if (!empty($params)) {
         foreach ($params as $key => $val) {
             $data .= "key:{$key}=>{$val}\n";
         }
     }
     return ['data' => ['data' => ['zphp' => $data]]];
 }
示例#20
0
 public function display()
 {
     $tplPath = ZPHP\Core\Config::getField('project', 'tpl_path', 'template' . DS . 'template');
     $fileName = ZPHP\ZPHP::getRootPath() . DS . $tplPath . DS . $this->tplFile;
     if (!\is_file($fileName)) {
         throw new \Exception("no file {$fileName}");
     }
     if (!empty($this->model)) {
         \extract($this->model);
     }
     include "{$fileName}";
 }
示例#21
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);
 }
示例#22
0
 /**
  * @param $level
  * @param $message
  * @param array $context
  * @return bool
  * @throws \Exception
  * @desc {type} | {timeStamp} |{dateTime} | {$message}
  */
 public function log($level, $message, array $context = array())
 {
     $logLevel = ZConfig::getField('project', 'log_level', Level::ALL);
     if (Level::$levels[$level] & $logLevel) {
         $str = $level . self::SEPARATOR . $message . self::SEPARATOR . \implode(self::SEPARATOR, array_map('\\ZPHP\\Common\\Log::myJson', $context));
         if (!$this->_client) {
             $this->_client = new WebSocketClient($this->_config['host'], $this->_config['port']);
             $this->_client->connect();
         }
         $this->_client->send($str);
     }
     return false;
 }
示例#23
0
文件: File.php 项目: jxw7733/zphp
 /**
  * @param $level
  * @param $message
  * @param array $context
  * @return bool
  * @throws \Exception
  * @desc {type} | {timeStamp} |{dateTime} | {$message}
  */
 public function log($level, $message, array $context = array())
 {
     $logLevel = ZConfig::getField('project', 'log_level', Level::ALL);
     if (Level::$levels[$level] & $logLevel) {
         $str = $level . self::SEPARATOR . $message . self::SEPARATOR . \implode(self::SEPARATOR, array_map('\\ZPHP\\Common\\Log::myJson', $context));
         if ($this->_config['type_file']) {
             $logFile = $this->_config['dir'] . \DS . $level . '.' . $this->_config['suffix'];
         } else {
             $logFile = $this->_config['dir'] . \DS . ZConfig::getField('project', 'project_name', 'log') . '.' . $this->_config['suffix'];
         }
         \file_put_contents($logFile, $str . "\n", FILE_APPEND | LOCK_EX);
     }
     return false;
 }
示例#24
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);
 }
示例#25
0
 /**
  * 直接 parse $_REQUEST
  * @param $_data
  * @return bool
  */
 public function parse($_data)
 {
     $data = $_data;
     $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];
     }
     $this->_params = $data;
     return true;
 }
示例#26
0
文件: Rpc.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];
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Rpc'));
     return true;
 }
示例#27
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();
 }
示例#28
0
 public static function makeUrl($action, $method, $params = array())
 {
     $appUrl = $_SERVER['HTTP_HOST'];
     $actionName = 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;
         }
     }
     return $appUrl . "?{$actionName}={$action}&{$methodName}={$method}&" . http_build_query($params);
 }
示例#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
文件: Factory.php 项目: xifat/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'];
             }
         }
         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_name(ZConfig::getField('project', 'session_name', 'ZPHPSESSID'));
         \session_start();
         self::$isStart = true;
     }
 }