Ejemplo n.º 1
0
/**
 * yangzie入口
 * 开始处理请求,如果没有指定uri,默认处理当前的uri请求, 
 * 如果没有指定method,则以请求的方法为主(get post put delete)
 *
 * @param string $uri            
 * @param string $method            
 * @param bool $return true则return,false直接输出
 * @return string
 */
function yze_go($uri = null, $method = null, $return = null, $request_method = null)
{
    $output_view = function ($request, $controller, $response, $return) {
        $layout = new YZE_Layout($controller->get_layout(), $response, $controller);
        $output = $layout->get_output();
        $request->remove();
        $controller->cleanup();
        if ($return) {
            return $output;
        }
        echo $output;
        exit;
    };
    $output_header = function ($request, $controller, $response, $return) {
        $output = $response->output($return);
        if ($return) {
            $controller->cleanup();
            $request->remove();
            return $output;
        }
        $controller->cleanup();
        $request->remove();
        if ($output) {
            header("Location: {$output}");
        }
        exit;
    };
    try {
        $request = YZE_Request::get_instance();
        $session = YZE_Session_Context::get_instance();
        $dba = YZE_DBAImpl::getDBA();
        $format = null;
        //之前已经有请求了,则copy一个新请求
        if ($request->has_request()) {
            $old_uri = $request->the_uri();
            $format = $request->get_output_format();
            $request = $request->copy();
        }
        $request->init($uri, $method, $format, $request_method);
        // 初始化请求上下文环境,请求入栈
        $controller = $request->controller();
        // 如果yze_go 是从一个控制器的处理中再次调用的,则为新的控制器copy一个上下文环境
        if (@$old_uri) {
            $session->copy($old_uri, $request->the_uri());
        }
        $action = "YZE_ACTION_BEFORE_" . ($request->is_get() ? "GET" : "POST");
        \yangzie\YZE_Hook::do_hook(constant($action), $controller);
        $request->auth();
        $dba->beginTransaction();
        $response = $request->dispatch();
        $dba->commit();
        // content output
        if (is_a($response, "\\yangzie\\YZE_View_Adapter")) {
            return $output_view($request, $controller, $response, $return);
        }
        //header output
        return $output_header($request, $controller, $response, $return);
    } catch (\Exception $e) {
        $controller = $request->controller();
        //嵌套调用的,把异常往外层抛
        //是请求的控制器自己处理异常好,还是把异常一直抛出到顶级请求来处理好?
        if (!$request->is_top_request()) {
            $request->remove();
            throw $e;
        }
        try {
            $dba->rollback();
            if (!@$controller || is_a($e, "\\yangzie\\YZE_Suspend_Exception")) {
                $controller = new YZE_Exception_Controller();
            }
            $response = $controller->do_exception($e);
            if (!$response) {
                $errController = new YZE_Exception_Controller();
                $response = $errController->do_exception($e);
            }
            $filter_data = \yangzie\YZE_Hook::do_hook(YZE_FILTER_YZE_EXCEPTION, array("exception" => $e, "controller" => $controller, "response" => $response));
            $response = $filter_data['response'];
            // content output
            if (is_a($response, "\\yangzie\\YZE_View_Adapter")) {
                return $output_view($request, $controller, $response, $return);
            }
            //header output
            return $output_header($request, $controller, $response, $return);
        } catch (\Exception $notCatch) {
            $controller = new YZE_Exception_Controller();
            $controller->do_exception(new YZE_RuntimeException($notCatch->getMessage()))->output();
            $request->remove();
        }
    }
}
Ejemplo n.º 2
0
/**
 * 返回当前控制器的出错信息
 *
 * @author leeboo
 *        
 * @param $begin_tag 每条错误消息的开始html标签
 * @param $end_tag 每条错误消息的结束html标签
 * 
 * @return string
 *
 */
function yze_controller_error($begin_tag = null, $end_tag = null)
{
    $session = YZE_Session_Context::get_instance();
    if ($exception = YZE_Request::get_instance()->getException()) {
        return $begin_tag . $exception->getMessage() . $end_tag;
    }
}