增加接口类 规范驱动扩展

This commit is contained in:
thinkphp
2016-05-05 07:39:05 +08:00
parent 3774e3451a
commit 7a8bb3b54f
33 changed files with 237 additions and 64 deletions

View File

@@ -18,7 +18,7 @@ use think\Exception;
* Apc缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Apc
class Apc implements CacheInterface
{
protected $options = [

View File

@@ -0,0 +1,50 @@
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\cache\driver;
interface CacheInterface
{
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name);
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param int $expire 有效时间 0为永久
* @return boolean
*/
public function set($name, $value, $expire = null);
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return boolean
*/
public function rm($name);
/**
* 清除缓存
* @access public
* @return boolean
*/
public function clear();
}

View File

@@ -24,23 +24,23 @@ use think\Cache;
* );
* @author liu21st <liu21st@gmail.com>
*/
class Db
class Db implements CacheInterface
{
protected $handler = null;
protected $options = [
'type' => '',
'dsn' => '',
'hostname' => '',
'hostport' => '',
'username' => '',
'password' => '',
'database' => '',
'charset' => '',
'table' => '',
'prefix' => '',
'expire' => 0,
'length' => 0,
'type' => '',
'dsn' => '',
'hostname' => '',
'hostport' => '',
'username' => '',
'password' => '',
'database' => '',
'charset' => '',
'table' => '',
'prefix' => '',
'expire' => 0,
'length' => 0,
];
/**
@@ -162,4 +162,4 @@ class Db
return $this->handler->execute('TRUNCATE TABLE `' . $this->options['table'] . '`');
}
}
}

View File

@@ -17,7 +17,7 @@ use think\Cache;
* 文件类型缓存类
* @author liu21st <liu21st@gmail.com>
*/
class File
class File implements CacheInterface
{
protected $options = [
@@ -186,7 +186,7 @@ class File
*/
public function clear()
{
$fileLsit = (array)glob($this->options['path'].'*');
$fileLsit = (array) glob($this->options['path'] . '*');
foreach ($fileLsit as $path) {
is_file($path) && unlink($path);
}

View File

@@ -17,7 +17,7 @@ use think\Cache;
* 文件类型缓存类
* @author liu21st <liu21st@gmail.com>
*/
class Lite
class Lite implements CacheInterface
{
protected $options = [
'prefix' => '',

View File

@@ -14,7 +14,7 @@ namespace think\cache\driver;
use think\Cache;
use think\Exception;
class Memcache
class Memcache implements CacheInterface
{
protected $handler = null;
protected $options = [

View File

@@ -14,7 +14,7 @@ namespace think\cache\driver;
use think\Cache;
use think\Exception;
class Memcached
class Memcached implements CacheInterface
{
protected $handler = null;
protected $options = [

View File

@@ -19,7 +19,7 @@ use think\Exception;
* 要求安装phpredis扩展https://github.com/nicolasff/phpredis
* @author 尘缘 <130775@qq.com>
*/
class Redis
class Redis implements CacheInterface
{
protected $handler = null;
protected $options = [
@@ -65,10 +65,10 @@ class Redis
public function get($name)
{
Cache::$readTimes++;
$value = $this->handler->get($this->options['prefix'] . $name);
$jsonData = json_decode( $value, true );
$value = $this->handler->get($this->options['prefix'] . $name);
$jsonData = json_decode($value, true);
// 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 byron sampson<xiaobo.sun@qq.com>
return ($jsonData === null) ? $value : $jsonData;
return (null === $jsonData) ? $value : $jsonData;
}
/**
@@ -87,7 +87,7 @@ class Redis
}
$name = $this->options['prefix'] . $name;
//对数组/对象数据进行缓存处理,保证数据完整性 byron sampson<xiaobo.sun@qq.com>
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
if (is_int($expire)) {
$result = $this->handler->setex($name, $expire, $value);
} else {

View File

@@ -18,7 +18,7 @@ use think\Exception;
* SAE Memcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Sae
class Sae implements CacheInterface
{
protected $handler = null;
protected $options = [

View File

@@ -17,7 +17,7 @@ use think\Cache;
* Secache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Secache
class Secache implements CacheInterface
{
protected $handler = null;

View File

@@ -18,7 +18,7 @@ use think\Exception;
* Sqlite缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Sqlite
class Sqlite implements CacheInterface
{
protected $options = [

View File

@@ -17,7 +17,7 @@ use think\Cache;
* 测试缓存类
* @author liu21st <liu21st@gmail.com>
*/
class Test
class Test implements CacheInterface
{
/**

View File

@@ -18,7 +18,7 @@ use think\Exception;
* Wincache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Wincache
class Wincache implements CacheInterface
{
protected $options = [

View File

@@ -18,7 +18,7 @@ use think\Exception;
* Xcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Xcache
class Xcache implements CacheInterface
{
protected $options = [

View File

@@ -0,0 +1,25 @@
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\config\driver;
interface ConfigInterface
{
/**
* 解析配置
* @access public
* @param mixed $config 配置
* @return mixed
*/
public function parse($config);
}

View File

@@ -11,7 +11,7 @@
namespace think\config\driver;
class Ini
class Ini implements ConfigInterface
{
public function parse($config)
{

View File

@@ -11,7 +11,7 @@
namespace think\config\driver;
class Xml
class Xml implements ConfigInterface
{
public function parse($config)
{

View File

@@ -21,7 +21,7 @@ use think\exception\DbBindParamException;
use think\exception\PDOException;
use think\Log;
abstract class Connection
abstract class Connection implements ConnectionInterface
{
// PDO操作实例
protected $PDOStatement;
@@ -226,14 +226,6 @@ abstract class Connection
}
}
/**
* 解析pdo连接的dsn信息由驱动扩展
* @access public
* @param array $config 连接信息
* @return string
*/
abstract protected function parseDsn($config);
/**
* 释放查询结果
* @access public

View File

@@ -0,0 +1,47 @@
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\db;
interface ConnectionInterface
{
/**
* 解析pdo连接的dsn信息
* @access public
* @param array $config 连接信息
* @return string
*/
public function parseDsn($config);
/**
* 取得数据表的字段信息
* @access public
* @param string $tableName
* @return array
*/
public function getFields($tableName);
/**
* 取得数据库的表信息
* @access public
* @param string $dbName
* @return array
*/
public function getTables($dbName);
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
public function getExplain($sql);
}

View File

@@ -26,7 +26,7 @@ class Mysql extends Connection
* @param array $config 连接信息
* @return string
*/
protected function parseDsn($config)
public function parseDsn($config)
{
$dsn = 'mysql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
if (!empty($config['hostport'])) {
@@ -91,11 +91,11 @@ class Mysql extends Connection
/**
* SQL性能分析
* @access protected
* @access public
* @param string $sql
* @return array
*/
protected function getExplain($sql)
public function getExplain($sql)
{
$pdo = $this->linkID->query("EXPLAIN " . $sql);
$result = $pdo->fetch(\PDO::FETCH_ASSOC);

View File

@@ -28,7 +28,7 @@ class Oracle extends Connection
* @param array $config 连接信息
* @return string
*/
protected function parseDsn($config)
public function parseDsn($config)
{
$dsn = 'oci:dbname=';
if (!empty($config['hostname'])) {
@@ -139,11 +139,11 @@ class Oracle extends Connection
/**
* SQL性能分析
* @access protected
* @access public
* @param string $sql
* @return array
*/
protected function getExplain($sql)
public function getExplain($sql)
{
return [];
}

View File

@@ -25,7 +25,7 @@ class Pgsql extends Connection
* @param array $config 连接信息
* @return string
*/
protected function parseDsn($config)
public function parseDsn($config)
{
$dsn = 'pgsql:dbname=' . $config['database'] . ';host=' . $config['hostname'];
if (!empty($config['hostport'])) {
@@ -79,11 +79,11 @@ class Pgsql extends Connection
/**
* SQL性能分析
* @access protected
* @access public
* @param string $sql
* @return array
*/
protected function getExplain($sql)
public function getExplain($sql)
{
return [];
}

View File

@@ -25,7 +25,7 @@ class Sqlite extends Connection
* @param array $config 连接信息
* @return string
*/
protected function parseDsn($config)
public function parseDsn($config)
{
$dsn = 'sqlite:' . $config['database'];
return $dsn;
@@ -78,11 +78,11 @@ class Sqlite extends Connection
/**
* SQL性能分析
* @access protected
* @access public
* @param string $sql
* @return array
*/
protected function getExplain($sql)
public function getExplain($sql)
{
return [];
}

View File

@@ -33,7 +33,7 @@ class Sqlsrv extends Connection
* @param array $config 连接信息
* @return string
*/
protected function parseDsn($config)
public function parseDsn($config)
{
$dsn = 'sqlsrv:Database=' . $config['database'] . ';Server=' . $config['hostname'];
if (!empty($config['hostport'])) {
@@ -96,11 +96,11 @@ class Sqlsrv extends Connection
/**
* SQL性能分析
* @access protected
* @access public
* @param string $sql
* @return array
*/
protected function getExplain($sql)
public function getExplain($sql)
{
return [];
}

View File

@@ -13,7 +13,7 @@ namespace think\log\driver;
/**
* 本地化调试输出到文件
*/
class File
class File implements LogInterface
{
protected $config = [
'time_format' => ' c ',

View File

@@ -0,0 +1,25 @@
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\log\driver;
interface LogInterface
{
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return bool
*/
public function save(array $log = []);
}

View File

@@ -13,7 +13,7 @@ namespace think\log\driver;
/**
* 调试输出到SAE
*/
class Sae
class Sae implements LogInterface
{
protected $config = [
'log_time_format' => ' c ',

View File

@@ -5,7 +5,7 @@
*/
namespace think\log\driver;
class Socket
class Socket implements LogInterface
{
public $port = 1116; //SocketLog 服务的http的端口号

View File

@@ -13,7 +13,7 @@ namespace think\log\driver;
/**
* 模拟测试输出
*/
class Test
class Test implements LogInterface
{
/**
* 日志写入接口

View File

@@ -16,7 +16,7 @@ use think\Debug;
/**
* 页面Trace调试
*/
class Trace
class Trace implements LogInterface
{
protected $config = [
'trace_file' => '',

View File

@@ -13,7 +13,7 @@ namespace think\view\driver;
use think\Exception;
use think\Log;
class Php
class Php implements ViewInterface
{
// 模板引擎参数
protected $config = [

View File

@@ -14,7 +14,7 @@ use think\Exception;
use think\Log;
use think\Template;
class Think
class Think implements ViewInterface
{
// 模板引擎实例
private $template = null;

View File

@@ -0,0 +1,34 @@
<?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 <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\view\driver;
interface ViewInterface
{
/**
* 渲染模板文件
* @access public
* @param string $template 模板文件
* @param array $data 模板变量
* @return void
*/
public function fetch($template, $data = []);
/**
* 渲染模板内容
* @access public
* @param string $content 模板内容
* @param array $data 模板变量
* @return void
*/
public function display($content, $data = []);
}