/** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if (method_exists($this, '_initialize')) { $this->_initialize(); } //导入类库 \think\Loader::import('vendor.Hprose.HproseHttpServer'); //实例化HproseHttpServer $server = new \HproseHttpServer(); if ($this->allowMethodList) { $methods = $this->allowMethodList; } else { $methods = get_class_methods($this); $methods = array_diff($methods, array('__construct', '__call', '_initialize')); } $server->addMethods($methods, $this); if (APP_DEBUG || $this->debug) { $server->setDebugEnabled(true); } // Hprose设置 $server->setCrossDomainEnabled($this->crossDomain); $server->setP3PEnabled($this->P3P); $server->setGetEnabled($this->get); // 启动server $server->start(); }
/** * 登陆 * @param string $callback 登陆成功后的回调地址 */ public function index($callback = '') { if (IS_POST) { $validate = Loader::validate('Login'); $data = $this->request->post(); if (config('verify_code')) { $validateResult = $validate->check($data); } else { $validateResult = $validate->scene('not_verify')->check($data); } if (!$validateResult) { return $this->error($validate->getError(), ''); } $user = Db::name('Member')->where('account', $data['account'])->find(); if (!$user) { return $this->error('用户不存在', ''); } elseif ($user['status'] != 1) { return $this->error('用户被禁用', ''); } elseif ($user['password'] != umd5($data['password'])) { logs('登陆失败:密码错误', '', $user['id']); return $this->error('密码错误', ''); } else { self::autoLogin($user); return $this->success('登陆成功', $callback ? $callback : url('system/index/index')); } } else { if (isLogin()) { $this->redirect(url('system/index/index')); } return view(); } }
/** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if (method_exists($this, '_initialize')) { $this->_initialize(); } //导入类库 \think\Loader::import('vendor.jsonrpc.jsonRPCServer'); // 启动server \jsonRPCServer::handle($this); }
/** * 利用TP核心的单图片上传方法 */ public function picture() { $Storage = Loader::model('Storage'); $options = ['ext' => ['jpg', 'gif', 'png', 'jpeg']]; $info = $Storage->upload('upload', $options); if (false !== $info) { return $this->success('上传成功', '', $info); } else { return $this->error($Storage->getError()); } }
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'); }
/** * [add description] */ public function add() { if (IS_POST) { $data = $this->request->post(); $validate = Loader::validate('Auth'); if (!$validate->check($data)) { return $this->error($validate->getError()); } $data = ['title' => $data['title'], 'create_time' => NOW_TIME, 'update_time' => 0, 'status' => $data['status'], 'remark' => $data['remark'], 'rules' => '']; if (Db::name('Auth')->insert($data)) { return $this->success(); } else { return $this->error(); } } else { return $this->fetch('edit'); } }
/** * 修改密码 */ public function password() { if (IS_POST) { $data = $this->request->post(); $validate = Loader::validate('Member'); if (!$validate->scene('changepass')->check($data)) { return $this->error($validate->getError()); } $passData = ['password' => umd5($data['newpass']), 'update_time' => NOW_TIME]; if (Db::name('Member')->where('id', UID)->update($passData)) { return $this->success('密码修改成功'); } else { return $this->error(); } } else { return $this->fetch(); } }
/** * [edit description] * @param integer $id [description] */ public function edit($id) { if (IS_POST) { $data = $this->request->post(); $validate = Loader::validate('Category'); if (!$validate->check($data)) { return $this->error($validate->getError()); } if (Loader::model('Category')->update($data)) { return $this->success(); } else { return $this->error(); } } else { $this->assign('info', Db::name('Category')->find($id)); $this->assign('upcate_list', Loader::model('Category')->treeSelect('', $id)); return $this->fetch('edit'); } }
/** * 编辑 * @param [type] $id 主键 */ public function edit($id) { if (IS_POST) { $data = $this->request->post(); $validate = Loader::validate('Menu'); if (!$validate->check($data)) { return $this->error($validate->getError()); } if (Loader::model('Menu')->update($data)) { session('system_menu_list', null); return $this->success(); } else { return $this->error(); } } else { $this->assign('up_menus', self::_treeShow($id)); $this->assign('info', Db::name('Menu')->where('id', $id)->find()); return $this->fetch(); } }
/** * 加载系统扩展配置 */ public static function load() { $config = \think\Cache::get('db_config_cache_data'); if (!$config) { // 在这里先判断一下数据库是否已经正确安装 $Db = \think\Loader::db(); $Query = $Db->query("SHOW TABLES LIKE '" . \think\Config::get('database.prefix') . "config'"); if (empty($Query)) { self::install(); } $data = \think\Db::name('Config')->where('status', 1)->field('type,name,value')->select(); $config = []; if ($data && is_array($data)) { foreach ($data as $value) { $config[$value['name']] = self::parse($value['type'], $value['value']); } } \think\Cache::set('db_config_cache_data', $config); } \think\Config::set($config); }
/** * 架构函数 * @access public */ public function __construct() { //控制器初始化 if (method_exists($this, '_initialize')) { $this->_initialize(); } //导入类库 \think\Loader::import('vendor.phprpc.phprpc_server'); //实例化phprpc $server = new \PHPRPC_Server(); if ($this->allowMethodList) { $methods = $this->allowMethodList; } else { $methods = get_class_methods($this); $methods = array_diff($methods, array('__construct', '__call', '_initialize')); } $server->add($methods, $this); if (APP_DEBUG || $this->debug) { $server->setDebugMode(true); } $server->setEnableGZIP(true); $server->start(); echo $server->comment(); }
/** * 利用__call方法实现一些特殊的Model方法 * @access public * @param string $method 方法名称 * @param array $args 调用参数 * @return mixed */ public function __call($method, $args) { if (in_array(strtolower($method), ['count', 'sum', 'min', 'max', 'avg'], true)) { // 统计查询的实现 $field = isset($args[0]) ? $args[0] : '*'; return $this->getField(strtoupper($method) . '(' . $field . ') AS tp_' . $method); } elseif (strtolower(substr($method, 0, 5)) == 'getby') { // 根据某个字段获取记录 $field = Loader::parseName(substr($method, 5)); $where[$field] = $args[0]; return $this->where($where)->find(); } elseif (strtolower(substr($method, 0, 10)) == 'getfieldby') { // 根据某个字段获取记录的某个值 $name = Loader::parseName(substr($method, 10)); $where[$name] = $args[0]; return $this->where($where)->getField($args[1]); } elseif (isset($this->scope[$method])) { // 命名范围的单独调用支持 return $this->scope($method, $args[0]); } else { throw new \think\Exception(__CLASS__ . ':' . $method . Lang::get('_METHOD_NOT_EXIST_')); return; } }
/** * 验证数据 * @access protected * @param array $data 数据 * @param string|array $validate 验证器名或者验证规则数组 * @param array $message 提示信息 * @param bool $batch 是否批量验证 * @param mixed $callback 回调方法(闭包) * @return array|string|true * @throws ValidateException */ protected function validate($data, $validate, $message = [], $batch = false, $callback = null) { if (is_array($validate)) { $v = Loader::validate(); $v->rule($validate); } else { if (strpos($validate, '.')) { // 支持场景 list($validate, $scene) = explode('.', $validate); } $v = Loader::validate($validate); if (!empty($scene)) { $v->scene($scene); } } // 是否批量验证 if ($batch || $this->batchValidate) { $v->batch(true); } if (is_array($message)) { $v->message($message); } if ($callback && is_callable($callback)) { call_user_func_array($callback, [$v, &$data]); } if (!$v->check($data)) { if ($this->failException) { throw new ValidateException($v->getError()); } else { return $v->getError(); } } else { return true; } }
/** * 快速导入第三方框架类库 所有第三方框架的类库文件统一放到 系统的Vendor目录下面 * @param string $class 类库 * @param string $ext 类库后缀 * @return boolean */ function vendor($class, $ext = EXT) { return Loader::import($class, VENDOR_PATH, $ext); }
/** * 自动定位模板文件 * @access private * @param string $template 模板文件规则 * @return string */ private function parseTemplate($template) { if (empty($this->config['view_path'])) { $this->config['view_path'] = App::$modulePath . 'view' . DS; } if (strpos($template, '@')) { list($module, $template) = explode('@', $template); $path = APP_PATH . $module . DS . 'view' . DS; } else { $path = $this->config['view_path']; } // 分析模板文件规则 $request = Request::instance(); $controller = Loader::parseName($request->controller()); if ($controller && 0 !== strpos($template, '/')) { $depr = $this->config['view_depr']; $template = str_replace(['/', ':'], $depr, $template); if ('' == $template) { // 如果模板文件名为空 按照默认规则定位 $template = str_replace('.', DS, $controller) . $depr . $request->action(); } elseif (false === strpos($template, $depr)) { $template = str_replace('.', DS, $controller) . $depr . $template; } } return $path . ltrim($template, '/') . '.' . ltrim($this->config['view_suffix'], '.'); }
protected static function parseUrl($url) { $request = Request::instance(); 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 = $request->module(); $module = $module ? $module . '/' : ''; $controller = $request->controller(); if ('' == $url) { // 空字符串输出当前的 模块/控制器/操作 $url = $module . $controller . '/' . $request->action(); } else { $path = explode('/', $url); $action = Config::get('url_convert') ? strtolower(array_pop($path)) : array_pop($path); $controller = empty($path) ? $controller : (Config::get('url_convert') ? Loader::parseName(array_pop($path)) : array_pop($path)); $module = empty($path) ? $module : array_pop($path) . '/'; $url = $module . $controller . '/' . $action; } } return $url; }
/** * 关联数据验证 * @access protected * @param mixed $data 数据对象 * @param string $name 关联名称 * @return mixed */ protected function crRelation(&$data, $name = '') { if (empty($data) && !empty($this->data)) { $data = $this->data; } elseif (!is_array($data)) { // 数据无效返回 return false; } if (!empty($this->_link)) { $fields = $this->getFields(); // 遍历关联定义 foreach ($this->_link as $key => $val) { // 操作制定关联类型 $mappingName = !empty($val['mapping_name']) ? $val['mapping_name'] : $key; // 映射名称 if (empty($name) || true === $name || $mappingName == $name || is_array($name) && in_array($mappingName, $name)) { // 操作制定的关联 $mappingType = !empty($val['mapping_type']) ? $val['mapping_type'] : $val; // 关联类型 $mappingClass = !empty($val['class_name']) ? $val['class_name'] : $key; // 关联类名 $mappingKey = !empty($val['mapping_key']) ? $val['mapping_key'] : $this->getPk(); // 关联键名 if (strtoupper($mappingClass) == strtoupper($this->name) || !isset($data[$mappingName])) { continue; // 自引用关联或提交关联数据跳过 } // 获取关联model对象 $model = \think\Loader::model($mappingClass); $_data = $data[$mappingName]; unset($data[$key]); if ($_data = $model->token(false)->create($_data)) { $data[$mappingName] = $_data; $fields[] = $mappingName; } else { $error = $model->getError(); if ($this->patchValidate) { $this->error[$mappingName] = $error; } else { $this->error = $error; return false; } } } } // 过滤非法字段数据 $diff = array_diff(array_keys($data), $fields); foreach ($diff as $key) { unset($data[$key]); } } }
// 配置文件目录 defined('CONF_EXT') or define('CONF_EXT', EXT); // 配置文件后缀 defined('ENV_PREFIX') or define('ENV_PREFIX', 'PHP_'); // 环境变量的配置前缀 // 环境常量 define('IS_CLI', PHP_SAPI == 'cli' ? true : false); define('IS_WIN', strpos(PHP_OS, 'WIN') !== false); // 载入Loader类 require CORE_PATH . 'Loader.php'; // 加载环境变量配置文件 if (is_file(ROOT_PATH . '.env')) { $env = parse_ini_file(ROOT_PATH . '.env', true); foreach ($env as $key => $val) { $name = ENV_PREFIX . strtoupper($key); if (is_array($val)) { foreach ($val as $k => $v) { $item = $name . '_' . strtoupper($k); putenv("{$item}={$v}"); } } else { putenv("{$name}={$val}"); } } } // 注册自动加载 \think\Loader::register(); // 注册错误和异常处理机制 \think\Error::register(); // 加载惯例配置文件 \think\Config::set(include THINK_PATH . 'convention' . EXT);
<?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 <*****@*****.**> // +---------------------------------------------------------------------- namespace think; \think\Loader::import('controller/Jump', TRAIT_PATH, EXT); use think\Exception; use think\exception\ValidateException; class Controller { use \traits\controller\Jump; // 视图类实例 protected $view; // Request实例 protected $request; // 验证失败是否抛出异常 protected $failException = false; // 是否批量验证 protected $batchValidate = false; /** * 前置操作方法列表 * @var array $beforeActionList * @access protected
<?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);
/** * 解析规则路由 * @access private * @param string $rule 路由规则 * @param string $route 路由地址 * @param string $pathinfo URL地址 * @param array $option 路由参数 * @param array $matches 匹配的变量 * @return array */ private static function parseRule($rule, $route, $pathinfo, $option = [], $matches = []) { $request = Request::instance(); // 解析路由规则 if ($rule) { $rule = explode('/', $rule); // 获取URL地址中的参数 $paths = explode('|', $pathinfo); foreach ($rule as $item) { $fun = ''; if (0 === strpos($item, '[:')) { $item = substr($item, 1, -1); } if (0 === strpos($item, ':')) { $var = substr($item, 1); $matches[$var] = array_shift($paths); } else { // 过滤URL中的静态变量 array_shift($paths); } } } else { $paths = explode('|', $pathinfo); } // 获取路由地址规则 if (is_string($route) && isset($option['prefix'])) { // 路由地址前缀 $route = $option['prefix'] . $route; } // 替换路由地址中的变量 if (is_string($route) && !empty($matches)) { foreach ($matches as $key => $val) { if (false !== strpos($route, ':' . $key)) { $route = str_replace(':' . $key, $val, $route); unset($matches[$key]); } } } // 绑定模型数据 if (isset($option['bind_model'])) { $bind = []; foreach ($option['bind_model'] as $key => $val) { if ($val instanceof \Closure) { $result = call_user_func_array($val, [$matches]); } else { if (is_array($val)) { $fields = explode('&', $val[1]); $model = $val[0]; $exception = isset($val[2]) ? $val[2] : true; } else { $fields = ['id']; $model = $val; $exception = true; } $where = []; $match = true; foreach ($fields as $field) { if (!isset($matches[$field])) { $match = false; break; } else { $where[$field] = $matches[$field]; } } if ($match) { $query = strpos($model, '\\') ? $model::where($where) : Loader::model($model)->where($where); $result = $query->failException($exception)->find(); } } if (!empty($result)) { $bind[$key] = $result; } } $request->bind($bind); } // 解析额外参数 self::parseUrlParams(empty($paths) ? '' : implode('|', $paths), $matches); // 记录匹配的路由信息 $request->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]); // 检测路由after行为 if (!empty($option['after_behavior'])) { if ($option['after_behavior'] instanceof \Closure) { $result = call_user_func_array($option['after_behavior'], []); } else { foreach ((array) $option['after_behavior'] as $behavior) { $result = Hook::exec($behavior, ''); if (!is_null($result)) { break; } } } // 路由规则重定向 if ($result instanceof Response) { return ['type' => 'response', 'response' => $result]; } elseif (is_array($result)) { return $result; } } if ($route instanceof \Closure) { // 执行闭包 $result = ['type' => 'function', 'function' => $route]; } elseif (0 === strpos($route, '/') || strpos($route, '://')) { // 路由到重定向地址 $result = ['type' => 'redirect', 'url' => $route, 'status' => isset($option['status']) ? $option['status'] : 301]; } elseif (false !== strpos($route, '\\')) { // 路由到方法 $route = str_replace('/', '@', $route); $method = strpos($route, '@') ? explode('@', $route) : $route; $result = ['type' => 'method', 'method' => $method]; } elseif (0 === strpos($route, '@')) { // 路由到控制器 $result = ['type' => 'controller', 'controller' => substr($route, 1)]; } else { // 路由到模块/控制器/操作 $result = self::parseModule($route); } // 开启请求缓存 if ($request->isGet() && !empty($option['cache'])) { $cache = $option['cache']; if (is_array($cache)) { list($key, $expire) = $cache; } else { $key = $pathinfo; $expire = $cache; } $request->cache($key, $expire); } return $result; }
/** * 初始化模块 * @access private * @return void */ private static function initModule($path, &$config) { // 加载初始化文件 if (is_file($path . 'init' . EXT)) { include $path . 'init' . EXT; } else { // 检测配置文件 if (is_file($path . 'config' . EXT)) { $config = Config::set(include $path . 'config' . EXT); } // 检测额外配置 if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $conf) { if (is_file($path . $conf . EXT)) { $config = Config::set(include $path . $conf . EXT); } } } // 加载应用状态配置文件 if ($config['app_status'] && is_file($path . $config['app_status'] . EXT)) { $config = Config::set(include $path . $config['app_status'] . EXT); } // 加载别名文件 if (is_file($path . 'alias' . EXT)) { Loader::addMap(include $path . 'alias' . EXT); } // 加载公共文件 if (is_file($path . 'common' . EXT)) { include $path . 'common' . EXT; } // 加载行为扩展文件 if (is_file($path . 'tags' . EXT)) { Hook::import(include $path . 'tags' . EXT); } } }
private static function initModule($module, $config) { // 定位模块目录 $module = COMMON_MODULE == $module || !APP_MULTI_MODULE ? '' : $module . DS; // 加载初始化文件 if (is_file(APP_PATH . $module . 'init' . EXT)) { include APP_PATH . $module . 'init' . EXT; } else { $path = APP_PATH . $module; // 加载模块配置 $config = Config::load(APP_PATH . $module . 'config' . CONF_EXT); // 加载应用状态配置 if ($config['app_status']) { $config = Config::load(APP_PATH . $module . $config['app_status'] . CONF_EXT); } // 读取扩展配置文件 if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $name => $file) { $filename = $path . $file . CONF_EXT; Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME)); } } // 加载别名文件 if (is_file($path . 'alias' . EXT)) { Loader::addMap(include $path . 'alias' . EXT); } // 加载行为扩展文件 if (APP_HOOK && is_file($path . 'tags' . EXT)) { Hook::import(include $path . 'tags' . EXT); } // 加载公共文件 if (is_file($path . 'common' . EXT)) { include $path . 'common' . EXT; } // 加载当前模块语言包 if ($config['lang_switch_on'] && $module) { Lang::load($path . 'lang' . DS . LANG_SET . EXT); } } }
/** * 设置关联查询JOIN预查询 * @access public * @param string|array $with 关联方法名称 * @return $this */ public function with($with) { if (empty($with)) { return $this; } if (is_string($with)) { $with = explode(',', $with); } $i = 0; $currentModel = $this->model; /** @var Model $class */ $class = new $currentModel(); foreach ($with as $key => $relation) { $closure = false; if ($relation instanceof \Closure) { // 支持闭包查询过滤关联条件 $closure = $relation; $relation = $key; $with[$key] = $key; } elseif (is_string($relation) && strpos($relation, '.')) { $with[$key] = $relation; list($relation, $subRelation) = explode('.', $relation, 2); } /** @var Relation $model */ $model = $class->{$relation}(); $info = $model->getRelationInfo(); if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) { if (0 == $i) { $name = Loader::parseName(basename(str_replace('\\', '/', $currentModel))); $table = $this->getTable(); $alias = isset($info['alias'][$name]) ? $info['alias'][$name] : $name; $this->table($table)->alias($alias); if (isset($this->options['field'])) { $field = $this->options['field']; unset($this->options['field']); } else { $field = true; } $this->field($field, false, $table, $alias); } // 预载入封装 $joinTable = $model->getTable(); $joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); $joinAlias = isset($info['alias'][$joinName]) ? $info['alias'][$joinName] : $joinName; $this->via($joinAlias); if (Relation::HAS_ONE == $info['type']) { $this->join($joinTable . ' ' . $joinAlias, $alias . '.' . $info['localKey'] . '=' . $joinAlias . '.' . $info['foreignKey'], $info['joinType']); } else { $this->join($joinTable . ' ' . $joinAlias, $alias . '.' . $info['foreignKey'] . '=' . $joinAlias . '.' . $info['localKey'], $info['joinType']); } if ($closure) { // 执行闭包查询 call_user_func_array($closure, [&$this]); //指定获取关联的字段 //需要在 回调中 调方法 withField 方法,如 // $query->where(['id'=>1])->withField('id,name'); if (!empty($this->options['with_field'])) { $field = $this->options['with_field']; unset($this->options['with_field']); } } $this->field($field, false, $joinTable, $joinAlias, $relation . '__'); $i++; } elseif ($closure) { $with[$key] = $closure; } } $this->via(); $this->options['with'] = $with; return $this; }
/** * 渲染输出Widget * @param string $name Widget名称 * @param array $data 传人的参数 * @return mixed */ function W($name, $data = []) { return \think\Loader::action($name, $data, 'Widget'); }
/** * 初始化应用或模块 * @access public * @param string $module 模块名 * @return array */ private static function init($module = '') { // 定位模块目录 $module = $module ? $module . DS : ''; // 加载初始化文件 if (is_file(APP_PATH . $module . 'init' . EXT)) { include APP_PATH . $module . 'init' . EXT; } else { $path = APP_PATH . $module; // 加载模块配置 $config = Config::load(CONF_PATH . $module . 'config' . CONF_EXT); // 加载应用状态配置 if ($config['app_status']) { $config = Config::load(CONF_PATH . $module . $config['app_status'] . CONF_EXT); } // 读取扩展配置文件 if ($config['extra_config_list']) { foreach ($config['extra_config_list'] as $name => $file) { $filename = CONF_PATH . $module . $file . CONF_EXT; Config::load($filename, is_string($name) ? $name : pathinfo($filename, PATHINFO_FILENAME)); } } // 加载别名文件 if (is_file(CONF_PATH . $module . 'alias' . EXT)) { Loader::addClassMap(include CONF_PATH . $module . 'alias' . EXT); } // 加载行为扩展文件 if (is_file(CONF_PATH . $module . 'tags' . EXT)) { Hook::import(include CONF_PATH . $module . 'tags' . EXT); } // 加载公共文件 if (is_file($path . 'common' . EXT)) { include $path . 'common' . EXT; } // 加载当前模块语言包 if ($config['lang_switch_on'] && $module) { Lang::load($path . 'lang' . DS . Request::instance()->langset() . EXT); } } return Config::get(); }
/** * BELONGS TO MANY 关联定义 * @access public * @param string $model 模型名 * @param string $table 中间表名 * @param string $foreignKey 关联外键 * @param string $localKey 当前模型关联键 * @param array $alias 别名定义 * @return Relation */ public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '', $alias = []) { // 记录当前关联信息 $model = $this->parseModel($model); $name = Loader::parseName(basename(str_replace('\\', '/', $model))); $table = $table ?: $this->db()->getTable(Loader::parseName($this->name) . '_' . $name); $foreignKey = $foreignKey ?: $name . '_id'; $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; return $this->relation()->belongsToMany($model, $table, $foreignKey, $localKey, $alias); }
/** * 检查是否定义了所有字段 * @access protected * @param string $name 模型名称 * @param array $fields 字段数组 * @return array */ private function _checkFields($name, $fields) { if (false !== ($pos = array_search('*', $fields))) { // 定义所有字段 $fields = array_merge($fields, \think\Loader::model($name)->getFields()); unset($fields[$pos]); } return $fields; }
/** * 得到完整的数据表名 Mongo表名不带dbName * @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 $this->trueTableName; }
/** * 编辑配置 * @param integer $id 配置主键 */ public function edit($id) { if (IS_POST) { $data = $this->request->post(); $validate = Loader::validate('Config'); if (!$validate->check($data)) { return $this->error($validate->getError()); } if (Loader::model('Config')->update($data)) { Cache::clear(); return $this->success(); } else { return $this->error(); } } else { $this->assign('info', Db::name('Config')->find($id)); return $this->fetch(); } }