Session驱动的扩展定义方式改变 采用SessionHandler机制

Model类的架构方法 参数调整
This commit is contained in:
thinkphp
2013-04-09 21:29:32 +08:00
parent 279ce15d21
commit 15a45e9ff7
8 changed files with 75 additions and 53 deletions

View File

@@ -42,9 +42,7 @@ class LocationTemplate {
}elseif(false === strpos($template,'.')) { }elseif(false === strpos($template,'.')) {
$template = $template; $template = $template;
} }
$templateFile = MODULE_PATH.'view/'.$template.'.html'; $templateFile = MODULE_PATH.'View/'.$template.'.html';
if(!is_file($templateFile))
E(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
return $templateFile; return $templateFile;
} }
} }

View File

@@ -21,7 +21,7 @@ class Config {
// 解析其他格式的配置参数 // 解析其他格式的配置参数
static public function parse($config,$type='',$range=''){ static public function parse($config,$type='',$range=''){
if(empty($type)) { if(empty($type)) {
$type = strtolower(substr(strrchr($config, '.'),1)); $type = substr(strrchr($config, '.'),1);
} }
$class = '\Think\Config\Driver\\'.ucwords($type); $class = '\Think\Config\Driver\\'.ucwords($type);
self::set((new $class())->parse($config),'',$range); self::set((new $class())->parse($config),'',$range);

View File

@@ -18,6 +18,7 @@ class Loader {
static protected $namespace = [ static protected $namespace = [
'Think' => CORE_PATH, 'Think' => CORE_PATH,
'Vendor' => VENDOR_PATH, 'Vendor' => VENDOR_PATH,
'Library' => LIB_PATH,
]; ];
// 自动加载 // 自动加载
@@ -118,7 +119,7 @@ class Loader {
} }
$guid = $tablePrefix . $name . '_' . $class; $guid = $tablePrefix . $name . '_' . $class;
if (!isset($_model[$guid])) if (!isset($_model[$guid]))
$_model[$guid] = new $class($name,$tablePrefix,$connection); $_model[$guid] = new $class($name,['table_prefix'=>$tablePrefix,'connection'=>$connection]);
return $_model[$guid]; return $_model[$guid];
} }
@@ -129,7 +130,7 @@ class Loader {
* @return Object * @return Object
*/ */
static public function model($name='',$layer='Model') { static public function model($name='',$layer='Model') {
if(empty($name)) return new Model; if(empty($name)) return new Think\Model;
static $_model = []; static $_model = [];
if(isset($_model[$name.$layer])) return $_model[$name.$layer]; if(isset($_model[$name.$layer])) return $_model[$name.$layer];
if(strpos($name,'/')) { if(strpos($name,'/')) {
@@ -141,14 +142,14 @@ class Loader {
if(class_exists($class)) { if(class_exists($class)) {
$model = new $class($name); $model = new $class($name);
}else { }else {
$model = new Model($name); $model = new Think\Model($name);
} }
$_model[$name.$layer] = $model; $_model[$name.$layer] = $model;
return $model; return $model;
} }
/** /**
* 实例化(分层)控制器 格式:[分组/]模块 * 实例化(分层)控制器 格式:[模块名/]控制器名
* @param string $name 资源地址 * @param string $name 资源地址
* @param string $layer 控制层名称 * @param string $layer 控制层名称
* @return Action|false * @return Action|false

View File

@@ -61,32 +61,40 @@ class Model {
* 取得DB类的实例对象 字段检查 * 取得DB类的实例对象 字段检查
* @access public * @access public
* @param string $name 模型名称 * @param string $name 模型名称
* @param string $tablePrefix 表前缀 * @param array $config 模型配置
* @param mixed $connection 数据库连接信息
*/ */
public function __construct($name='',$tablePrefix='',$connection='') { public function __construct($name='',$config=[]) {
// 模型初始化 // 模型初始化
$this->_initialize(); $this->_initialize();
// 读取配置参数 // 传入模型参数
$this->config = Config::get();
// 获取模型名称
if(!empty($name)){ if(!empty($name)){
if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义
list($this->dbName,$this->name) = explode('.',$name);
}else{
$this->name = $name; $this->name = $name;
}
}elseif(empty($this->name)){ }elseif(empty($this->name)){
$this->name = $this->getModelName(); $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表示没有前缀 if(empty($this->tablePrefix)) {
$this->tablePrefix = ''; $this->tablePrefix = is_null($this->tablePrefix)?'':C('db_prefix');
}elseif('' != $tablePrefix) {
$this->tablePrefix = $tablePrefix;
}else{
$this->tablePrefix = $this->tablePrefix?$this->tablePrefix:$this->config['db_prefix'];
} }
// 数据库初始化操作 // 数据库初始化操作
@@ -149,7 +157,7 @@ class Model {
// 连贯操作的实现 // 连贯操作的实现
$this->options[strtolower($method)] = $args[0]; $this->options[strtolower($method)] = $args[0];
return $this; 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]:'*'; $field = isset($args[0])?$args[0]:'*';
return $this->getField(strtoupper($method).'('.$field.') AS tp_'.$method); return $this->getField(strtoupper($method).'('.$field.') AS tp_'.$method);
@@ -361,7 +369,7 @@ class Model {
// 根据主键删除记录 // 根据主键删除记录
$pk = $this->getPk(); $pk = $this->getPk();
if(strpos($options,',')) { if(strpos($options,',')) {
$where[$pk] = array('IN', $options); $where[$pk] = ['IN', $options];
}else{ }else{
$where[$pk] = $options; $where[$pk] = $options;
} }
@@ -394,7 +402,7 @@ class Model {
// 根据主键查询 // 根据主键查询
$pk = $this->getPk(); $pk = $this->getPk();
if(strpos($options,',')) { if(strpos($options,',')) {
$where[$pk] = array('IN',$options); $where[$pk] = ['IN',$options];
}else{ }else{
$where[$pk] = $options; $where[$pk] = $options;
} }
@@ -489,6 +497,7 @@ class Model {
* @return void * @return void
*/ */
protected function _parseType(&$data,$key) { protected function _parseType(&$data,$key) {
if(isset($this->fields['_type'][$key])) {
$fieldType = strtolower($this->fields['_type'][$key]); $fieldType = strtolower($this->fields['_type'][$key]);
if(false === strpos($fieldType,'bigint') && false !== strpos($fieldType,'int')) { if(false === strpos($fieldType,'bigint') && false !== strpos($fieldType,'int')) {
$data[$key] = intval($data[$key]); $data[$key] = intval($data[$key]);
@@ -498,6 +507,7 @@ class Model {
$data[$key] = (bool)$data[$key]; $data[$key] = (bool)$data[$key];
} }
} }
}
/** /**
* 查询数据 * 查询数据
@@ -581,7 +591,7 @@ class Model {
* @return boolean * @return boolean
*/ */
public function setInc($field,$step=1) { 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 * @return boolean
*/ */
public function setDec($field,$step=1) { 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预处理 }elseif(is_array($parse)){ // SQL预处理
$sql = vsprintf($sql,$parse); $sql = vsprintf($sql,$parse);
}else{ }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); $this->db->setModel($this->name);
return $sql; return $sql;
@@ -779,7 +789,7 @@ class Model {
if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) { if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $config && $_linkNum[$linkNum]!=$config) ) {
// 创建一个新的实例 // 创建一个新的实例
if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数 if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
$config = Config::get($config); $config = C($config);
} }
$_db[$linkNum] = Db::instance($config); $_db[$linkNum] = Db::instance($config);
}elseif(NULL === $config){ }elseif(NULL === $config){
@@ -1023,7 +1033,7 @@ class Model {
*/ */
public function cache($key=true,$expire=null,$type=''){ public function cache($key=true,$expire=null,$type=''){
if(false !== $key) if(false !== $key)
$this->options['cache'] = array('key'=>$key,'expire'=>$expire,'type'=>$type); $this->options['cache'] = ['key'=>$key,'expire'=>$expire,'type'=>$type];
return $this; return $this;
} }
@@ -1097,7 +1107,7 @@ class Model {
$parse = func_get_args(); $parse = func_get_args();
array_shift($parse); array_shift($parse);
} }
$parse = array_map(array($this->db,'escapeString'),$parse); $parse = array_map([$this->db,'escapeString'],$parse);
$where = vsprintf($where,$parse); $where = vsprintf($where,$parse);
}elseif(is_object($where)){ }elseif(is_object($where)){
$where = get_object_vars($where); $where = get_object_vars($where);

21
Think/Seesion/Driver.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
namespace Think\Session\Driver;
use SessionHandler;
class Driver extends SessionHandler {
protected $config = [];
public function __construct($config=[]){
$this->config = array_merge($this->config,$config);
}
}

View File

@@ -51,13 +51,7 @@ class Session {
if(!empty($config['type'])) { // 读取session驱动 if(!empty($config['type'])) { // 读取session驱动
$class = 'Think\\Session\\Driver\\'. ucwords(strtolower($config['type'])); $class = 'Think\\Session\\Driver\\'. ucwords(strtolower($config['type']));
// 检查驱动类 // 检查驱动类
if(class_exists($class)) { session_set_save_handler(new $class());
$hander = new $class();
$hander->execute();
}else {
// 类没有定义
E(L('_CLASS_NOT_EXIST_').': ' . $class);
}
} }
// 启动session // 启动session
if($config['auto_start']) session_start(); if($config['auto_start']) session_start();

View File

@@ -54,7 +54,6 @@ class Tag {
// 如果返回false 则中断行为执行 // 如果返回false 则中断行为执行
return ; return ;
} }
} }
} }
return; return;

View File

@@ -24,5 +24,4 @@ class Think {
$this->template->fetch($template,$data); $this->template->fetch($template,$data);
} }
} }
} }