/** * 渲染指定的视图文件 * * 渲染时,视图要使用的数据保存在控件的 $view 属性中。 * * @param string $viewname * @param boolean $return * * @return string */ protected function _renderBlock($viewname, $return = false) { if (!is_object($this->_view_adapter)) { $adapter_class = is_null($this->_view_adapter_class) ? $this->context->getIni('view_adapter') : $this->_view_adapter_class; $adapter_obj_id = "webcontrols_{$adapter_class}"; if (Q::isRegistered($adapter_obj_id)) { /** * @var QView_Adapter_Abstract */ $adapter = Q::registry($adapter_obj_id); } else { /** * @var QView_Adapter_Abstract */ $adapter = new $adapter_class($this->context); Q::register($adapter, $adapter_obj_id); } } else { $adapter = $this->_view_adapter; } $adapter->clear(); $adapter->assign($this->view); $adapter->assign('_ctx', $this->context); $filename = QView::getControlViewFilename($this->context, $adapter, $viewname, $this->_controls_view_dir); if ($return) { return $adapter->fetch($filename); } else { return $adapter->display($filename); } }
/** * 输出一个元素 */ function element($element) { $dir = QView::getExtraViewDir($this->context, '_elements', 'elements_view_dir'); $filename = "{$element}_element{$this->tpl_file_ext}"; if ($this->context->getIni('view_config/flat_dir') > 0) { $filename = '_' . $filename; } $_ctx = $this->context; include "{$dir}/{$filename}"; }
/** * 执行指定的 Action 方法 * * @param QContext $context * @param array $args * * @return mixed */ protected function _executeAction(QContext $context, array $args = array()) { // 检查是否有权限访问 $controller_name = $context->controller_name; $action_name = $context->action_name; $namespace = $context->namespace; $module = $context->module_name; QLog::log(sprintf('Execute controller action: "%s".', $context->getRequestUDI())); if (!$this->checkAuthorized($controller_name, $action_name, $namespace, $module)) { $response = call_user_func($context->getIni('dispatcher_on_access_denied'), $context); } else { // 尝试载入控制器 $class_name = $context->getIni('controller_class_prefix') . 'Controller_'; if ($namespace) { $class_name .= ucfirst($namespace) . '_'; } $class_name .= ucfirst(str_replace('_', '', $controller_name)); $app_config = self::getAppConfig($this->_appid); if ($module) { $dir = $app_config['ROOT_DIR'] . "/modules/{$module}/controller"; } else { $dir = $app_config['ROOT_DIR'] . "/app/controller"; } if ($namespace) { $dir .= DS . $namespace; } // 构造控制器对象 try { $filename = $controller_name . '_controller.php'; Q::loadClassFile($filename, array($dir), $class_name); } catch (QException $ex) { $response = call_user_func($this->context->getIni('dispatcher_on_action_not_found'), $context); if (is_null($response)) { $response = ''; } } if (!isset($response)) { $controller = new $class_name($this, $context); /* @var $controller QController_Abstract */ if ($context->isAJAX()) { $controller->view = null; } $response = $controller->_execute($args); } } if (is_object($response) && method_exists($response, 'execute')) { $response = $response->execute(); } elseif ($response instanceof QController_Forward) { // 更新 flash message $key = $this->context->getIni('app_flash_message_key'); unset($_SESSION[$key]); $response = $this->_executeAction($response->context, $response->args); } return $response; }
/** * 确定视图名字 * * 继承类可以覆盖此方法来改变视图名称确认规则。 * * @return string */ protected function _getViewname() { $viewname = !empty($this->viewname) ? $this->viewname : $this->context->action_name; if ($viewname[0] == '/') { return $viewname; } $arr = $this->context->destinfo($viewname); if ($this->context->getIni('view_config/flat_dir')) { return "{$this->context->controller_name}_{$viewname}"; } else { return "{$this->context->controller_name}/{$viewname}"; } }
/** * 获取当前模块的配置内容 * * $option 参数指定要获取的设置名。如果当前模板的设置中没有该选项,则尝试从应用程序的全局设置中读取。 * 如果全局设置中还是找不到指定的选项,则返回由 $default 参数指定的值。 * * @code * $option_value = $context->getIni('my_option'); * @endcode * * 对于层次化的配置信息(类似 PHP 的嵌套数组),可以通过在 $option 中使用“/”符号来指定。 * * 例如有一个名为 option_group 的设置项,其中包含三个子项目。现在要查询其中的 my_option 设置项的内容。 * * @code * // +--- option_group * // +-- my_option = this is my_option * // +-- my_option2 = this is my_option2 * // \-- my_option3 = this is my_option3 * * // 查询 option_group 设置组里面的 my_option 项 * $option_value = $context->getIni('option_group/my_option'); * * // 将会显示 this is my_option * echo $option_value; * * @endcode * * 要读取更深层次的设置项,可以使用更多的“/”符号。但太多层次会导致读取速度变慢。 * * 如果要获得当前模块所有设置项的内容,将 $option 参数指定为 '/' 即可: * * @code * // 获取所有设置项的内容 * $all = $context->getIni('/'); * @endcode * * @param string $option * 要获取设置项的名称 * @param mixed $default * 当设置不存在时要返回的设置默认值 * @param boolean $backtracking * 在当前模块中找不到指定设置时是否查询父级 QContext 对象 * * @return mixed * 返回设置项的值 */ function getIni($option, $default = null, $backtracking = true) { if ($option == '/') { return $this->_config; } if (strpos($option, '/') === false) { if (array_key_exists($option, $this->_config)) { return $this->_config[$option]; } if (!$backtracking || is_null($this->_parent)) { return $default; } return $this->_parent->getIni($option, $default); } $parts = explode('/', $option); $pos =& $this->_config; foreach ($parts as $part) { if (!isset($pos[$part])) { return $default; } $pos =& $pos[$part]; } return $pos; }
/** * 构造函数 * * @param QContext $context */ function __construct(QContext $context) { $this->context = $context; $this->_rules = (array) $context->getIni('routes'); //$this->_log = new QLog(array('log_writer_filename' => 'router.log')); }
/** * 辅助方法,用于获得一些特定目录的路径 * * @return string */ static function getExtraViewDir(QContext $context, $suffix, $ini_name, $ini_default = null) { if ($ini_default) { return $ini_default; } $dir = $context->getIni('view_config/' . $ini_name); if ($dir) { return $dir; } $dir = self::getViewDir($context, $ini_default); if (intval($context->getIni('view_config/flat_dir')) < 1) { $dir = $dir . '/' . $suffix; } return $dir; }
/** * 渲染表单 * * @param QContext $context * @param boolean $return * * @return string */ function render(QContext $context, $return = false) { $this->_event(self::BEFORE_RENDER_FORM, $this); if (!is_object($this->_view_adapter)) { $adapter_class = is_null($this->_view_adapter_class) ? $context->getIni('view_adapter') : $this->_view_adapter_class; $adapter_obj_id = "form_{$adapter_class}"; if (Q::isRegistered($adapter_obj_id)) { $adapter = Q::registry($adapter_obj_id); } else { $adapter = new $adapter_class($context); Q::register($adapter, $adapter_obj_id); } } else { $adapter = $this->_view_adapter; } /* @var $adapter QView_Adapter_Abstract */ $viewname = !empty($this->_viewname) ? $this->_viewname : 'form'; $filename = QView::getViewLayoutsFilename($adapter->context, $adapter, $viewname); $adapter->clear(); $adapter->assign('form', $this); $adapter->assign('_ctx', $context); $output = $adapter->fetch($filename); $this->_event(self::AFTER_RENDER_FORM, $this); if (!$return) { echo $output; $output = null; } return $output; }