Exemplo n.º 1
0
 /**
  * Dispatch an HTTP request to a controller/action.
  *
  * @param Yaf_Request_Abstract|null $request
  *
  * @return void Yaf_Response_Abstract
  */
 public function dispatch()
 {
     $request = $this->getRequest();
     if (!$request instanceof Yaf_Request_Abstract) {
         throw new Yaf_Exception('Expect a Yaf_Request_Abstract instance');
     }
     if ($request instanceof Yaf_Request_Http) {
         $response = new Yaf_Response_Http();
     } elseif ($request instanceof Yaf_Request_Cli) {
         $response = new Yaf_Response_Cli();
     }
     // 选择路由
     $router = $this->getRouter();
     foreach ($this->_plugins as $plugin) {
         $plugin->routerStartup($request, $response);
     }
     $router->route($request);
     $this->_fixDefault($request);
     foreach ($this->_plugins as $plugin) {
         $plugin->routerShutdown($request, $response);
     }
     // 执行Action
     try {
         $view = $this->initView();
         foreach ($this->_plugins as $plugin) {
             $plugin->dispatchLoopStartup($request, $response);
         }
         $nested = Yaf_G::getConf('forward_limit');
         $nested = empty($nested) ? 5 : $nested;
         do {
             foreach ($this->_plugins as $plugin) {
                 $plugin->preDispatch($request, $response, $view);
             }
             $this->handle($request, $response, $view);
             $this->_fixDefault($request);
             foreach ($this->_plugins as $plugin) {
                 $plugin->postDispatch($request, $response);
             }
             $nested--;
         } while (!$request->isDispatched() && $nested > 0);
         foreach ($this->_plugins as $plugin) {
             $plugin->dispatchLoopShutdown($request, $response);
         }
     } catch (Exception $oExp) {
         if (Yaf_G::isDebug() || $request->getMethod() == 'CLI') {
             if ($request->getMethod() == 'CLI') {
                 Yaf_Logger::error(Yaf_G::parseException($oExp));
                 echo Yaf_G::parseException($oExp);
             } else {
                 echo "<pre>";
                 echo Yaf_G::parseException($oExp);
                 echo "</pre>";
             }
         } else {
             $response->setResponseCode(404);
             $view->display('404.phtml');
         }
     }
     if ($nested == 0 && !$request->isDispatched()) {
         throw new Yaf_Exception('The max dispatch nesting ' . Yaf_G::getConf('forward_limit') . ' was reached');
     }
     if ($this->returnResponse() == false) {
         $response->response();
     }
     return $response;
 }
Exemplo n.º 2
0
 private static function curl($sDomain, $sRoute, $aParam, $method, $iCacheTime)
 {
     if ($iCacheTime > 0) {
         $sCacheKey = md5($sDomain . $sRoute . serialize($aParam) . $method);
         $aRet = Util_Common::getCache()->get($sCacheKey);
         if (!empty($aRet)) {
             return $aRet;
         }
     }
     $time1 = microtime(true);
     $aParam['_time'] = time();
     $aParam['_sign'] = md5($aParam['_time'] . Yaf_G::getConf('signkey', 'sdk'));
     $sHost = Yaf_G::getConf($sDomain, 'sdkdomain');
     if (!$sHost) {
         throw new Exception('配置[sdkdomain][' . $sDomain . ']未找到!');
         return false;
     }
     $sRoute = trim($sRoute, '/?& ');
     $sUrl = 'http://' . $sHost . '/' . $sRoute;
     if (!self::$_hCurl) {
         self::$_hCurl = curl_init();
     }
     curl_setopt(self::$_hCurl, CURLOPT_DNS_USE_GLOBAL_CACHE, true);
     curl_setopt(self::$_hCurl, CURLOPT_HEADER, false);
     curl_setopt(self::$_hCurl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt(self::$_hCurl, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt(self::$_hCurl, CURLOPT_CONNECTTIMEOUT, 30);
     curl_setopt(self::$_hCurl, CURLOPT_TIMEOUT, 30);
     if ($method == 'post') {
         curl_setopt(self::$_hCurl, CURLOPT_POST, 1);
         curl_setopt(self::$_hCurl, CURLOPT_POSTFIELDS, http_build_query($aParam));
     } else {
         $sUrl .= strpos($sUrl, '?') === false ? '?' : '&';
         $sUrl .= http_build_query($aParam);
         $aParam = null;
     }
     $aCookie = array();
     if (Yaf_G::getEnv() == 'beta') {
         $aCookie[] = 'fjdp_version=beta';
     }
     if (Yaf_G::isDebug()) {
         $aCookie[] = 'debug=2k9j38h#4';
     }
     if (!empty($aCookie)) {
         curl_setopt(self::$_hCurl, CURLOPT_COOKIE, join('; ', $aCookie));
     }
     curl_setopt(self::$_hCurl, CURLOPT_URL, $sUrl);
     $sData = curl_exec(self::$_hCurl);
     $aRet = array('status' => false, 'data' => '数据请求失败,返回为空!');
     if (!empty($sData)) {
         $aData = json_decode($sData, true);
         if (isset($aData['code'])) {
             //兼容mapi没有status
             if ($aData['code'] === 0) {
                 $aData['status'] = true;
             } else {
                 $aData['status'] = false;
                 //unset($aData['msg']);
             }
             if (isset($aData['data'])) {
                 $aData['data']['msg'] = $aData['msg'];
             } else {
                 $aData['data'] = array('msg' => $aData['msg']);
             }
             unset($aData['msg']);
             unset($aData['code']);
         }
         if (isset($aData['status']) && isset($aData['data'])) {
             $aRet = $aData;
         } else {
             $aRet = array('status' => false, 'data' => '数据请求错误:' . $sData);
         }
     }
     $oDebug = Util_Common::getDebug();
     if ($oDebug) {
         $time2 = microtime(true);
         $use_time = round(($time2 - $time1) * 1000, 2);
         self::$_iOptCnt++;
         self::$_iUseTime += $use_time;
         $oDebug->groupCollapsed('Api: ' . $sUrl . ' ' . $use_time . '毫秒');
         if (!empty($aParam)) {
             $oDebug->debug($aParam);
         }
         $oDebug->debug($aRet['data']);
         if (isset($aRet['debug'])) {
             foreach ($aRet['debug'] as $v) {
                 if (is_array($v)) {
                     $oDebug->add($v[0], $v[1]);
                 } else {
                     $oDebug->groupCollapsed($v);
                 }
             }
             $oDebug->groupEnd();
             unset($aRet['debug']);
         }
         $oDebug->groupEnd();
     }
     if ($iCacheTime > 0 && $aRet['status']) {
         Util_Common::getCache()->set($sCacheKey, $aRet, $iCacheTime);
     }
     return $aRet;
 }
Exemplo n.º 3
0
 public static function isDebug()
 {
     return Yaf_G::isDebug();
 }