Esempio n. 1
0
 /**
  * 根据运行时上下文对象,调用相应的控制器动作方法
  *
  * @param array $args
  *
  * @return mixed
  */
 function run(array $args = array())
 {
     $context = QContext::instance();
     $udi = $context->requestUDI('array');
     $dir = dirname(__FILE__) . '/app/controller';
     $class_name = 'controller_';
     $controller_name = strtolower($udi[QContext::UDI_CONTROLLER]);
     $class_name .= $controller_name;
     $filename = "{$controller_name}_controller.php";
     // 载入控制器文件
     if (!class_exists($class_name, false)) {
         Q::loadClassFile($filename, array($dir), $class_name);
     }
     // 构造控制器对象
     $controller = new $class_name($this);
     $action_name = $udi[QContext::UDI_ACTION];
     $response = $controller->execute($action_name, $args);
     if (is_object($response) && method_exists($response, 'execute')) {
         // 如果返回结果是一个对象,并且该对象有 execute() 方法,则调用
         $response = $response->execute();
     } elseif ($response instanceof QController_Forward) {
         // 如果是一个 QController_Forward 对象,则将请求进行转发
         $response = $this->run($response->args);
     }
     // 其他情况则返回执行结果
     return $response;
 }
Esempio n. 2
0
 /**
  * 根据运行时上下文对象,调用相应的控制器动作方法
  *
  * @param array $args
  *
  * @return mixed
  */
 function dispatching(array $args = array())
 {
     // 构造运行时上下文对象
     $context = QContext::instance();
     // 获得请求对应的 UDI(统一目的地信息)
     $udi = $context->requestUDI('array');
     #IFDEF DEBUG
     QLog::log('REQUEST UDI: ' . $context->UDIString($udi), QLog::DEBUG);
     #ENDIF
     // 检查是否有权限访问
     if (!$this->authorizedUDI($this->currentUserRoles(), $udi)) {
         // 拒绝访问
         $response = $this->_on_access_denied();
     } else {
         // 确定控制器的类名称
         // 控制器类名称 = 模块名_Controller_名字空间_控制器名
         $module_name = $udi[QContext::UDI_MODULE];
         if ($module_name != QContext::UDI_DEFAULT_MODULE && $module_name) {
             $dir = "{$this->_app_config['MODULE_DIR']}/{$module_name}/controller";
             $class_name = "{$module_name}_controller_";
         } else {
             $dir = "{$this->_app_config['APP_DIR']}/controller";
             $class_name = 'controller_';
         }
         $ns = $udi[QContext::UDI_NAMESPACE];
         if ($ns != QContext::UDI_DEFAULT_NAMESPACE && $ns) {
             $class_name .= "{$ns}_";
             $dir .= "/{$ns}";
         }
         $controller_name = $udi[QContext::UDI_CONTROLLER];
         $class_name .= $controller_name;
         $filename = "{$controller_name}_controller.php";
         do {
             // 载入控制器文件
             try {
                 if (!class_exists($class_name, false)) {
                     Q::loadClassFile($filename, array($dir), $class_name);
                 }
             } catch (Q_ClassNotDefinedException $ex) {
                 $response = $this->_on_action_not_defined();
                 break;
             } catch (Q_FileNotFoundException $ex) {
                 $response = $this->_on_action_not_defined();
                 break;
             }
             // 构造控制器对象
             $controller = new $class_name($this);
             $action_name = $udi[QContext::UDI_ACTION];
             if ($controller->existsAction($action_name)) {
                 // 如果指定动作存在,则调用
                 $response = $controller->execute($action_name, $args);
             } else {
                 // 如果指定动作不存在,则尝试调用控制器的 _on_action_not_defined() 函数处理错误
                 $response = $controller->_on_action_not_defined($action_name);
                 if (is_null($response)) {
                     // 如果控制器的 _on_action_not_defined() 函数没有返回处理结果
                     // 则由应用程序对象的 _on_action_not_defined() 函数处理
                     $response = $this->_on_action_not_defined();
                 }
             }
         } while (false);
     }
     if (is_object($response) && method_exists($response, 'execute')) {
         // 如果返回结果是一个对象,并且该对象有 execute() 方法,则调用
         $response = $response->execute();
     } elseif ($response instanceof QController_Forward) {
         // 如果是一个 QController_Forward 对象,则将请求进行转发
         $response = $this->dispatching($response->args);
     }
     // 其他情况则返回执行结果
     return $response;
 }
Esempio n. 3
0
 /**
  * 根据运行时上下文对象,调用相应的控制器动作方法
  *
  * @param array $args
  *
  * @return mixed
  */
 function dispatching(array $args = array())
 {
     header("Content-type: text/html; charset=utf-8");
     // 从 session 中提取 flash message
     if (isset($_SESSION)) {
         $message = isset($_SESSION[$this->_flash_message_key]) ? $_SESSION[$this->_flash_message_key] : null;
         $this->_flash_message = $message;
         unset($_SESSION[$this->_flash_message_key]);
     }
     // 构造运行时上下文对象
     $context = QContext::instance();
     // 获得请求对应的 UDI(统一目的地信息)
     $udi = $context->requestUDI('array');
     #dump($udi);
     #IFDEF DEBUG
     QLog::log('REQUEST UDI: ' . $context->UDIString($udi), QLog::DEBUG);
     #ENDIF
     // 检查是否有权限访问
     if (false) {
         // 拒绝访问
         $response = $this->_on_access_denied();
     } else {
         // 确定控制器的类名称
         $module_name = $udi[QContext::UDI_MODULE];
         $controller_name = $udi[QContext::UDI_CONTROLLER];
         $dir = "{$this->_app_config['APP_DIR']}/controller/{$module_name}";
         $class_name = sprintf("Controller_%s_%s", ucwords($module_name), ucwords($controller_name));
         $filename = "{$controller_name}_controller.php";
         do {
             if (!class_exists($class_name, false)) {
                 Q::loadClassFile($filename, array($dir), $class_name);
             }
             // 构造控制器对象
             $controller = new $class_name($this);
             $action_name = $udi[QContext::UDI_ACTION];
             if ($controller->existsAction($action_name)) {
                 // 如果指定动作存在,则调用
                 $response = $controller->execute($action_name, $args);
                 // 更新 flash message
             } else {
                 // 如果指定动作不存在,则尝试调用控制器的 _on_action_not_defined() 函数处理错误
                 $response = $controller->_on_action_not_defined($action_name);
                 if (is_null($response)) {
                     // 如果控制器的 _on_action_not_defined() 函数没有返回处理结果
                     // 则由应用程序对象的 _on_action_not_defined() 函数处理
                     $response = $this->_on_action_not_defined();
                 }
             }
         } while (false);
     }
     if (is_object($response) && method_exists($response, 'execute')) {
         // 如果返回结果是一个对象,并且该对象有 execute() 方法,则调用
         $response = $response->execute();
     } elseif ($response instanceof QController_Forward) {
         // 更新 flash message
         if (isset($_SESSION)) {
             unset($_SESSION[$this->_flash_message_key]);
         }
         // 如果是一个 QController_Forward 对象,则将请求进行转发
         $response = $this->dispatching($response->args);
     }
     // 其他情况则返回执行结果
     return $response;
 }
Esempio n. 4
0
 *
 * @{
 */
// {{{ includes
do {
    if (class_exists('Smarty', false)) {
        break;
    }
    $view_config = (array) Q::getIni('view_config');
    if (empty($view_config['smarty_dir']) && !defined('SMARTY_DIR')) {
        throw new QView_Exception(__('Application settings "view_config[\'smarty_dir\']" ' . 'and constant SMARTY_DIR must be defined for QView_Adapter_Smarty.'));
    }
    if (empty($view_config['smarty_dir'])) {
        $view_config['smarty_dir'] = SMARTY_DIR;
    }
    Q::loadClassFile('Smarty.class.php', $view_config['smarty_dir'], 'Smarty');
} while (false);
// }}}
/**
 * QView_Adapter_Smarty 提供了对 Smarty 模板引擎的支持
 */
class QView_Adapter_Smarty extends QView_Adapter_Abstract
{
    public $tpl_file_ext = '.html';
    /**
     * Smarty 对象
     *
     * @var Smarty
     */
    public $smarty;
    function __construct(QContext $context)
Esempio n. 5
0
 /**
  * 执行指定的 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;
 }