/** * 具体解析 * @param unknown_type $isPre */ private function parse($isPre = true) { $InitPHP_conf = InitPHP::getConfig(); $interceptor = $InitPHP_conf['interceptor']; $filePath = $interceptor['path']; //文件路径 $return = true; if (is_array($interceptor['rule']) && count($interceptor['rule']) > 0) { foreach ($interceptor['rule'] as $k => $v) { $file = ltrim($filePath, "/") . "/" . $v['file'] . $interceptor['postfix'] . '.php'; $class = $v['file'] . $interceptor['postfix']; //处理正则匹配 $regular = $v['regular']; if ($regular['m'] != "" && $regular['m'] != '*') { if (!preg_match($regular['m'], $this->m)) { continue; } } if ($regular['c'] != "" && $regular['c'] != '*') { if (!preg_match($regular['c'], $this->c)) { continue; } } if ($regular['a'] != "" && $regular['a'] != '*') { if (!preg_match($regular['a'], $this->a)) { continue; } } if (file_exists(InitPHP::getAppPath($file))) { InitPHP::import($file); $obj = InitPHP::loadclass($class); if ($isPre == true) { $ret = $obj->preHandle(); if ($ret == false) { $return = false; break; } } else { $obj->postHandle(); } } } } return $return; }
<?php define("APP_PATH", dirname(__FILE__)); header("Content-Type:text/html; charset=utf-8"); require_once 'initphp/initphp.php'; //导入配置文件-必须载入 require_once 'conf/comm.conf.php'; //公用配置 require_once 'conf/admin.conf.php'; //公用配置 InitPHP::import('library/helper/BaseDao.php'); InitPHP::import('library/helper/BaseService.php'); InitPHP::import('library/helper/BaseAdminController.php'); InitPHP::init(); //框架初始化
/** * 调用其它Controller中的方法 * 1. 一般不建议采用Controller调用另外一个Controller中的方法 * 2. 该函数可以用于接口聚集,将各种接口聚集到一个接口中使用 * 全局使用方法:InitPHP::getController($controllerName, $functionName) * @param $controllerName 控制器名称 * @param $functionName 方法名称 * @param $params 方法参数 * @param $controllerPath 控制器文件夹名称,例如在控制器文件夹目录中,还有一层目录,user/则,该参数需要填写 * @return */ public static function getController($controllerName, $functionName, $params = array(), $controllerPath = '') { $InitPHP_conf = InitPHP::getConfig(); $controllerPath = $controllerPath == '' ? '' : rtrim($controllerPath, '/') . '/'; $path = rtrim($InitPHP_conf['controller']['path'], '/') . '/' . $controllerPath . $controllerName . '.php'; if (!InitPHP::import($path)) { $controllerName = ucfirst($controllerName); $path = rtrim($InitPHP_conf['controller']['path'], '/') . '/' . $controllerPath . $controllerName . '.php'; InitPHP::import($path); } $controller = InitPHP::loadclass($controllerName); if (!$controller) { return InitPHP::initError('can not loadclass : ' . $controllerName); } if (!method_exists($controller, $functionName)) { return InitPHP::initError('function is not exists : ' . $controllerName); } if (!$params) { $controller->{$functionName}(); } else { call_user_func_array(array($controller, $functionName), $params); } }
/** * 获取运行的service * @return object */ private function get_service_obj() { $InitPHP_conf = InitPHP::getConfig(); $test_file_name = $this->classname . $InitPHP_conf['service']['service_postfix']; if (InitPHP::import("{$test_file_name}" . ".php", array($InitPHP_conf['service']['path']))) { return InitPHP::loadclass($test_file_name); } return false; }
/** * 验证请求是否合法 * 1. 如果请求参数m,c,a都为空,则走默认的 */ private function checkRequest() { $InitPHP_conf = InitPHP::getConfig(); $controller = isset($_GET['c']) ? $_GET['c'] : ''; $action = isset($_GET['a']) ? $_GET['a'] : ''; if ($InitPHP_conf['ismodule'] == true) { $module = isset($_GET['m']) ? $_GET['m'] : ''; if ($module == "" && $controller == "" && $action == "") { $module = $_GET['m'] = $this->default_module; $controller = $_GET['c'] = $this->default_controller; $action = $_GET['a'] = $this->default_action; } //如果module不在白名单中,则直接返回404 if (!in_array($module, $this->module_list) || empty($module)) { return InitPHP::return404(); } $module = $module . '/'; } else { if ($controller == "" && $action == "") { $controller = $_GET['c'] = $this->default_controller; $action = $_GET['a'] = $this->default_action; } $module = ''; } //controller处理,如果导入Controller文件失败,则返回404 $path = rtrim($InitPHP_conf['controller']['path'], '/') . '/'; $controllerClass = $controller . $this->controller_postfix; $controllerFilePath = $path . $module . $controllerClass . '.php'; if (!InitPHP::import($controllerFilePath)) { //如果开启了首字母大写,例如请求c=my,则类名为MyController $controllerClass = ucfirst($controller) . $this->controller_postfix; //改成大写 $controllerFilePath = $path . $module . $controllerClass . '.php'; if (!InitPHP::import($controllerFilePath)) { return InitPHP::return404(); } } $controllerObj = InitPHP::loadclass($controllerClass); //处理Action,如果方法不存在,则直接返回404 list($whiteList, $methodList) = $this->parseWhiteList($controllerObj->initphp_list); if ($action != $this->default_action) { if (!in_array($action, $whiteList)) { return InitPHP::return404(); //如果Action不在白名单中 } else { if ($methodList[$action]) { $method = strtolower($_SERVER['REQUEST_METHOD']); if (!in_array($method, $methodList[$action])) { //检查提交的HTTP METHOD return InitPHP::return405(); //如果请求Method不正确,则返回405 } } } } return $controllerObj; }