Exemple #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();
        }
    }
}
Exemple #2
0
define("YZE_PUBLIC_HTML", YZE_INSTALL_PATH . "app" . DS . "public_html" . DS);
//应用代码路径
define("YZE_APP_CACHES_PATH", YZE_INSTALL_PATH . "app" . DS . "public_html" . DS . "caches" . DS);
//缓存存放路径
define("YZE_APP_INC", YZE_APP_PATH);
//应用代码目录名称
define("YZE_APP_MODULES_INC", YZE_APP_PATH . "modules/");
//应用代码目录名称
define("YZE_APP_VENDOR", YZE_APP_PATH . "vendor/");
define("YZE_APP_LAYOUTS_INC", YZE_APP_PATH . "vendor/layouts/");
define("YZE_APP_VIEWS_INC", YZE_APP_PATH . "vendor/views/");
define('YZE_REWRITE_MODE_PATH_INFO', 'yze_rewrite_mode_path_info');
define('YZE_REWRITE_MODE_REWRITE', 'yze_rewrite_mode_rewrite');
ini_set('include_path', get_include_path() . PS . dirname(dirname(dirname(__FILE__))));
require_once YANGZIE . '/init.php';
spl_autoload_register("\\yangzie\\yze_autoload");
try {
    /**
     * 加载应用:
     * 1. 加载应用配置文件app/__config__.php,根据其中的配置进行系统初始化,比如数据库配置
     * 2. 加载应用中所有的模块配置文件,__module__.php,根据其中的配置加载模块的包含路径,自动包含的文件,url映射等等
     */
    \yangzie\yze_load_app();
    //启动会话,yze_load_app中把保存在会话中的对象类都include进来了,这样不会出现 incomplete object
    \session_start();
    //加载l10n本地语言翻译处理,根据用户的请求中的指示,决定合适的显示语言
    \yangzie\load_default_textdomain();
} catch (\Exception $notCatch) {
    $controller = new YZE_Exception_Controller();
    $controller->do_exception(new YZE_RuntimeException($notCatch->getMessage()))->output();
}