示例#1
0
 /**
  * Controller层的调用入口函数,在scripts中调用
  */
 public function execute($action = NULL)
 {
     $startTime = getMicrotime();
     try {
         $error_handler = set_error_handler("ErrorHandler");
         $objRequest = new HttpRequest();
         $objResponse = new HttpResponse();
         // 指定action
         if ($action != NULL) {
             $objRequest->setAction($action);
         }
         // 入力检查
         $this->check($objRequest, $objResponse);
         $isCacheValid = false;
         if ($this->_cache) {
             $objDBCache = new DBCache();
             $objDBCache->cache_dir = $this->_cache_dir;
             $isCacheValid = $objDBCache->isValid($this->_cache_id, $this->_cache_time, $this->_renew_cachedir);
         }
         // if(!ob_start("ob_gzhandler"))
         ob_start();
         if (!$isCacheValid) {
             // 执行方法
             $this->service($objRequest, $objResponse);
             if ($this->displayDisabled == false) {
                 $this->display($objResponse, $this->compiler);
                 if ($this->_cache) {
                     $objDBCache->cachePage($this->_cache_id, json_encode(ob_get_contents()), $this->_renew_cachedir);
                 }
                 if ($this->_create_html) {
                     File::creatFile($this->_html_name, ob_get_contents(), $this->_html_dir);
                 }
             }
         } else {
             echo json_decode($objDBCache->fetch($this->_cache_id, false, $this->_renew_cachedir));
         }
         ob_implicit_flush(1);
         ob_end_flush();
         // 资源回收
         $this->release($objRequest, $objResponse);
         // 数据库事务提交(由DBQuery判断是否需要做)
         // DBQuery::instance()->commit();
     } catch (Exception $e) {
         if (__Debug) {
             echo 'error: ' . $e->getMessage() . "<br>";
             echo str_replace("\n", "\n<br>", $e->getTraceAsString());
         }
         try {
             // 错误处理
             $this->tryexecute($objRequest, $objResponse);
             // 数据库事务回滚(由DBQuery判断是否需要做)
             // DBQuery::instance()->rollback();
         } catch (Exception $e) {
             logError($e->getMessage(), __MODEL_EXCEPTION);
             if (__Debug) {
                 print_r($e->getMessage());
                 print_r($e->getTraceAsString());
             }
         }
         // 错误日志
         logError($e->getMessage(), __MODEL_EXCEPTION);
         logError($e->getTraceAsString(), __MODEL_EMPTY);
         // 重定向到错误页面
         // redirect("errorpage.htm");
         // 最终处理
         $this->finalexecute($objRequest, $objResponse);
         // set_exception_handler('exception_handler');
         if ($this->showErrorPage && __Debug == false) {
             redirect(__WEB . "404.htm");
         }
     }
     // debug...
     if (__Debug) {
         $endTime = getMicrotime();
         $useTime = $endTime - $startTime;
         logDebug("excute time {$useTime} s");
     }
 }