protected function buildCacheContent($module) { $content = ''; $path = realpath(APP_PATH . $module) . DS; // 加载模块配置 $config = \think\Config::load(CONF_PATH . $module . 'config' . CONF_EXT); // 加载应用状态配置 if ($module && $config['app_status']) { $config = \think\Config::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT); } // 读取扩展配置文件 if ($module && $config['extra_config_list']) { foreach ($config['extra_config_list'] as $name => $file) { $filename = CONF_PATH . $module . $file . CONF_EXT; \think\Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME)); } } // 加载别名文件 if (is_file(CONF_PATH . $module . 'alias' . EXT)) { $content .= '\\think\\Loader::addClassMap(' . var_export(include CONF_PATH . $module . 'alias' . EXT, true) . ');' . PHP_EOL; } // 加载行为扩展文件 if (is_file(CONF_PATH . $module . 'tags' . EXT)) { $content .= '\\think\\Hook::import(' . var_export(include CONF_PATH . $module . 'tags' . EXT, true) . ');' . PHP_EOL; } // 加载公共文件 if (is_file($path . 'common' . EXT)) { $content .= substr(php_strip_whitespace($path . 'common' . EXT), 5) . PHP_EOL; } $content .= '\\think\\Config::set(' . var_export(\think\Config::get(), true) . ');'; return $content; }
/** * 切换缓存类型 需要配置 cache.type 为 complex * @access public * @param string $name 缓存标识 * @return \think\cache\Driver */ public static function store($name) { if ('complex' == Config::get('cache.type')) { self::connect(Config::get('cache.' . $name), strtolower($name)); } return self::$handler; }
/** * 被调用接口 * 载入Smarty模板引擎,获得相关内容 */ public function fetch($templateFile, $var) { // 载入Smarty文件 include_once CORE_PATH . 'Views/Smarty/Smarty.class.php'; // 实例化 $tpl = new SmartyEngine(); // 是否开启缓存, 模板目录, 编译目录, 缓存目录 $tpl->caching = Config::get('TMPL_CACHE_ON'); $tpl->template_dir = THEME_PATH; $tpl->compile_dir = CACHE_PATH; $tpl->cache_dir = TEMP_PATH; $tpl->debugging = false; $tpl->left_delimiter = '{{'; $tpl->right_delimiter = '}}'; // 自定义配置 if (C('TMPL_ENGINE_CONFIG')) { $config = C('TMPL_ENGINE_CONFIG'); foreach ($config as $key => $val) { $tpl->{$key} = $val; } } // 输出 $tpl->assign($var); $tpl->display($templateFile); }
/** * 发送数据到客户端 * @access public * @param mixed $data 数据 * @param string $type 返回类型 * @param bool $return 是否返回数据 * @return mixed */ public function send($data = [], $type = '', $return = false) { if ('' == $type) { $type = $this->type ?: (IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type')); } $type = strtolower($type); $data = $data ?: $this->data; if (!headers_sent() && isset($this->contentType[$type])) { header('Content-Type:' . $this->contentType[$type] . '; charset=utf-8'); } if (is_callable($this->transform)) { $data = call_user_func_array($this->transform, [$data]); } else { switch ($type) { case 'json': // 返回JSON数据格式到客户端 包含状态信息 $data = json_encode($data, JSON_UNESCAPED_UNICODE); break; case 'jsonp': // 返回JSON数据格式到客户端 包含状态信息 $handler = !empty($_GET[Config::get('var_jsonp_handler')]) ? $_GET[Config::get('var_jsonp_handler')] : Config::get('default_jsonp_handler'); $data = $handler . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ');'; break; } } APP_HOOK && Hook::listen('return_data', $data); if ($return) { return $data; } echo $data; $this->isExit() && exit; }
/** * 自动初始化缓存 * @access public * @return void */ public static function init() { if (is_null(self::$handler)) { // 自动初始化缓存 self::connect(Config::get('cache')); } }
/** * 检查菜单权限 * @return boolean */ public static function checkAuth($uid, $node) { $adminUsers = \think\Config::get('administrator'); // 当前用户不是超级用户,要做权限验证 if (!in_array($uid, $adminUsers)) { // 根据控制器 去验证 $nodeId = Db::name('Menu')->where('url', $node)->value('id'); if ($nodeId) { $nodes = session('user_auth_nodes'); if (!$nodes) { // 获取当前用户的授权节点 $gIds = self::getGroupIds($uid); if ($gIds) { $nodes = Db::name('Auth')->where('id', 'in', $gIds)->column('rules'); $nodes = implode($nodes, ','); $nodes = trim($nodes, ','); $nodes = explode(',', $nodes); session('user_auth_nodes', $nodes); } else { return false; } } if (in_array($nodeId, $nodes)) { return true; } else { return false; } } else { // 不存在的节点 不验证 return true; } } else { return true; } }
/** * 架构函数 初始化视图类 并采用内置模板引擎 * @access public */ public function initView() { // 模板引擎参数 if (is_null($this->view)) { $this->view = \think\View::instance(Config::get()); // 只能这样写,不然view会冲突 } }
function C($name = '', $value = null, $range = '') { if (is_null($value) && is_string($name)) { return \think\Config::get($name, $range); } else { return \think\Config::set($name, $value, $range); } }
public function __construct($config = []) { $this->config = ['tpl_path' => App::$modulePath . 'view' . DS, 'tpl_suffix' => Config::get('view.engine_config.suffix') ?: '.html', 'tpl_cache_path' => CACHE_PATH, 'tpl_cache_suffix' => Config::get('view.engine_config.cache') ?: '.php', 'attr' => 'php-']; $this->template = new \Angular($this->config); // 初始化模板编译存储器 $storage = Config::get('compile_type') ?: '\\think\\template\\driver\\File'; $this->storage = new $storage(); }
/** * 调试输出接口 * @access public * @param Response $response Response对象 * @param array $log 日志信息 * @return bool */ public function output(Response $response, array $log = []) { $request = Request::instance(); $contentType = $response->getHeader('Content-Type'); $accept = $request->header('accept'); if (strpos($accept, 'application/json') === 0 || $request->isAjax()) { return false; } elseif (!empty($contentType) && strpos($contentType, 'html') === false) { return false; } // 获取基本信息 $runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME; $reqs = number_format(1 / $runtime, 2); $mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2); // 页面Trace信息 if (isset($_SERVER['HTTP_HOST'])) { $uri = $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { $uri = 'cmd:' . implode(' ', $_SERVER['argv']); } $base = ['请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $uri, '运行时间' => number_format($runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()), '查询信息' => Db::$queryTimes . ' queries ' . Db::$executeTimes . ' writes ', '缓存信息' => Cache::$readTimes . ' reads,' . Cache::$writeTimes . ' writes', '配置加载' => count(Config::get())]; if (session_id()) { $base['会话信息'] = 'SESSION_ID=' . session_id(); } $info = Debug::getFile(true); // 页面Trace信息 $trace = []; foreach ($this->config['trace_tabs'] as $name => $title) { $name = strtolower($name); switch ($name) { case 'base': // 基本信息 $trace[$title] = $base; break; case 'file': // 文件信息 $trace[$title] = $info; break; default: // 调试信息 if (strpos($name, '|')) { // 多组信息 $names = explode('|', $name); $result = []; foreach ($names as $name) { $result = array_merge($result, isset($log[$name]) ? $log[$name] : []); } $trace[$title] = $result; } else { $trace[$title] = isset($log[$name]) ? $log[$name] : ''; } } } // 调用Trace页面模板 ob_start(); include $this->config['trace_file']; return ob_get_clean(); }
/** * session初始化 * @param array $config * @return void * @throws \think\Exception */ public static function init(array $config = []) { if (empty($config)) { $config = Config::get('session'); } // 记录初始化信息 App::$debug && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info'); $isDoStart = false; if (isset($config['use_trans_sid'])) { ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0); } // 启动session if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) { ini_set('session.auto_start', 0); $isDoStart = true; } if (isset($config['prefix'])) { self::$prefix = $config['prefix']; } if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) { session_id($_REQUEST[$config['var_session_id']]); } elseif (isset($config['id']) && !empty($config['id'])) { session_id($config['id']); } if (isset($config['name'])) { session_name($config['name']); } if (isset($config['path'])) { session_save_path($config['path']); } if (isset($config['domain'])) { ini_set('session.cookie_domain', $config['domain']); } if (isset($config['expire'])) { ini_set('session.gc_maxlifetime', $config['expire']); ini_set('session.cookie_lifetime', $config['expire']); } if (isset($config['use_cookies'])) { ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0); } if (isset($config['cache_limiter'])) { session_cache_limiter($config['cache_limiter']); } if (isset($config['cache_expire'])) { session_cache_expire($config['cache_expire']); } if (!empty($config['type'])) { // 读取session驱动 $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']); // 检查驱动类 if (!class_exists($class) || !session_set_save_handler(new $class($config))) { throw new ClassNotFoundException('error session handler:' . $class, $class); } } if ($isDoStart) { session_start(); } }
/** * 返回封装后的API数据到客户端 * @access protected * @param mixed $data 要返回的数据 * @param string $msg 提示信息 * @param integer $code 返回的code * @param string $url 重定向地址 * @param integer $wait 跳转等待时间 * @return void */ public function result($data = '', $msg = '', $code = 0, $url = '', $wait = 0) { $result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait]; if ('html' == Config::get('default_return_type')) { return $this->fetch(Config::get('dispatch_jump_tmpl'), $result); } else { return $result; } }
/** * 日志写入接口 * @access public * @param array $log 日志信息 * @return bool */ public function save(array $log = []) { if (IS_AJAX || IS_CLI || IS_API || 'html' != Config::get('default_return_type')) { // ajax cli api方式下不输出 return false; } // 获取基本信息 $runtime = microtime(true) - START_TIME; $reqs = number_format(1 / $runtime, 2); $runtime = number_format($runtime, 6); $mem = number_format((memory_get_usage() - START_MEM) / 1024, 2); // 页面Trace信息 $base = ['请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ] 内存消耗:{$mem}kb 文件加载:" . count(get_included_files()), '查询信息' => \think\Db::$queryTimes . ' queries ' . \think\Db::$executeTimes . ' writes ', '缓存信息' => \think\Cache::$readTimes . ' reads,' . \think\Cache::$writeTimes . ' writes', '配置加载' => count(Config::get())]; if (session_id()) { $base['会话信息'] = 'SESSION_ID=' . session_id(); } $info = Debug::getFile(true); // 获取调试日志 $debug = []; foreach ($log as $line) { $debug[$line['type']][] = $line['msg']; } // 页面Trace信息 $trace = []; foreach ($this->config['trace_tabs'] as $name => $title) { $name = strtolower($name); switch ($name) { case 'base': // 基本信息 $trace[$title] = $base; break; case 'file': // 文件信息 $trace[$title] = $info; break; default: // 调试信息 if (strpos($name, '|')) { // 多组信息 $names = explode('|', $name); $result = []; foreach ($names as $name) { $result = array_merge($result, isset($debug[$name]) ? $debug[$name] : []); } $trace[$title] = $result; } else { $trace[$title] = isset($debug[$name]) ? $debug[$name] : ''; } } } // 调用Trace页面模板 ob_start(); include $this->config['trace_file']; echo ob_get_clean(); return true; }
/** * Fetch the encryption key * * Returns it as MD5 in order to have an exact-length 128 bit key. * Mcrypt is sensitive to keys that are not the correct length * * @access public * @param string * @return string */ public function getKey($key = '') { if ($key == '') { if ($this->encryption_key != '') { return $this->encryption_key; } $key = \think\Config::get('encryption_key'); } return md5($key); }
public function testConfig() { App::run(Config::get()); Config::parse('isTrue=1', 'test'); Config::range('test'); $this->assertTrue(Config::has('isTrue')); $this->assertEquals(1, Config::get('isTrue')); Config::set('isTrue', false); $this->assertEquals(0, Config::get('isTrue')); Config::reset(); }
public function testRun() { $response = App::run(Request::create("http://www.example.com")); $expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>'; $this->assertEquals($expectOutputString, $response->getContent()); $this->assertEquals(200, $response->getCode()); $this->assertEquals(true, function_exists('lang')); $this->assertEquals(true, function_exists('config')); $this->assertEquals(true, function_exists('input')); $this->assertEquals(Config::get('default_timezone'), date_default_timezone_get()); }
public function testConfig() { App::run(); $this->assertTrue(Config::has('url_route_on')); $this->assertEquals(1, Config::get('url_route_on')); Config::set('url_route_on', false); $this->assertEquals(0, Config::get('url_route_on')); Config::range('test'); $this->assertFalse(Config::has('url_route_on')); Config::reset(); }
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'); }
/** * 架构函数 * 遍历合并Config * * @return void */ public function __construct() { if (!empty($this->options)) { foreach ($this->options as $name => $val) { // 参数已设置 则覆盖行为参数 if (Config::get($name) !== null) { $this->options[$name] = C($name); } else { Config::set($name, $val); } } array_change_key_case($this->options); } }
/** * 操作错误跳转的快捷方法 * @access public * @param mixed $msg 提示信息 * @param string $url 跳转的URL地址 * @param mixed $data 返回的数据 * @param integer $wait 跳转等待时间 * @return void */ public function error($msg = '', $url = null, $data = '', $wait = 3) { $code = 0; if (is_numeric($msg)) { $code = $msg; $msg = ''; } $result = ['code' => $code, 'msg' => $msg, 'data' => $data, 'url' => is_null($url) ? 'javascript:history.back(-1);' : $url, 'wait' => $wait]; $type = IS_AJAX ? Config::get('default_ajax_return') : Config::get('default_return_type'); if ('html' == $type) { $result = View::instance(Config::get('template'), Config::get('view_replace_str'))->fetch(Config::get('dispatch_error_tmpl'), $result); } Response::instance()->send($result, $type); }
/** * 显示页面Trace信息 * @access private */ private function showTrace() { // 系统默认显示信息 $files = get_included_files(); $info = []; foreach ($files as $key => $file) { $info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )'; } $trace = []; Debug::remark('START', NOW_TIME); $base = ['请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['PHP_SELF'], '运行时间' => Debug::getUseTime('START', 'END', 6) . 's', '内存开销' => MEMORY_LIMIT_ON ? G('START', 'END', 'm') . 'b' : '不支持', '查询信息' => N('db_query') . ' queries ' . N('db_write') . ' writes ', '文件加载' => count($files), '缓存信息' => N('cache_read') . ' gets ' . N('cache_write') . ' writes ', '配置加载' => count(Config::get())]; // 读取项目定义的Trace文件 $traceFile = MODULE_PATH . 'trace.php'; if (is_file($traceFile)) { $base = array_merge($base, include $traceFile); } $debug = Log::getLog(); $tabs = Config::get('trace_page_tabs'); foreach ($tabs as $name => $title) { switch (strtoupper($name)) { case 'BASE': // 基本信息 $trace[$title] = $base; break; case 'FILE': // 文件信息 $trace[$title] = $info; break; default: // 调试信息 $name = strtoupper($name); if (strpos($name, '|')) { // 多组信息 $array = explode('|', $name); $result = []; foreach ($array as $name) { $result += isset($debug[$name]) ? $debug[$name] : []; } $trace[$title] = $result; } else { $trace[$title] = isset($debug[$name]) ? $debug[$name] : ''; } } } unset($files, $info, $base, $debug); // 调用Trace页面模板 ob_start(); include Config::has('tmpl_trace_file') ? Config::get('tmpl_trace_file') : THINK_PATH . 'tpl/page_trace.tpl'; return ob_get_clean(); }
/** * 写入初始数据 * @return boolean true - 写入成功,false - 写入失败 */ public function create() { $sql = "-- -----------------------------\n"; $sql .= "-- c.Jango MySQL Data Transfer\n"; $sql .= "--\n"; $sql .= "-- Host : " . \think\Config::get('database.hostname') . "\n"; $sql .= "-- UserName : "******"\n"; $sql .= "-- Database : " . \think\Config::get('database.database') . "\n"; $sql .= "--\n"; $sql .= "-- Part : #{$this->file['part']}\n"; $sql .= "-- Date : " . date("Y-m-d H:i:s") . "\n"; $sql .= "-- -----------------------------\n\n"; return $this->write($sql); }
protected function execute(Input $input, Output $output) { if (!is_dir(RUNTIME_PATH . 'schema')) { @mkdir(RUNTIME_PATH . 'schema', 0755, true); } if ($input->hasOption('module')) { $module = $input->getOption('module'); // 读取模型 $list = scandir(APP_PATH . $module . DS . 'model'); $app = App::$namespace; foreach ($list as $file) { if ('.' == $file || '..' == $file) { continue; } $class = '\\' . $app . '\\' . $module . '\\model\\' . pathinfo($file, PATHINFO_FILENAME); $this->buildModelSchema($class); } $output->writeln('<info>Succeed!</info>'); return; } else { if ($input->hasOption('table')) { $table = $input->getOption('table'); if (!strpos($table, '.')) { $dbName = Db::getConfig('database'); } $tables[] = $table; } elseif ($input->hasOption('db')) { $dbName = $input->getOption('db'); $tables = Db::getTables($dbName); } elseif (!\think\Config::get('app_multi_module')) { $app = App::$namespace; $list = scandir(APP_PATH . 'model'); foreach ($list as $file) { if ('.' == $file || '..' == $file) { continue; } $class = '\\' . $app . '\\model\\' . pathinfo($file, PATHINFO_FILENAME); $this->buildModelSchema($class); } $output->writeln('<info>Succeed!</info>'); return; } else { $tables = Db::getTables(); } } $db = isset($dbName) ? $dbName . '.' : ''; $this->buildDataBaseSchema($tables, $db); $output->writeln('<info>Succeed!</info>'); }
public function testRun() { Config::set('root_namespace', ['/path/']); App::run(); $expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP5</b>!</p></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>'; $this->expectOutputString($expectOutputString); $rc = new ReflectionClass('\\think\\Loader'); $ns = $rc->getProperty('namespace'); $ns->setAccessible(true); $this->assertEquals(true, in_array('/path/', $ns->getValue())); $this->assertEquals(true, function_exists('L')); $this->assertEquals(true, function_exists('C')); $this->assertEquals(true, function_exists('I')); $this->assertEquals(Config::get('default_timezone'), date_default_timezone_get()); }
public function testRun() { Config::set('root_namespace', ['/path/']); App::run(); $expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>'; $this->expectOutputString($expectOutputString); $rc = new ReflectionClass('\\think\\Loader'); $ns = $rc->getProperty('namespace'); $ns->setAccessible(true); $this->assertEquals(true, in_array('/path/', $ns->getValue())); $this->assertEquals(true, function_exists('L')); $this->assertEquals(true, function_exists('C')); $this->assertEquals(true, function_exists('I')); $this->assertEquals(Config::get('default_timezone'), date_default_timezone_get()); }
/** * 架构函数 * @param Request $request Request对象 * @access public */ public function __construct(Request $request = null) { if (is_null($request)) { $request = Request::instance(); } $this->view = View::instance(Config::get('template'), Config::get('view_replace_str')); $this->request = $request; // 控制器初始化 $this->_initialize(); // 前置操作方法 if ($this->beforeActionList) { foreach ($this->beforeActionList as $method => $options) { is_numeric($method) ? $this->beforeAction($options) : $this->beforeAction($method, $options); } } }
/** * 日志写入接口 * @access public * @param array $log 日志信息 * @return void */ public function save($log = []) { // 获取基本信息 $current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $runtime = number_format(microtime(true) - START_TIME, 6); $reqs = number_format(1 / $runtime, 2); // 页面Trace信息 $base = ['请求信息' => date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']) . ' ' . $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $current_uri, '运行时间' => "{$runtime}s [ 吞吐率:{$reqs}req/s ]", '内存消耗' => number_format((memory_get_usage() - START_MEM) / 1024, 2) . 'kb', '查询信息' => \think\Db::$queryTimes . ' queries ' . \think\Db::$executeTimes . ' writes ', '缓存信息' => \think\Cache::$readTimes . ' reads,' . \think\Cache::$writeTimes . ' writes', '文件加载' => count(get_included_files()), '配置加载' => count(\think\Config::get()), '会话信息' => 'SESSION_ID=' . session_id()]; $info = \think\Debug::getFile(true); // 获取调试日志 $debug = []; foreach ($log as $line) { $debug[$line['type']][] = $line['msg']; } // 页面Trace信息 $trace = []; foreach ($this->tabs as $name => $title) { $name = strtolower($name); switch ($name) { case 'base': // 基本信息 $trace[$title] = $base; break; case 'file': // 文件信息 $trace[$title] = $info; break; default: // 调试信息 if (strpos($name, '|')) { // 多组信息 $names = explode('|', $name); $result = []; foreach ($names as $name) { $result = array_merge($result, isset($debug[$name]) ? $debug[$name] : []); } $trace[$title] = $result; } else { $trace[$title] = isset($debug[$name]) ? $debug[$name] : ''; } } } // 调用Trace页面模板 ob_start(); include $this->config['trace_file']; echo ob_get_clean(); }
protected function buildRouteCache() { $files = \think\Config::get('route_config_file'); foreach ($files as $file) { if (is_file(CONF_PATH . $file . CONF_EXT)) { $config = (include CONF_PATH . $file . CONF_EXT); if (is_array($config)) { \think\Route::import($config); } } } $rules = \think\Route::rules(true); array_walk_recursive($rules, [$this, 'buildClosure']); $content = '<?php ' . PHP_EOL . 'return '; $content .= var_export($rules, true) . ';'; $content = str_replace(['\'[__start__', '__end__]\''], '', stripcslashes($content)); return $content; }
/** * 行为入口 * * @param &$_data * @return void */ public function run(&$_data) { try { // 配置引擎 $_content = empty($_data['content']) ? $_data['file'] : $_data['content']; $_data['prefix'] = !empty($_data['prefix']) ? $_data['prefix'] : C('TMPL_CACHE_PREFIX'); // 调用第三方模板引擎解析和输出 $class = Config::get('TMPL_ENGINE_DRIVER'); // 加载类 if (class_exists($class)) { $tpl = new $class(); $tpl->fetch($_content, $_data['var']); } else { throw new Exception(L('_NOT_SUPPERT_') . ': ' . $class); } } catch (Exception $error) { exit($error->getMessage()); } }
/** * 上传文件 * @param string $name input name * @param array $options 扩展配置 * @return array|boolean */ public function upload($name = '', $options = []) { $File = Request::instance()->file($name); // 检查是否有该文件,如果上传过,直接返回文件信息 $info = $this->where('hash', $File->hash())->find(); if ($info) { return ['file_id' => $info->id, 'path' => ltrim($info->path, '.')]; } // 合并扩展选项 $config = Config::get('storage'); if (is_array($options) && !empty($options)) { $config = array_merge($config, $options); } // 校验文件是否允许上传 if (!$File->check($config)) { $this->error = $File->getError(); return false; } else { // 保存的规则 $File->rule(function ($file) { $rule = explode('/', $file->getMime())[0] . '/'; $rule .= date('Y-m/d'); $rule .= '/' . $file->hash(); $rule .= '.' . strtolower(pathinfo($file->getInfo('name'), PATHINFO_EXTENSION)); return $rule; }); // 上传文件 $info = $File->move($config['path'], true, $config['replace']); if ($info) { $data = ['type' => explode('/', $info->getMime())[0], 'name' => rtrim($info->getInfo('name'), '.' . $info->getExtension()), 'ext' => $info->getExtension(), 'hash' => $info->hash(), 'path' => ltrim($info->getPathname(), '.'), 'size' => $info->getSize()]; // 保存文件信息 if ($insert = parent::create($data)) { return ['file_id' => $insert->id, 'path' => ltrim($insert->path, '.')]; } else { $this->error = '数据保存失败'; return false; } } else { $this->error = $File->getError(); return false; } } }