Esempio 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();
        }
    }
}
Esempio n. 2
0
require 'init.php';
$type = strtolower($_GET["t"]);
if (!in_array($type, array("js", "css"))) {
    return;
}
$bundle = @$_GET["b"];
//load static bundle
$module = @$_GET["m"];
//load module bundle
if (!$bundle && !$module) {
    return;
}
date_default_timezone_set('Asia/Chongqing');
$bundle_files = array();
if ($module) {
    $temp = $type == "js" ? YZE_Request::jsBundle($module) : YZE_Request::cssBundle($module);
    if ($temp) {
        foreach ($temp as $file) {
            $bundle_files[] = "/module-assets/{$module}/" . ltrim($file, "/");
        }
    }
} else {
    $app = new App_Module();
    foreach (explode(",", $bundle) as $bundle) {
        if (empty($bundle)) {
            continue;
        }
        $temp = $type == "js" ? $app->js_bundle($bundle) : $app->css_bundle($bundle);
        if (!$temp) {
            continue;
        }
Esempio n. 3
0
use yangzie\YZE_Need_Signin_Exception;
use yangzie\YZE_Session_Context;
use app\sp\Service_Provider_Model;
YZE_Hook::add_hook(YZE_HOOK_GET_LOGIN_USER, function ($datas) {
    $loginUser = $_SESSION['admin'];
    if (!$loginUser) {
        return null;
    }
    return $loginUser;
});
YZE_Hook::add_hook(YZE_HOOK_SET_LOGIN_USER, function ($data) {
    $_SESSION['admin'] = $data;
});
YZE_Hook::add_hook(YZE_FILTER_GET_USER_ARO_NAME, function ($data) {
    if (!@$_SESSION['admin']) {
        return "/";
    }
});
YZE_Hook::add_hook(YZE_FILTER_YZE_EXCEPTION, function ($datas) {
    //如果array("exception"=>$e, "controller"=>$controller, "response"=>$response)
    // 把signin替换成自己的登录url
    $request = YZE_Request::get_instance();
    if (!is_a($datas['exception'], "\\yangzie\\YZE_Need_Signin_Exception")) {
        return $datas;
    }
    $datas['response'] = new YZE_Redirect("/signin", $datas['controller']);
    if ($request->isInWeixin()) {
        $datas['response'] = new YZE_Redirect("/signin", $datas['controller']);
    }
    return $datas;
});
Esempio n. 4
0
/**
 * 输出module指定的css bundle,通常在controller中通过request->addCSSBundle()加入的
 */
function yze_module_css_bundle()
{
    $request = YZE_Request::get_instance();
    ?>
<link rel="stylesheet" type="text/css" href="/load.php?t=css&v=<?php 
    echo $version;
    ?>
&m=<?php 
    echo $request->module();
    ?>
" />
<?php 
}