php ZPHP-Core-Config类(方法)实例源码

下面列出了php ZPHP-Core-Config 类(方法)源码代码实例,从而了解它的用法。

作者:xifa    项目:zph   
public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         \header("Content-Type: application/json; charset=utf-8");
     }
     echo \json_encode($this->model);
 }

作者:heese    项目:zph   
/**
  * 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;
 }

作者:jix    项目:zph   
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;
     }
 }

作者:google201    项目:swoole_flash_gam   
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);
 }

作者:jxw773    项目:zphpdem   
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();
 }

作者:heese    项目:zph   
/**
  * 直接 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;
 }

作者:google201    项目:swoole_flash_gam   
/**
  * 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");
 }

作者:xifa    项目:zph   
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);
 }

作者:xifa    项目:zph   
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);
 }

作者:heese    项目:zph   
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);
 }

作者:google201    项目:swoole_flash_gam   
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);
 }

作者:xifa    项目:zph   
public function display()
 {
     if (Config::get('server_mode') == 'Http') {
         \header("Content-Type:text/xml; charset=utf-8");
     }
     echo $this->xmlEncode();
 }

作者:google201    项目:swoole_flash_gam   
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'));
 }

作者:jxw773    项目:zphpdem   
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;
 }

作者:niceDreame    项目:zph   
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;
     }
 }

作者:google201    项目:swoole_flash_gam   
public static function getRankCache()
 {
     if (empty(self::$rankCache)) {
         $config = ZConfig::getField('cache', 'net');
         self::$rankCache = ZRank::getInstance($config['adapter'], $config);
     }
     return self::$rankCache;
 }

作者:google201    项目:swoole_flash_gam   
public static function getConnection()
 {
     if (empty(self::$connection)) {
         $config = ZConfig::get('connection');
         self::$connection = CFactory::getInstance($config['adapter'], $config);
     }
     return self::$connection;
 }

作者:ymnl00    项目:zphpdem   
public function _before()
 {
     $ehConfig = ZConfig::getField('project', 'exception_handler');
     if (!empty($ehConfig)) {
         \set_exception_handler($ehConfig);
     }
     return true;
 }

作者:google201    项目:swoole_flash_gam   
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();
 }

作者:44624445    项目:zph   
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();
 }


问题


面经


文章

微信
公众号

扫码关注公众号