/** * 执行应用程序 * @access public * @return void */ public static function run(array $config = []) { // 初始化公共模块 self::initModule(COMMON_MODULE, $config); // 读取扩展配置文件 if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $file) { Config::load($file, $file); } } // 获取配置参数 $config = Config::get(); // 日志初始化 Log::init($config['log']); // 缓存初始化 Cache::connect($config['cache']); // 如果启动SocketLog调试, 进行SocketLog配置 if (SLOG_ON) { Slog::config($config['slog']); } // 设置系统时区 date_default_timezone_set($config['default_timezone']); // 默认语言 $lang = strtolower($config['default_lang']); Lang::range($lang); // 加载默认语言包 Lang::load(THINK_PATH . 'Lang/' . $lang . EXT); // 监听app_init APP_HOOK && Hook::listen('app_init'); // 启动session API CLI 不开启 if (!IS_CLI && !IS_API && $config['use_session']) { Session::init($config['session']); } // 应用URL调度 self::dispatch($config); // 监听app_run APP_HOOK && Hook::listen('app_run'); // 执行操作 if (!preg_match('/^[A-Za-z](\\/|\\.|\\w)*$/', CONTROLLER_NAME)) { // 安全检测 throw new Exception('illegal controller name:' . CONTROLLER_NAME, 10000); } if (Config::get('action_bind_class')) { $class = self::bindActionClass(Config::get('empty_controller')); $instance = new $class(); // 操作绑定到类后 固定执行run入口 $action = 'run'; } else { $instance = Loader::controller(CONTROLLER_NAME, '', Config::get('empty_controller')); // 获取当前操作名 $action = ACTION_NAME . Config::get('action_suffix'); } if (!$instance) { throw new Exception('class [ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists', 10001); } try { // 操作方法开始监听 $call = [$instance, $action]; APP_HOOK && Hook::listen('action_begin', $call); if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) { // 非法操作 throw new \ReflectionException(); } //执行当前操作 $method = new \ReflectionMethod($instance, $action); if ($method->isPublic()) { // URL参数绑定检测 if (Config::get('url_params_bind') && $method->getNumberOfParameters() > 0) { // 获取绑定参数 $args = self::getBindParams($method, Config::get('url_parmas_bind_type')); // 全局过滤 array_walk_recursive($args, 'think\\Input::filterExp'); $data = $method->invokeArgs($instance, $args); } else { $data = $method->invoke($instance); } // 操作方法执行完成监听 APP_HOOK && Hook::listen('action_end', $data); // 返回数据 Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit')); } else { // 操作方法不是Public 抛出异常 throw new \ReflectionException(); } } catch (\ReflectionException $e) { // 操作不存在 if (method_exists($instance, '_empty')) { $method = new \ReflectionMethod($instance, '_empty'); $data = $method->invokeArgs($instance, [$action, '']); // 返回数据 Response::returnData($data, Config::get('default_return_type'), Config::get('response_exit')); } else { throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ', 10002); } } }
/** * 得到完整的数据表名 * @access public * @return string */ public function getTableName() { if (empty($this->trueTableName)) { $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; if (!empty($this->tableName)) { $tableName .= $this->tableName; } else { $tableName .= Loader::parseName($this->name); } $this->trueTableName = strtolower($tableName); } return (!empty($this->dbName) ? $this->dbName . '.' : '') . $this->trueTableName; }
protected static function buildModule($module, $list) { if (!is_dir(APP_PATH . $module)) { // 创建模块目录 mkdir(APP_PATH . $module); } if ('runtime' != $module) { // 创建配置文件和公共文件 self::buildCommon($module); // 创建模块的默认页面 self::buildHello($module); } // 创建子目录和文件 foreach ($list as $path => $file) { $modulePath = APP_PATH . $module . DS; if ('__dir__' == $path) { // 生成子目录 foreach ($file as $dir) { if (!is_dir($modulePath . $dir)) { // 创建目录 mkdir($modulePath . $dir, 0777, true); } } } elseif ('__file__' == $path) { // 生成(空白)文件 foreach ($file as $name) { if (!is_file($modulePath . $name)) { file_put_contents($modulePath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? "<?php\n" : ''); } } } else { // 生成相关MVC文件 foreach ($file as $val) { $filename = $modulePath . $path . DS . Loader::parseName($val) . EXT; switch ($path) { case CONTROLLER_LAYER: // 控制器 $content = "<?php\nnamespace {$module}\\{$path};\n\nclass {$val} {\n\n}"; break; case MODEL_LAYER: // 模型 $content = "<?php\nnamespace {$module}\\{$path};\n\nclass {$val} extends \\Think\\Model{\n\n}"; break; case VIEW_LAYER: // 视图 $filename = $modulePath . $path . DS . Loader::parseName($val) . '.html'; if (!is_dir(dirname($filename))) { // 创建目录 mkdir(dirname($filename), 0777, true); } $content = ''; break; default: // 其他文件 $content = "<?php\nnamespace {$module}\\{$path};\n\nclass {$val} {\n\n}"; } if (!is_file($filename)) { file_put_contents($filename, $content); } } } } }
/** * 执行应用程序 * @access public * @return void */ public static function run(array $config = []) { if (version_compare(PHP_VERSION, '5.4.0', '<')) { throw new Exception('require PHP > 5.4.0 !'); } // 日志初始化 Log::init($config['log']); // 缓存初始化 Cache::connect($config['cache']); // 加载框架底层语言包 if (is_file(THINK_PATH . 'Lang/' . strtolower($config['default_lang']) . EXT)) { Lang::set(include THINK_PATH . 'Lang/' . strtolower($config['default_lang']) . EXT); } if (is_file(APP_PATH . 'build.php')) { // 自动化创建脚本 Create::build(include APP_PATH . 'build.php'); } // 监听app_init Hook::listen('app_init'); // 初始化公共模块 self::initModule(APP_PATH . $config['common_module'] . '/', $config); // 启动session if ($config['use_session']) { Session::init($config['session']); } // 应用URL调度 self::dispatch($config); // 监听app_run Hook::listen('app_run'); // 执行操作 if (!preg_match('/^[A-Za-z](\\/|\\w)*$/', CONTROLLER_NAME)) { // 安全检测 $instance = false; } elseif ($config['action_bind_class']) { // 操作绑定到类:模块\controller\控制器\操作 if (is_dir(MODULE_PATH . CONTROLLER_LAYER . '/' . CONTROLLER_NAME)) { $namespace = MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . CONTROLLER_NAME . '\\'; } else { // 空控制器 $namespace = MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . $config['empty_controller'] . '\\'; } $actionName = strtolower(ACTION_NAME); if (class_exists($namespace . $actionName)) { $class = $namespace . $actionName; } elseif (class_exists($namespace . '_empty')) { // 空操作 $class = $namespace . '_empty'; } else { throw new Exception('_ERROR_ACTION_:' . ACTION_NAME); } $instance = new $class(); // 操作绑定到类后 固定执行run入口 $action = 'run'; } else { $instance = Loader::controller(CONTROLLER_NAME, '', $config['empty_controller']); // 获取当前操作名 $action = ACTION_NAME . $config['action_suffix']; } if (!$instance) { throw new Exception('[ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(CONTROLLER_NAME, 1) . ' ] not exists'); } try { // 操作方法开始监听 $call = [$instance, $action]; Hook::listen('action_begin', $call); if (!preg_match('/^[A-Za-z](\\w)*$/', $action)) { // 非法操作 throw new \ReflectionException(); } //执行当前操作 $method = new \ReflectionMethod($instance, $action); if ($method->isPublic()) { // URL参数绑定检测 if ($config['url_params_bind'] && $method->getNumberOfParameters() > 0) { switch ($_SERVER['REQUEST_METHOD']) { case 'POST': $vars = array_merge($_GET, $_POST); break; case 'PUT': parse_str(file_get_contents('php://input'), $vars); break; default: $vars = $_GET; } $params = $method->getParameters(); $paramsBindType = $config['url_parmas_bind_type']; foreach ($params as $param) { $name = $param->getName(); if (1 == $paramsBindType && !empty($vars)) { $args[] = array_shift($vars); } if (0 == $paramsBindType && isset($vars[$name])) { $args[] = $vars[$name]; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } else { throw new Exception('_PARAM_ERROR_:' . $name); } } array_walk_recursive($args, 'Input::filterExp'); $data = $method->invokeArgs($instance, $args); } else { $data = $method->invoke($instance); } // 操作方法执行完成监听 Hook::listen('action_end', $data); // 返回数据 Response::returnData($data, $config['default_return_type']); } else { // 操作方法不是Public 抛出异常 throw new \ReflectionException(); } } catch (\ReflectionException $e) { // 操作不存在 if (method_exists($instance, '_empty')) { $method = new \ReflectionMethod($instance, '_empty'); $method->invokeArgs($instance, [$action, '']); } else { throw new Exception('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404); } } return; }
protected static function parseUrl($url) { if (0 === strpos($url, '/')) { // 直接作为路由地址解析 $url = substr($url, 1); } elseif (false !== strpos($url, '\\')) { // 解析到类 $url = ltrim(str_replace('\\', '/', $url), '/'); } elseif (0 === strpos($url, '@')) { // 解析到控制器 $url = substr($url, 1); } else { // 解析到 模块/控制器/操作 $module = MODULE_NAME ? MODULE_NAME . '/' : ''; if ('' == $url) { // 空字符串输出当前的 模块/控制器/操作 $url = $module . CONTROLLER_NAME . '/' . ACTION_NAME; } else { $path = explode('/', $url); $action = array_pop($path); $controller = empty($path) ? CONTROLLER_NAME : (Config::get('url_controller_convert') ? Loader::parseName(array_pop($path)) : array_pop($path)); $module = empty($path) ? $module : array_pop($path) . '/'; $url = $module . $controller . '/' . $action; } } return $url; }