/** * 数据库初始化,DB切换入口 * 1. 可以在使用中通过$this->init_db('test')来切换数据库 * 2. 该函数是DB默认初始化入口 * 3. 支持多数据库链接,主从,随机分布式数据库 * @param obj $db */ public function init_db($db = '') { $InitPHP_conf = InitPHP::getConfig(); $this->dbModel = $db == '' ? 'default' : $db; //Db模型 $driver = $InitPHP_conf['Db']['Driver']; //Db引擎 if (isset(self::$dbArr[$this->dbModel])) { return true; } if (!isset($InitPHP_conf['Db'][$this->dbModel])) { InitPHP::initError('database confing model {' . $this->dbModel . '} is error!'); } $db_type = $InitPHP_conf['Db'][$this->dbModel]['db_type']; $config = $InitPHP_conf['Db'][$this->dbModel]; switch ($db_type) { case 1: //主从模型 $key = floor(mt_rand(1, count($config) - 2)); self::$dbArr[$this->dbModel]['master']['link_id'] = $this->db_connect($config[0], $driver); self::$dbArr[$this->dbModel]['salver']['link_id'] = $this->db_connect($config[$key], $driver); break; case 2: //随机模型 $key = floor(mt_rand(0, count($config) - 2)); self::$dbArr[$this->dbModel]['link_id'] = $this->db_connect($config[$key], $driver); break; default: //默认单机模型 self::$dbArr[$this->dbModel]['link_id'] = $this->db_connect($config[0], $driver); break; } return true; }
/** * 重写MYSQL中的QUERY,对SQL语句进行监控 * @param string $sql */ public function query($sql, $is_set_default = true) { $this->get_link_id($sql); //link_id获取 $InitPHP_conf = InitPHP::getConfig(); if ($InitPHP_conf['is_debug'] == true) { $start = microtime(); } $query = $this->db->query($sql); if ($InitPHP_conf['is_debug'] == true) { $end = microtime(); } //sql query debug if ($InitPHP_conf['is_debug'] == true) { $k = count($InitPHP_conf['sqlcontrolarr']); $InitPHP_conf['sqlcontrolarr'][$k]['sql'] = $sql; $costTime = substr($end - $start, 0, 7); $InitPHP_conf['sqlcontrolarr'][$k]['queryTime'] = $costTime; $InitPHP_conf['sqlcontrolarr'][$k]['affectedRows'] = $this->affected_rows(); InitPHP::setConfig('sqlcontrolarr', $InitPHP_conf['sqlcontrolarr']); } if ($this->db->error()) { InitPHP::initError($this->db->error()); } if ($is_set_default) { $this->set_default_link_id(); } //设置默认的link_id return $query; }
/** * Error机制 私有函数,error输出 * @param string $error_type 错误类型 * @return object */ private function display($error_type) { $InitPHP_conf = InitPHP::getConfig(); if ($error_type == 'text') { $error = implode("\r\t", $this->error_data); exit($error); } elseif ($error_type == 'json') { exit(json_encode($this->error_data)); } elseif ($error_type == 'xml') { $xml = '<?xml version="1.0" encoding="utf-8"?>'; $xml .= '<return>'; foreach ($this->error_data as $v) { $xml .= '<error>' . $v . '</error>'; } $xml .= '</return>'; exit($xml); } elseif ($error_type == 'array') { exit(var_export($this->error_data)); } elseif ($error_type == 'html') { $error = $this->error_data; //$template = InitPHP::getAppPath($InitPHP_conf['error']['template']); $template = $InitPHP_conf['error']['template']; if ($template) { if (!file_exists($template)) { InitPHP::initError('error template is not exist'); } @(include $template); } else { InitPHP::initError('please set error template in initphp.conf.php'); } //扩展HTML错误输出 exit; } }
/** * MYSQL连接器 * @param string $host sql服务器 * @param string $user 数据库用户名 * @param string $password 数据库登录密码 * @param string $database 数据库 * @param string $charset 编码 * @param string $pconnect 是否持久链接 * @return obj */ public function connect($host, $user, $password, $database, $charset = 'utf8', $pconnect = 0) { $link_id = $pconnect == 0 ? mysql_connect($host, $user, $password, true) : mysql_pconnect($host, $user, $password); if (!$link_id) { InitPHP::initError('mysql connect error!'); } mysql_query('SET NAMES ' . $charset, $link_id); if (!mysql_select_db($database, $link_id)) { InitPHP::initError('database is not exist!'); } return $link_id; }
/** * MYSQL连接器 * @param string $host sql服务器 * @param string $user 数据库用户名 * @param string $password 数据库登录密码 * @param string $database 数据库 * @param string $charset 编码 * @param string $pconnect 是否持久链接 * @return obj */ public function connect($host, $user, $password, $database, $charset = 'utf8', $pconnect = 0) { $port = 3306; //设置默认值 if (strpos($host, ":")) { $arr = explode(":", $host); $host = $arr[0]; $port = $arr[1]; } $link_id = $pconnect == 0 ? mysqli_connect($host, $user, $password, $database, $port) : mysqli_pconnect($host, $user, $password, $database, $port); if (!$link_id) { InitPHP::initError("mysqli connect error"); } mysqli_query($link_id, 'SET NAMES ' . $charset); return $link_id; }
/** * 框架核心加载-框架的所有类都需要通过该函数出去 * 1. 单例模式 * 2. 可以加载-Controller,Service,View,Dao,Util,Library中的类文件 * 3. 框架加载核心函数 * 使用方法:$this->load($class_name, $type) * @param string $class_name 类名称 * @param string $type 类别 */ public function load($class_name, $type) { $class_path = $this->get_class_path($class_name, $type); $class_name = $this->get_class_name($class_name); if (!file_exists($class_path)) { \InitPHP::initError('file ' . $class_name . '.php is not exist!'); } if (!isset(self::$instance['initphp'][$type][$class_name])) { require_once $class_path; if (!class_exists($class_name)) { InitPHP::initError('class' . $class_name . ' is not exist!'); } $init_class = new $class_name(); self::$instance['initphp'][$type][$class_name] = $init_class; } return self::$instance['initphp'][$type][$class_name]; }
/** * 模板-显示视图 * 1. 在Controller中需要显示模板,就必须调用该函数 * 2. 模板解析可以设置 $InitPHP_conf['isviewfilter'] 值,对变量进行过滤 * Controller中使用方法:$this->view->display(); * @return array */ public function display($template = '') { if ($template != '') { $this->set_tpl($template); } $InitPHP_conf = InitPHP::getConfig(); if (is_array($this->view)) { if ($InitPHP_conf['isviewfilter']) { $this->out_put($this->view); } foreach ($this->view as $key => $val) { ${$key} = $val; } } $this->template_arr = $this->parse_template_arr($this->template_arr); //模板设置 foreach ($this->template_arr as $file_name) { if (in_array($file_name, $this->remove_tpl_arr)) { continue; } $complie_file_name = $this->template_run($file_name); //模板编译 if (!file_exists($complie_file_name)) { InitPHP::initError($complie_file_name . ' is not exist!'); } include_once $complie_file_name; } }
/** * 调用其它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); } }
/** * 框架运行控制器中的Action函数 * 1. 获取Action中的a参数 * 2. 检测是否在白名单中,不在则选择默认的 * 3. 检测方法是否存在,不存在则运行默认的 * 4. 运行函数 * @param object $controller 控制器对象 * @return file */ private function run_action($controller) { $action = trim($_GET['a']); $action = $action . $this->action_postfix; if (!method_exists($controller, $action)) { InitPHP::initError('Can not find default method : ' . $action); } /* REST形式访问 */ $controller->{$action}(); }