From 15a45e9ff79f23ce64d23b9761b01a330677bf92 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 9 Apr 2013 21:29:32 +0800 Subject: [PATCH] =?UTF-8?q?Session=E9=A9=B1=E5=8A=A8=E7=9A=84=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=AE=9A=E4=B9=89=E6=96=B9=E5=BC=8F=E6=94=B9=E5=8F=98?= =?UTF-8?q?=20=E9=87=87=E7=94=A8SessionHandler=E6=9C=BA=E5=88=B6=20Model?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E6=9E=B6=E6=9E=84=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Think/Behavior/LocationTemplate.php | 4 +- Think/Config.php | 2 +- Think/Loader.php | 9 ++-- Think/Model.php | 82 ++++++++++++++++------------- Think/Seesion/Driver.php | 21 ++++++++ Think/Session.php | 8 +-- Think/Tag.php | 1 - Think/View/Driver/Think.php | 1 - 8 files changed, 75 insertions(+), 53 deletions(-) create mode 100644 Think/Seesion/Driver.php diff --git a/Think/Behavior/LocationTemplate.php b/Think/Behavior/LocationTemplate.php index 149c75e4..db34c674 100644 --- a/Think/Behavior/LocationTemplate.php +++ b/Think/Behavior/LocationTemplate.php @@ -42,9 +42,7 @@ class LocationTemplate { }elseif(false === strpos($template,'.')) { $template = $template; } - $templateFile = MODULE_PATH.'view/'.$template.'.html'; - if(!is_file($templateFile)) - E(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']'); + $templateFile = MODULE_PATH.'View/'.$template.'.html'; return $templateFile; } } \ No newline at end of file diff --git a/Think/Config.php b/Think/Config.php index acd64f20..6684bac8 100644 --- a/Think/Config.php +++ b/Think/Config.php @@ -21,7 +21,7 @@ class Config { // 解析其他格式的配置参数 static public function parse($config,$type='',$range=''){ if(empty($type)) { - $type = strtolower(substr(strrchr($config, '.'),1)); + $type = substr(strrchr($config, '.'),1); } $class = '\Think\Config\Driver\\'.ucwords($type); self::set((new $class())->parse($config),'',$range); diff --git a/Think/Loader.php b/Think/Loader.php index 1d0d9727..576ed68d 100644 --- a/Think/Loader.php +++ b/Think/Loader.php @@ -18,6 +18,7 @@ class Loader { static protected $namespace = [ 'Think' => CORE_PATH, 'Vendor' => VENDOR_PATH, + 'Library' => LIB_PATH, ]; // 自动加载 @@ -118,7 +119,7 @@ class Loader { } $guid = $tablePrefix . $name . '_' . $class; if (!isset($_model[$guid])) - $_model[$guid] = new $class($name,$tablePrefix,$connection); + $_model[$guid] = new $class($name,['table_prefix'=>$tablePrefix,'connection'=>$connection]); return $_model[$guid]; } @@ -129,7 +130,7 @@ class Loader { * @return Object */ static public function model($name='',$layer='Model') { - if(empty($name)) return new Model; + if(empty($name)) return new Think\Model; static $_model = []; if(isset($_model[$name.$layer])) return $_model[$name.$layer]; if(strpos($name,'/')) { @@ -141,14 +142,14 @@ class Loader { if(class_exists($class)) { $model = new $class($name); }else { - $model = new Model($name); + $model = new Think\Model($name); } $_model[$name.$layer] = $model; return $model; } /** - * 实例化(分层)控制器 格式:[分组/]模块 + * 实例化(分层)控制器 格式:[模块名/]控制器名 * @param string $name 资源地址 * @param string $layer 控制层名称 * @return Action|false diff --git a/Think/Model.php b/Think/Model.php index 3d69f6d9..75112b0a 100644 --- a/Think/Model.php +++ b/Think/Model.php @@ -54,39 +54,47 @@ class Model { // 链操作方法列表 protected $methods = ['table','order','alias','having','group','lock','distinct','auto','filter','validate']; // 配置参数 - protected $config = []; + protected $config = []; /** * 架构函数 * 取得DB类的实例对象 字段检查 * @access public * @param string $name 模型名称 - * @param string $tablePrefix 表前缀 - * @param mixed $connection 数据库连接信息 + * @param array $config 模型配置 */ - public function __construct($name='',$tablePrefix='',$connection='') { + public function __construct($name='',$config=[]) { // 模型初始化 $this->_initialize(); - // 读取配置参数 - $this->config = Config::get(); - - // 获取模型名称 - if(!empty($name)) { - if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义 - list($this->dbName,$this->name) = explode('.',$name); - }else{ - $this->name = $name; - } + // 传入模型参数 + if(!empty($name)){ + $this->name = $name; }elseif(empty($this->name)){ $this->name = $this->getModelName(); } + if(strpos($this->name,'.')) { // 支持 数据库名.模型名的 定义 + list($this->dbName,$this->name) = explode('.',$this->name); + } + + if(isset($config['table_prefix'])) { + $this->tablePrefix = $config['table_prefix']; + } + if(isset($config['connection'])) { + $this->connection = $config['connection']; + } + if(isset($config['table_name'])) { + $this->tableName = $config['table_name']; + } + if(isset($config['true_table_name'])) { + $this->trueTableName = $config['true_table_name']; + } + if(isset($config['db_name'])) { + $this->dbName = $config['db_name']; + } + // 设置表前缀 - if(is_null($tablePrefix)) {// 前缀为Null表示没有前缀 - $this->tablePrefix = ''; - }elseif('' != $tablePrefix) { - $this->tablePrefix = $tablePrefix; - }else{ - $this->tablePrefix = $this->tablePrefix?$this->tablePrefix:$this->config['db_prefix']; + if(empty($this->tablePrefix)) { + $this->tablePrefix = is_null($this->tablePrefix)?'':C('db_prefix'); } // 数据库初始化操作 @@ -149,7 +157,7 @@ class Model { // 连贯操作的实现 $this->options[strtolower($method)] = $args[0]; return $this; - }elseif(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){ + }elseif(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); @@ -361,7 +369,7 @@ class Model { // 根据主键删除记录 $pk = $this->getPk(); if(strpos($options,',')) { - $where[$pk] = array('IN', $options); + $where[$pk] = ['IN', $options]; }else{ $where[$pk] = $options; } @@ -394,7 +402,7 @@ class Model { // 根据主键查询 $pk = $this->getPk(); if(strpos($options,',')) { - $where[$pk] = array('IN',$options); + $where[$pk] = ['IN',$options]; }else{ $where[$pk] = $options; } @@ -489,13 +497,15 @@ class Model { * @return void */ protected function _parseType(&$data,$key) { - $fieldType = strtolower($this->fields['_type'][$key]); - if(false === strpos($fieldType,'bigint') && false !== strpos($fieldType,'int')) { - $data[$key] = intval($data[$key]); - }elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){ - $data[$key] = floatval($data[$key]); - }elseif(false !== strpos($fieldType,'bool')){ - $data[$key] = (bool)$data[$key]; + if(isset($this->fields['_type'][$key])) { + $fieldType = strtolower($this->fields['_type'][$key]); + if(false === strpos($fieldType,'bigint') && false !== strpos($fieldType,'int')) { + $data[$key] = intval($data[$key]); + }elseif(false !== strpos($fieldType,'float') || false !== strpos($fieldType,'double')){ + $data[$key] = floatval($data[$key]); + }elseif(false !== strpos($fieldType,'bool')){ + $data[$key] = (bool)$data[$key]; + } } } @@ -581,7 +591,7 @@ class Model { * @return boolean */ public function setInc($field,$step=1) { - return $this->setField($field,array('exp',$field.'+'.$step)); + return $this->setField($field,['exp',$field.'+'.$step]); } /** @@ -592,7 +602,7 @@ class Model { * @return boolean */ public function setDec($field,$step=1) { - return $this->setField($field,array('exp',$field.'-'.$step)); + return $this->setField($field,['exp',$field.'-'.$step]); } /** @@ -756,7 +766,7 @@ class Model { }elseif(is_array($parse)){ // SQL预处理 $sql = vsprintf($sql,$parse); }else{ - $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->config['DB_PREFIX'])); + $sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]); } $this->db->setModel($this->name); return $sql; @@ -779,7 +789,7 @@ class Model { if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) { // 创建一个新的实例 if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数 - $config = Config::get($config); + $config = C($config); } $_db[$linkNum] = Db::instance($config); }elseif(NULL === $config){ @@ -1023,7 +1033,7 @@ class Model { */ public function cache($key=true,$expire=null,$type=''){ if(false !== $key) - $this->options['cache'] = array('key'=>$key,'expire'=>$expire,'type'=>$type); + $this->options['cache'] = ['key'=>$key,'expire'=>$expire,'type'=>$type]; return $this; } @@ -1097,7 +1107,7 @@ class Model { $parse = func_get_args(); array_shift($parse); } - $parse = array_map(array($this->db,'escapeString'),$parse); + $parse = array_map([$this->db,'escapeString'],$parse); $where = vsprintf($where,$parse); }elseif(is_object($where)){ $where = get_object_vars($where); diff --git a/Think/Seesion/Driver.php b/Think/Seesion/Driver.php new file mode 100644 index 00000000..2d3d47f4 --- /dev/null +++ b/Think/Seesion/Driver.php @@ -0,0 +1,21 @@ + +// +---------------------------------------------------------------------- +// $Id$ +namespace Think\Session\Driver; +use SessionHandler; +class Driver extends SessionHandler { + protected $config = []; + + public function __construct($config=[]){ + $this->config = array_merge($this->config,$config); + } + +} \ No newline at end of file diff --git a/Think/Session.php b/Think/Session.php index 72c311a4..b5e19918 100644 --- a/Think/Session.php +++ b/Think/Session.php @@ -51,13 +51,7 @@ class Session { if(!empty($config['type'])) { // 读取session驱动 $class = 'Think\\Session\\Driver\\'. ucwords(strtolower($config['type'])); // 检查驱动类 - if(class_exists($class)) { - $hander = new $class(); - $hander->execute(); - }else { - // 类没有定义 - E(L('_CLASS_NOT_EXIST_').': ' . $class); - } + session_set_save_handler(new $class()); } // 启动session if($config['auto_start']) session_start(); diff --git a/Think/Tag.php b/Think/Tag.php index 7073b7c3..e05bfb64 100644 --- a/Think/Tag.php +++ b/Think/Tag.php @@ -54,7 +54,6 @@ class Tag { // 如果返回false 则中断行为执行 return ; } - } } return; diff --git a/Think/View/Driver/Think.php b/Think/View/Driver/Think.php index 0f1929d2..36111959 100644 --- a/Think/View/Driver/Think.php +++ b/Think/View/Driver/Think.php @@ -24,5 +24,4 @@ class Think { $this->template->fetch($template,$data); } } - } \ No newline at end of file