/** * 渲染模板 * @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; }
/** * 初始化相应必要的对象 * @access public * @return XF_Controller_Front */ public function init() { if ($this->_router == NULL) { $this->_router = new XF_Controller_Router(); $this->_plugin_manage = XF_Controller_Plugin_Manage::getInstance(); $this->_request = XF_Controller_Request_Http::getInstance(); } return $this; }
/** * 在当前类中获取指定文件在缓冲区的内容 * @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; }
/** * 执行控制器动作 * @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); }
/** * 普通错误,警告 * @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;*/ } }