public function end_form() { echo '</form>'; $form = ob_get_clean(); $aroname = \yangzie\YZE_Hook::do_hook(YZE_FILTER_GET_USER_ARO_NAME); if ($this->acl->check_byname($aroname, $this->form_name)) { echo $form; } }
function load_default_textdomain() { $local = \yangzie\YZE_Hook::do_hook("get_locale", "zh-cn"); if (!function_exists("\\yangzie\\script_locale") && !$local) { return; } if (function_exists("\\yangzie\\script_locale")) { // for script tool $locale = script_locale(); } else { $locale = $local; } $mofile = "vendor/i18n/{$locale}.mo"; return load_textdomain('default', $mofile); }
/** * 加载所有的模块,设置其配置 */ function yze_load_app() { // 加载app配置 if (!file_exists(YZE_APP_PATH . "__config__.php")) { die(__("app/__config__.php not found")); } include_once YZE_APP_INC . '__config__.php'; @(include_once YZE_APP_INC . '__aros_acos__.php'); $app_module = new App_Module(); $app_module->check(); $module_include_files = $app_module->module_include_files(); foreach ((array) $module_include_files as $path) { include_once $path; } YZE_Hook::include_hooks("app", YZE_APP_INC . 'hooks'); foreach (glob(YZE_APP_MODULES_INC . "*") as $module) { $phar_wrap = ""; if (is_file($module)) { // phar $phar_wrap = "phar://"; } $module_name = strtolower(basename($module)); if ($phar_wrap) { $module_name = ucfirst(preg_replace('/\\.phar$/', "", $module_name)); } if (@file_exists("{$phar_wrap}{$module}/__module__.php")) { require_once "{$phar_wrap}{$module}/__module__.php"; $class = "\\app\\{$module_name}\\" . ucfirst($module_name) . "_Module"; $object = new $class(); $object->check(); $mappings = $object->get_module_config('routers'); if ($mappings) { YZE_Router::get_Instance()->set_Routers($module_name, $mappings); } \yangzie\YZE_Object::set_loaded_modules($module_name, array("is_phar" => $phar_wrap ? true : false)); } YZE_Hook::include_hooks($module_name, "{$phar_wrap}{$module}/hooks"); } }
/** * 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(); } } }
public final function do_exception(YZE_RuntimeException $e) { $request = $this->request; $request->setException($e); \yangzie\YZE_Hook::do_hook(YZE_ACTION_BEFORE_DO_EXCEPTION, $this); $session = YZE_Session_Context::get_instance(); $response = $this->exception($e); if ($request->is_post()) { if (!$response && strcasecmp($request->get_from_request('yze_post_context', ''), "json") == 0) { // post直接返回结果 $this->layout = ""; return $this->post_result_of_json; } if (!$response && strcasecmp($request->get_from_request('yze_post_context', ''), "iframe") == 0) { $this->layout = ""; return new YZE_Notpl_View("<script>window.parent.yze_iframe_form_submitCallback(" . json_encode($this->post_result_of_json->get_datas()) . ");</script>", $this); } } else { if (!$response) { $this->set_View_Data("exception", $e); $response = $this->getResponse(YZE_APP_VIEWS_INC . "500"); } } return $response; }
public function auth() { $req_method = $this->the_method(); if ($this->need_auth($req_method)) { // 需要验证 $loginuser = YZE_Hook::do_hook(YZE_HOOK_GET_LOGIN_USER); if (!$loginuser) { throw new YZE_Need_Signin_Exception("请登录"); } $aro = \yangzie\YZE_Hook::do_hook(YZE_FILTER_GET_USER_ARO_NAME); // 验证请求的方法是否有权限调用 $acl = YZE_ACL::get_instance(); $aco_name = "/" . $this->module() . "/" . $this->controller_name(true) . "/" . $req_method; if (!$acl->check_byname($aro, $aco_name)) { throw new YZE_Permission_Deny_Exception(vsprintf(__("没有访问该页面的权限({$aco_name}:{$aro})"), array(\app\yze_get_aco_desc($aco_name)))); } } return $this; }
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; });
/** * 返回表名 */ public function get_table() { $data = array("table" => $this::TABLE, "module" => $this->get_module_name()); $result = \yangzie\YZE_Hook::do_hook("get_table", $data); return $result["table"]; }
/** * 保存(update,insert)记录;如果有主键,则更新;没有则插入; * 插入情况,根据$type进行不同的插入策略 * INSERT_NORMAL:普通插入语句 * INSERT_NOT_EXIST: 指定的where条件查询不出数据时才插入,如果插入、更新成功,会返回主键值,如果插入失败会返回0,这是的entity->get_key()返回0 * INSERT_NOT_EXIST_OR_UPDATE: 指定的$checkSql条件查询不出数据时才插入, 查询数据则更新这条数据;如果插入、更新成功,会返回主键值,如果插入失败会返回0,这是的entity->get_key()返回0 * INSERT_EXIST: 指定的$checkSql条件查询出数据时才插入,如果插入、更新成功,会返回主键值,如果插入失败会返回0,这是的entity->get_key()返回0 * INSERT_ON_DUPLICATE_KEY_UPDATE: 有唯一健冲突时更新其它字段 * INSERT_ON_DUPLICATE_KEY_REPLACE: 有唯一健冲突时先删除原来的,然后在插入 * INSERT_ON_DUPLICATE_KEY_IGNORE: 有唯一健冲突时忽略,不抛异常 * @param string $sql 完整的判断查询sql * @param YZE_Model $entity * @param string $type YZE_SQL::INSERT_XX常量 * @throws YZE_DBAException * @return string */ public function save(YZE_Model $entity, $type = YZE_SQL::INSERT_NORMAL, YZE_SQL $checkSql = null) { if (empty($entity)) { throw new YZE_DBAException("save YZE_Model is empty"); } if ($entity->get_key()) { //update return $this->_save_update($entity); } $sql = new YZE_SQL(); $extra_info = $type == YZE_SQL::INSERT_ON_DUPLICATE_KEY_UPDATE ? array_keys($entity->get_unique_key()) : $checkSql; //insert $sql->insert('t', $entity->get_records(), $type, $extra_info)->from(get_class($entity), "t"); $rowCount = $this->execute($sql); $insert_id = $this->conn->lastInsertId(); if ($type == YZE_SQL::INSERT_EXIST || $type == YZE_SQL::INSERT_NOT_EXIST) { if ($rowCount) { //这种情况下last insert id 得不到? $entity->set($entity->get_key_name(), $insert_id); } } elseif ($type == YZE_SQL::INSERT_NOT_EXIST_OR_UPDATE) { if (!$rowCount) { $alias = $checkSql->get_alias($entity->get_table()); $checkSql->update($alias, $entity->get_records()); $this->execute($checkSql); $checkSql->select($alias, array($entity->get_key_name())); $obj = $this->getSingle($checkSql); $insert_id = $obj->get_key(); } $entity->set($entity->get_key_name(), $insert_id); } else { if ($type == YZE_SQL::INSERT_ON_DUPLICATE_KEY_UPDATE) { //0 not modified, 1 insert, 2 update if ($rowCount == 2 && count($entity->get_unique_key()) > 1) { $records = $entity->get_records(); $entity->refresh(); //当$update_on_duplicate_key是考虑有多个唯一健的更新情况;可能会由于某个唯一值冲突,导致其它唯一值没有更新的情况 //所以这里在update一下 foreach ($entity->get_unique_key() as $field) { $entity->set($field, $records[$field]); } $entity->save(); } } else { if ($type == YZE_SQL::INSERT_ON_DUPLICATE_KEY_IGNORE) { $insert_id = 0; } } } $entity->set($entity->get_key_name(), $insert_id); \yangzie\YZE_Hook::do_hook(YZE_HOOK_MODEL_INSERT, $entity); return $insert_id; }