private static function init() { // 加载初始化文件 if (is_file(APP_PATH . 'init' . EXT)) { include APP_PATH . 'init' . EXT; // 加载模块配置 $config = Config::get(); } else { // 加载模块配置 $config = Config::load(APP_PATH . 'config' . EXT); // 加载应用状态配置 if ($config['app_status']) { $config = Config::load(APP_PATH . $config['app_status'] . EXT); } // 读取扩展配置文件 if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $name => $file) { $filename = APP_PATH . $file . EXT; Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME)); } } // 加载别名文件 if (is_file(APP_PATH . 'alias' . EXT)) { Loader::addMap(include APP_PATH . 'alias' . EXT); } // 加载行为扩展文件 if (APP_HOOK && is_file(APP_PATH . 'tags' . EXT)) { Hook::import(include APP_PATH . 'tags' . EXT); } // 加载公共文件 if (is_file(APP_PATH . 'common' . EXT)) { include APP_PATH . 'common' . EXT; } } // 注册根命名空间 if (!empty($config['root_namespace'])) { Loader::addNamespace($config['root_namespace']); } // 加载额外文件 if (!empty($config['extra_file_list'])) { foreach ($config['extra_file_list'] as $file) { $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; if (is_file($file)) { include_once $file; } } } // 设置系统时区 date_default_timezone_set($config['default_timezone']); // 监听app_init APP_HOOK && Hook::listen('app_init'); }
/** * 执行应用程序 * @access public * @param \think\Request $request Request对象 * @return \think\Response * @throws Exception */ public static function run($request) { // 初始化应用(公共模块) self::initModule(COMMON_MODULE, Config::get()); // 获取配置参数 $config = Config::get(); // 注册根命名空间 if (!empty($config['root_namespace'])) { Loader::addNamespace($config['root_namespace']); } // 加载额外文件 if (!empty($config['extra_file_list'])) { foreach ($config['extra_file_list'] as $file) { $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; if (is_file($file)) { include_once $file; } } } // 设置系统时区 date_default_timezone_set($config['default_timezone']); // 监听app_init APP_HOOK && Hook::listen('app_init'); // 开启多语言机制 if ($config['lang_switch_on']) { // 获取当前语言 defined('LANG_SET') or define('LANG_SET', Lang::range()); // 加载系统语言包 Lang::load(THINK_PATH . 'lang' . DS . LANG_SET . EXT); if (!APP_MULTI_MODULE) { Lang::load(APP_PATH . 'lang' . DS . LANG_SET . EXT); } } // 获取当前请求的调度信息 $dispatch = $request->dispatch(); if (empty($dispatch)) { // 未指定调度类型 则进行URL路由检测 $dispatch = self::route($request, $config); } // 记录路由信息 APP_DEBUG && Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info'); // 监听app_begin APP_HOOK && Hook::listen('app_begin', $dispatch); try { switch ($dispatch['type']) { case 'redirect': // 执行重定向跳转 header('Location: ' . $dispatch['url'], true, $dispatch['status']); break; case 'module': // 模块/控制器/操作 $data = self::module($dispatch['module'], $config); break; case 'controller': // 执行控制器操作 $data = Loader::action($dispatch['controller'], $dispatch['params']); break; case 'method': // 执行回调方法 $data = self::invokeMethod($dispatch['method'], $dispatch['params']); break; case 'function': // 规则闭包 $data = self::invokeFunction($dispatch['function'], $dispatch['params']); break; case 'finish': // 已经完成 不再继续执行 break; default: throw new Exception('dispatch type not support', 10008); } } catch (HttpResponseException $exception) { $data = $exception->getResponse(); } // 输出数据到客户端 if (isset($data)) { if ($data instanceof Response) { return $data->send(); } else { // 监听app_end APP_HOOK && Hook::listen('app_end', $data); // 自动响应输出 return Response::instance()->send($data, '', Config::get('response_return')); } } }
/** * 初始化应用 */ public static function initCommon() { if (empty(self::$init)) { // 初始化应用 $config = self::init(); self::$suffix = $config['class_suffix']; // 应用调试模式 self::$debug = Config::get('app_debug'); if (!self::$debug) { ini_set('display_errors', 'Off'); } else { //重新申请一块比较大的buffer if (ob_get_level() > 0) { $output = ob_get_clean(); } ob_start(); if (!empty($output)) { echo $output; } } // 应用命名空间 self::$namespace = $config['app_namespace']; Loader::addNamespace($config['app_namespace'], APP_PATH); if (!empty($config['root_namespace'])) { Loader::addNamespace($config['root_namespace']); } // 加载额外文件 if (!empty($config['extra_file_list'])) { foreach ($config['extra_file_list'] as $file) { $file = strpos($file, '.') ? $file : APP_PATH . $file . EXT; if (is_file($file)) { include_once $file; } } } // 设置系统时区 date_default_timezone_set($config['default_timezone']); // 监听app_init Hook::listen('app_init'); self::$init = $config; } return self::$init; }
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <*****@*****.**> // +---------------------------------------------------------------------- // 测试入口文件 define('IN_UNIT_TEST', true); $_SERVER['REQUEST_METHOD'] = 'GET'; // 定义项目测试基础路径 define('TEST_PATH', __DIR__ . '/'); // 定义项目路径 define('APP_PATH', __DIR__ . '/../../application/'); // 开启调试模式 define('APP_DEBUG', true); // 加载框架引导文件 require __DIR__ . '/../start.php'; \think\Loader::addNamespace('tests', TEST_PATH);
<?php /** * Created by PhpStorm. * User: remo * Date: 2016/7/20 * Time: 12:49 */ /** * Created by PhpStorm. * User: remo * Date: 2016/7/20 * Time: 12:49 */ define('TEST_PATH', __DIR__ . '/'); // 加载框架基础文件 require __DIR__ . '/../thinkphp/base.php'; \think\Loader::addNamespace('tests', TEST_PATH); \think\Loader::addNamespace('think', __DIR__ . '/../src/');
public function testAddNamespace() { Loader::addNamespace('top', __DIR__ . DS . 'loader' . DS); $this->assertEquals(true, Loader::autoload('top\\test\\Hello')); }