Exemple #1
0
 /**
  * 获取当前实例
  * @access public
  * @return XF_Controller_Plugin_Manage
  */
 public static function getInstance()
 {
     if (self::$_instance === null) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Exemple #2
0
 /**
  * 转发控制器
  * @param XF_Controller_Request_Abstract $request
  * @param bool $runRouter 重新运行路由解析 默认为true
  * @throws XF_Controller_Exception
  */
 private function _dispatch(XF_Controller_Request_Abstract $request = null, $runRouter = true)
 {
     $this->_dispath_count++;
     if ($request != null) {
         $this->_request = $request;
     }
     if ($this->_dispath_count == 1) {
         $this->_plugin_manage->routeStartup($this->_request);
         $this->_router->run();
         $this->_plugin_manage->routeShutdown($this->_request);
     } elseif ($this->_dispath_count >= 5) {
         XF_Functions::writeErrLog('URI:' . $this->_request->getRequestUrl() . ' - loop forever dispatch controller');
         throw new XF_Exception('loop forever dispatch controller');
     } else {
         if ($runRouter == true) {
             $this->_router->run();
         }
     }
     //没有找到正确的模块
     if ($this->_request->getModule() == 'unknown') {
         throw new XF_Exception('404 Not found!', 404);
     }
     //加载控制器
     $this->getModuleDir();
     $controllerName = NULL;
     if (strtolower($this->_request->getModule()) != 'default') {
         $controllerName = ucfirst($this->_request->getModule()) . '_';
     }
     $this->_controller_dir = $this->_module_dir . '/controllers';
     $controllerFile = $this->_controller_dir . '/' . ucfirst($this->_request->getController()) . 'Controller.php';
     if (is_file($controllerFile)) {
         require_once $controllerFile;
         $controllerName .= ucfirst($this->_request->getController()) . 'Controller';
         if (class_exists($controllerName, FALSE)) {
             $this->_plugin_manage->preDispatch($this->_request);
             $this->_controller_instance = new $controllerName();
             $this->_plugin_manage->postDispatch($this->_request);
         }
     }
     if ($this->_controller_instance == null) {
         throw new XF_Exception('404 Not found!', 404);
     } elseif ($this->_controller_instance instanceof XF_Controller_Interface) {
         $this->_controller_instance->doAction();
     } else {
         throw new XF_Exception('404 Not found!', 404);
     }
 }
Exemple #3
0
 /**
  * 渲染模板
  * @access public
  * @param string $template_file Action模板文件
  * @param string $cache_sign 缓存标识
  * @param XF_View_Layout_Abstract $layout 
  * @return string
  */
 public function render($template_file = null, $cache_sign = '', XF_View_Layout_Abstract $layout = null)
 {
     $this->getTemplateStartLocation();
     $request = XF_Controller_Request_Http::getInstance();
     $appName = $request->getModule();
     $controllerName = $request->getController();
     $actionName = $request->getAction();
     //如果没有设置模板,则自动获取当前Action名称相关的模板
     if ($template_file == null) {
         $template_file = $this->_template_folder . '/' . $controllerName . '/' . $actionName . '.php';
     }
     if (!is_file($template_file)) {
         throw new XF_View_Exception('Action template not found');
     }
     $content = $this->obGetContents($template_file);
     XF_Controller_Plugin_Manage::getInstance()->postRender($content);
     //是否需要缓存?
     if ($this->_cache_time > 0) {
         $layout_tag = '';
         if ($layout != null) {
             $layout_tag = '<!--Layout:' . get_class($layout) . ',' . $layout->getCacheTime() . ',' . (int) $layout->getCacheType() . '-->';
         }
         $_content = $content . $layout_tag . $this->_makeCacheHeadTitleAndHeadMeta();
         //写入缓存
         if ($this->_cache_instance instanceof XF_Cache_SECache) {
             XF_File::mkdirs(TEMP_PATH . '/Cache');
             $this->_cache_instance->setCacheSaveFile(TEMP_PATH . '/Cache/ActionViewCache');
         }
         $this->_cache_instance->setCacheTime($this->_cache_time);
         $this->_cache_instance->add($cache_sign, $_content);
     }
     //是否启用布局
     if ($layout != null) {
         $layout->assign('$layoutContent', $content);
         $content = $layout->render();
     }
     return $content;
 }
Exemple #4
0
 /**
  * 执行控制器动作
  * @access public
  * @return void
  */
 public function doAction()
 {
     $plugins = XF_Controller_Plugin_Manage::getInstance();
     //当前Action是否存在文件缓存
     if ($content = $this->_checkCacheContent()) {
         echo $content;
         return;
     }
     $this->_checkControllerInstance();
     $actionName = $this->_request->getAction();
     $valiMethod = 'validate' . ucfirst($actionName);
     $method = $actionName . 'Action';
     $methods = get_class_methods($this->_controller);
     //是否存在Action验证方法
     if ($this->hasAction($valiMethod)) {
         if (call_user_func(array($this->_controller, $valiMethod)) !== true) {
             $plugins->exception404($this->_request);
             return;
         }
     }
     //是否存在要执行的Action
     if ($this->hasAction($method)) {
         $plugins->preAction($this->_request);
         call_user_func(array($this->_controller, $method));
         $plugins->postAction($this->_request);
         $this->_render();
         $plugins->postOutput();
         return;
     }
     $plugins->exception404($this->_request);
 }
Exemple #5
0
 /**
  * 在当前类中获取指定文件在缓冲区的内容
  * @access private
  * @param string $file
  * @return string
  */
 private function _getObContent($file)
 {
     if (!is_file($file)) {
         return '';
     }
     $content = '';
     ob_start();
     require_once $file;
     $content = ob_get_contents();
     ob_end_clean();
     //清除BOM
     $charset[1] = substr($content, 0, 1);
     $charset[2] = substr($content, 1, 1);
     $charset[3] = substr($content, 2, 1);
     if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
         $content = substr($content, 3);
     }
     XF_Controller_Plugin_Manage::getInstance()->postRender($content);
     return $content;
 }
Exemple #6
0
 /**
  * 普通错误,警告
  * @param int $errno
  * @param string $errstr
  * @param string $errfile
  * @param int $errline
  * @throws XF_Exception
  */
 public static function appError($errno, $errstr, $errfile, $errline)
 {
     $env = XF_Config::getInstance()->getEnvironmental();
     $req = XF_Controller_Request_Http::getInstance();
     $url = $req->getMethod() . ' "' . $req->getRequestUrl() . '"';
     switch ($errno) {
         case E_ERROR:
         case E_PARSE:
         case E_CORE_ERROR:
         case E_COMPILE_ERROR:
         case E_USER_ERROR:
             $message = $url . "\n" . 'ERROR: ' . $errstr . ' in ' . $errfile . ' on line ' . $errline;
             if ($env == 'development') {
                 exit($message);
             } else {
                 XF_Functions::writeErrLog($message);
                 XF_Controller_Plugin_Manage::getInstance()->exception(XF_Controller_Request_Http::getInstance(), new XF_Exception($e['message']));
             }
             break;
             /*case E_STRICT:
                 case E_USER_WARNING:
                 case E_USER_NOTICE:
                 default:
             	    $message = 'ERROR: ['.$errno.']'.$errstr. ' in '.$errfile.' on line '.$errline;
             		XF_Functions::writeErrLog($message);
                     if ($env == 'development')
                     {
                       echo '<br/>'.$message;
                     }
             	    break;*/
     }
 }