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,'.')) {
$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;
}
}

View File

@@ -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);

View File

@@ -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

View File

@@ -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);

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驱动
$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();

View File

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

View File

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