This commit is contained in:
thinkphp
2016-01-29 21:49:31 +08:00
14 changed files with 516 additions and 370 deletions

View File

@@ -294,7 +294,7 @@ function trace($log = '[think]', $level = 'log')
* @param array $vars 模板变量 * @param array $vars 模板变量
* @return string * @return string
*/ */
function V($template, $vars) function V($template = '', $vars = [])
{ {
return \think\View::instance(\think\Config::get())->fetch($template, $vars); return \think\View::instance(\think\Config::get())->fetch($template, $vars);
} }

View File

@@ -26,7 +26,7 @@ class Input
*/ */
public static function get($name = '', $default = null, $filter = null, $merge = false) public static function get($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $_GET); return self::data($_GET, $name, $default, $filter, $merge);
} }
/** /**
@@ -39,7 +39,7 @@ class Input
*/ */
public static function post($name = '', $default = null, $filter = null, $merge = false) public static function post($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $_POST); return self::data($_POST, $name, $default, $filter, $merge);
} }
/** /**
@@ -56,7 +56,7 @@ class Input
if (is_null($_PUT)) { if (is_null($_PUT)) {
parse_str(file_get_contents('php://input'), $_PUT); parse_str(file_get_contents('php://input'), $_PUT);
} }
return self::data($name, $default, $filter, $merge, $_PUT); return self::data($_PUT, $name, $default, $filter, $merge);
} }
/** /**
@@ -92,7 +92,7 @@ class Input
*/ */
public static function request($name = '', $default = null, $filter = null, $merge = false) public static function request($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $_REQUEST); return self::data($_REQUEST, $name, $default, $filter, $merge);
} }
/** /**
@@ -105,7 +105,7 @@ class Input
*/ */
public static function session($name = '', $default = null, $filter = null, $merge = false) public static function session($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $_SESSION); return self::data($_SESSION, $name, $default, $filter, $merge);
} }
/** /**
@@ -118,7 +118,7 @@ class Input
*/ */
public static function cookie($name = '', $default = null, $filter = null, $merge = false) public static function cookie($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $_COOKIE); return self::data($_COOKIE, $name, $default, $filter, $merge);
} }
/** /**
@@ -131,7 +131,7 @@ class Input
*/ */
public static function server($name = '', $default = null, $filter = null, $merge = false) public static function server($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data(strtoupper($name), $default, $filter, $merge, $_SERVER); return self::data($_SERVER, strtoupper($name), $default, $filter, $merge);
} }
/** /**
@@ -144,7 +144,7 @@ class Input
*/ */
public static function globals($name = '', $default = null, $filter = null, $merge = false) public static function globals($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $GLOBALS); return self::data($GLOBALS, $name, $default, $filter, $merge);
} }
/** /**
@@ -157,7 +157,7 @@ class Input
*/ */
public static function env($name = '', $default = null, $filter = null, $merge = false) public static function env($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data(strtoupper($name), $default, $filter, $merge, $_ENV); return self::data($_ENV, strtoupper($name), $default, $filter, $merge);
} }
/** /**
@@ -173,7 +173,7 @@ class Input
if (!empty($_SERVER['PATH_INFO'])) { if (!empty($_SERVER['PATH_INFO'])) {
$depr = \think\Config::get('pathinfo_depr'); $depr = \think\Config::get('pathinfo_depr');
$input = explode($depr, trim($_SERVER['PATH_INFO'], $depr)); $input = explode($depr, trim($_SERVER['PATH_INFO'], $depr));
return self::data($name, $default, $filter, $merge, $input); return self::data($input, $name, $default, $filter, $merge);
} else { } else {
return $default; return $default;
} }
@@ -189,27 +189,23 @@ class Input
*/ */
public static function file($name = '', $default = null, $filter = null, $merge = false) public static function file($name = '', $default = null, $filter = null, $merge = false)
{ {
return self::data($name, $default, $filter, $merge, $_FILES); return self::data($_FILES, $name, $default, $filter, $merge);
} }
/** /**
* 获取系统变量 支持过滤和默认值 * 获取变量 支持过滤和默认值
* @param array $input 数据源
* @param string $name 字段名 * @param string $name 字段名
* @param mixed $default 默认值 * @param mixed $default 默认值
* @param mixed $filter 过滤函数 * @param mixed $filter 过滤函数
* @param boolean $merge 是否与默认的过虑方法合并 * @param boolean $merge 是否与默认的过虑方法合并
* @param array $input 数据源
* @return mixed * @return mixed
*/ */
public static function data($name, $default = null, $filter = null, $merge = false, $input = null) public static function data($input, $name, $default = null, $filter = null, $merge = false)
{ {
if (0 === strpos($name, '?')) { if (0 === strpos($name, '?')) {
return self::has(substr($name, 1), $input); return self::has(substr($name, 1), $input);
} }
if (is_null($input) && !empty($name)) {
$input = $name;
$name = '';
}
if (!empty($input)) { if (!empty($input)) {
$data = $input; $data = $input;
$name = (string) $name; $name = (string) $name;
@@ -257,6 +253,8 @@ class Input
foreach (explode('.', $name) as $val) { foreach (explode('.', $name) as $val) {
if (!isset($data[$val])) { if (!isset($data[$val])) {
return false; return false;
} else {
$data = $data[$val];
} }
} }
return true; return true;

View File

@@ -265,8 +265,7 @@ class Model
// 重置数据 // 重置数据
$this->data = []; $this->data = [];
} else { } else {
$this->error = Lang::get('_DATA_TYPE_INVALID_'); throw new Exception('invalid data');
return false;
} }
} }
// 数据处理 // 数据处理
@@ -309,8 +308,7 @@ class Model
public function addAll($dataList, $options = [], $replace = false) public function addAll($dataList, $options = [], $replace = false)
{ {
if (empty($dataList)) { if (empty($dataList)) {
$this->error = Lang::get('_DATA_TYPE_INVALID_'); throw new Exception('no data to write');
return false;
} }
// 数据处理 // 数据处理
foreach ($dataList as $key => $data) { foreach ($dataList as $key => $data) {
@@ -344,16 +342,14 @@ class Model
// 重置数据 // 重置数据
$this->data = []; $this->data = [];
} else { } else {
$this->error = Lang::get('_DATA_TYPE_INVALID_'); throw new Exception('no data require update');
return false;
} }
} }
// 数据处理 // 数据处理
$data = $this->_write_data($data, 'update'); $data = $this->_write_data($data, 'update');
if (empty($data)) { if (empty($data)) {
// 没有数据则不执行 // 没有数据则不执行
$this->error = Lang::get('_DATA_TYPE_INVALID_'); throw new Exception('no data require update');
return false;
} }
// 分析表达式 // 分析表达式
$options = $this->_parseOptions(); $options = $this->_parseOptions();
@@ -370,16 +366,14 @@ class Model
$where[$field] = $data[$field]; $where[$field] = $data[$field];
} else { } else {
// 如果缺少复合主键数据则不执行 // 如果缺少复合主键数据则不执行
$this->error = Lang::get('_OPERATION_WRONG_'); throw new Exception('miss complex primary data');
return false;
} }
unset($data[$field]); unset($data[$field]);
} }
} }
if (!isset($where)) { if (!isset($where)) {
// 如果没有任何更新条件则不执行 // 如果没有任何更新条件则不执行
$this->error = Lang::get('_OPERATION_WRONG_'); throw new Exception('no data to update without where');
return false;
} else { } else {
$options['where'] = $where; $options['where'] = $where;
} }
@@ -421,7 +415,7 @@ class Model
if (!empty($this->data) && isset($this->data[$pk])) { if (!empty($this->data) && isset($this->data[$pk])) {
return $this->delete($this->data[$pk]); return $this->delete($this->data[$pk]);
} else { } else {
return false; throw new Exception('no data to delete without where');
} }
} }
if (is_numeric($options) || is_string($options)) { if (is_numeric($options) || is_string($options)) {
@@ -450,14 +444,14 @@ class Model
} }
$options['where'] = $where; $options['where'] = $where;
} else { } else {
return false; throw new Exception('miss complex primary data');
} }
} }
// 分析表达式 // 分析表达式
$options = $this->_parseOptions($options); $options = $this->_parseOptions($options);
if (empty($options['where'])) { if (empty($options['where'])) {
// 如果条件为空 不进行删除操作 除非设置 1=1 // 如果条件为空 不进行删除操作 除非设置 1=1
return false; throw new Exception('no data to delete without where');
} }
if (isset($options['where'][$pk])) { if (isset($options['where'][$pk])) {
$pkValue = $options['where'][$pk]; $pkValue = $options['where'][$pk];
@@ -511,7 +505,7 @@ class Model
} }
$options['where'] = $where; $options['where'] = $where;
} else { } else {
return false; throw new Exception('miss complex primary data');
} }
} elseif (false === $options) { } elseif (false === $options) {
// 用于子查询 不查询只返回SQL // 用于子查询 不查询只返回SQL
@@ -689,7 +683,7 @@ class Model
$condition = $this->options['where']; $condition = $this->options['where'];
if (empty($condition)) { if (empty($condition)) {
// 没有条件不做任何更新 // 没有条件不做任何更新
return false; throw new Exception('no data to update');
} }
if ($lazyTime > 0) { if ($lazyTime > 0) {
// 延迟写入 // 延迟写入
@@ -717,7 +711,7 @@ class Model
$condition = $this->options['where']; $condition = $this->options['where'];
if (empty($condition)) { if (empty($condition)) {
// 没有条件不做任何更新 // 没有条件不做任何更新
return false; throw new Exception('no data to update');
} }
if ($lazyTime > 0) { if ($lazyTime > 0) {
// 延迟写入 // 延迟写入
@@ -867,7 +861,7 @@ class Model
} }
$options['where'] = $where; $options['where'] = $where;
} else { } else {
return false; throw new Exception('miss complex primary data');
} }
} }
// 总是查找一条记录 // 总是查找一条记录
@@ -941,14 +935,13 @@ class Model
{ {
// 如果没有传值默认取POST数据 // 如果没有传值默认取POST数据
if (empty($data)) { if (empty($data)) {
$data = $_POST; $data = \think\Input::post();
} elseif (is_object($data)) { } elseif (is_object($data)) {
$data = get_object_vars($data); $data = get_object_vars($data);
} }
// 验证数据 // 验证数据
if (empty($data) || !is_array($data)) { if (empty($data) || !is_array($data)) {
$this->error = Lang::get('_DATA_TYPE_INVALID_'); throw new Exception('invalid data type');
return false;
} }
// 状态 // 状态

View File

@@ -499,7 +499,7 @@ class Route
if (false !== $match = self::match($url, $rule, $pattern)) { if (false !== $match = self::match($url, $rule, $pattern)) {
// 匹配到路由规则 // 匹配到路由规则
// 检测是否定义路由 // 检测是否定义路由
if ($option['after_behavior']) { if (!empty($option['after_behavior'])) {
Hook::exec($option['after_behavior'], $route); Hook::exec($option['after_behavior'], $route);
} }
if ($route instanceof \Closure) { if ($route instanceof \Closure) {

View File

@@ -56,7 +56,7 @@ class Template
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
$this->config['cache_path'] = RUNTIME_PATH . 'template' . DS; $this->config['cache_path'] = RUNTIME_PATH . 'temp' . DS;
$this->config = array_merge($this->config, empty($config) ? (array) Config::get('template') : $config); $this->config = array_merge($this->config, empty($config) ? (array) Config::get('template') : $config);
$this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']);
$this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);

134
library/think/cache/driver/Memcache.php vendored Normal file
View File

@@ -0,0 +1,134 @@
<?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;
use think\Cache;
use think\Exception;
class Memcache
{
protected $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => 0, // 超时时间(单位:毫秒)
'persistent' => true,
'length' => 0,
];
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options = [])
{
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = new \Memcache;
// 支持集群
$hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']);
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
foreach ((array) $hosts as $i => $host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
$this->options['timeout'] > 0 ?
$this->handler->addServer($host, $port, $this->options['persistent'], 1) :
$this->handler->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']);
}
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name)
{
Cache::$readTimes++;
return $this->handler->get($this->options['prefix'] . $name);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer $expire 有效时间(秒)
* @return bool
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($this->handler->set($name, $value, 0, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
$this->handler->set('__info__', $queue);
}
return true;
}
return false;
}
/**
* 删除缓存
*
* @param string $name 缓存变量名
* @param bool|false $ttl
*
* @return bool
*/
public function rm($name, $ttl = false)
{
$name = $this->options['prefix'] . $name;
return false === $ttl ?
$this->handler->delete($name) :
$this->handler->delete($name, $ttl);
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear()
{
return $this->handler->flush();
}
}

View File

@@ -9,175 +9,128 @@
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace { namespace think\cache\driver;
// 检测php环境
if (!extension_loaded('memcached')) {
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache or memcachd');
}
class Memcached extends Memcache
{
const OPT_CONNECT_TIMEOUT = 14;
private $timeout = 1000;
public function addServers(array $servers = []) use think\Cache;
{ use think\Exception;
if (empty($servers)) {
return;
}
foreach ($servers as $key => $server) { class Memcached
if (empty($server[0])) { {
continue; protected $handler = null;
} protected $options = [
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => 0, // 超时时间(单位:毫秒)
'length' => 0,
];
$this->addServer( /**
$server[0], * 架构函数
!empty($server[1]) ? $server[1] : 11211, * @param array $options 缓存参数
true, * @access public
!empty($server[2]) ? $server[1] : 1, */
$this->timeout > 0 ? ($this->timeout / 1000) : 1 public function __construct($options = [])
);
}
}
public function setOption(int $option, mixed $value)
{
switch ($option) {
case self::OPT_CONNECT_TIMEOUT:
$this->timeout = $value;
break;
default:
break;
}
}
public function set(string $key, mixed $value, $expiration = 0)
{
return $this->set($key, $value, MEMCACHE_COMPRESSED, $expiration);
}
}
}
}
namespace think\cache\driver {
use think\Cache;
class Memcached
{ {
protected $handler = null; if (!extension_loaded('memcached')) {
protected $options = [ throw new Exception('_NOT_SUPPERT_:memcached');
'host' => '127.0.0.1',
'port' => 11211,
'expire' => 0,
'timeout' => 0, // 超时时间(单位:毫秒)
'length' => 0,
];
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$this->handler = new \Memcached;
// 设置连接超时时间(单位:毫秒)
if ($this->options['timeout'] > 0) {
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
}
// 支持集群
$hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']);
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
$servers = [];
foreach ((array) $hosts as $i => $host) {
$servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
}
$this->handler->addServers($servers);
} }
if (!empty($options)) {
/** $this->options = array_merge($this->options, $options);
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @return mixed
*/
public function get($name)
{
Cache::$readTimes++;
return $this->handler->get($this->options['prefix'] . $name);
} }
$this->handler = new \Memcached;
// 设置连接超时时间(单位:毫秒)
if ($this->options['timeout'] > 0) {
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
}
// 支持集群
$hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']);
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
$servers = [];
foreach ((array) $hosts as $i => $host) {
$servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
}
$this->handler->addServers($servers);
}
/** /**
* 写入缓存 * 读取缓存
* @access public * @access public
* @param string $name 缓存变量名 * @param string $name 缓存变量名
* @param mixed $value 存储数据 * @return mixed
* @param integer $expire 有效时间(秒) */
* @return bool public function get($name)
*/ {
public function set($name, $value, $expire = null) Cache::$readTimes++;
{ return $this->handler->get($this->options['prefix'] . $name);
Cache::$writeTimes++; }
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($this->handler->set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
}
if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) { /**
// 出列 * 写入缓存
$key = array_shift($queue); * @access public
// 删除缓存 * @param string $name 缓存变量名
$this->handler->delete($key); * @param mixed $value 存储数据
} * @param integer $expire 有效时间(秒)
$this->handler->set('__info__', $queue); * @return bool
*/
public function set($name, $value, $expire = null)
{
Cache::$writeTimes++;
if (is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'] . $name;
if ($this->handler->set($name, $value, $expire)) {
if ($this->options['length'] > 0) {
// 记录缓存队列
$queue = $this->handler->get('__info__');
if (!$queue) {
$queue = [];
} }
return true; if (false === array_search($name, $queue)) {
array_push($queue, $name);
}
if (count($queue) > $this->options['length']) {
// 出列
$key = array_shift($queue);
// 删除缓存
$this->handler->delete($key);
}
$this->handler->set('__info__', $queue);
} }
return false; return true;
} }
return false;
}
/** /**
* 删除缓存 * 删除缓存
* *
* @param string $name 缓存变量名 * @param string $name 缓存变量名
* @param bool|false $ttl * @param bool|false $ttl
* *
* @return bool * @return bool
*/ */
public function rm($name, $ttl = false) public function rm($name, $ttl = false)
{ {
$name = $this->options['prefix'] . $name; $name = $this->options['prefix'] . $name;
return false === $ttl ? return false === $ttl ?
$this->handler->delete($name) : $this->handler->delete($name) :
$this->handler->delete($name, $ttl); $this->handler->delete($name, $ttl);
} }
/** /**
* 清除缓存 * 清除缓存
* @access public * @access public
* @return bool * @return bool
*/ */
public function clear() public function clear()
{ {
return $this->handler->flush(); return $this->handler->flush();
}
} }
} }

View File

@@ -0,0 +1,115 @@
<?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\session\driver;
use SessionHandler;
use think\Exception;
class Memcache extends SessionHandler
{
protected $handler = null;
protected $config = [
'host' => '127.0.0.1', // memcache主机
'port' => 1121, // memcache端口
'expire' => 3600, // session有效期
'timeout' => 0, // 连接超时时间(单位:毫秒)
'persistent' => true, // 长连接
'session_name' => '', // memcache key前缀
];
public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* 打开Session
* @access public
* @param string $savePath
* @param mixed $sessName
*/
public function open($savePath, $sessName)
{
// 检测php环境
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache');
}
$this->handler = new \Memcache;
// 支持集群
$hosts = explode(',', $this->config['host']);
$ports = explode(',', $this->config['port']);
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
foreach ((array) $hosts as $i => $host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
$this->config['timeout'] > 0 ?
$this->handler->addServer($host, $port, $this->config['persistent'], 1) :
$this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']);
}
return true;
}
/**
* 关闭Session
* @access public
*/
public function close()
{
$this->gc(ini_get('session.gc_maxlifetime'));
$this->handler->close();
$this->handler = null;
return true;
}
/**
* 读取Session
* @access public
* @param string $sessID
*/
public function read($sessID)
{
return $this->handler->get($this->config['session_name'] . $sessID);
}
/**
* 写入Session
* @access public
* @param string $sessID
* @param String $sessData
*/
public function write($sessID, $sessData)
{
return $this->handler->set($this->config['session_name'] . $sessID, $sessData, 0, $this->config['expire']);
}
/**
* 删除Session
* @access public
* @param string $sessID
*/
public function destroy($sessID)
{
return $this->handler->delete($this->config['session_name'] . $sessID);
}
/**
* Session 垃圾回收
* @access public
* @param string $sessMaxLifeTime
*/
public function gc($sessMaxLifeTime)
{
return true;
}
}

View File

@@ -9,155 +9,109 @@
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace { namespace think\session\driver;
// 检测php环境
if (!extension_loaded('memcached')) {
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache or memcached');
}
class Memcached extends Memcache
{
const OPT_CONNECT_TIMEOUT = 14;
private $timeout = 1000;
public function addServers(array $servers = []) use SessionHandler;
{ use think\Exception;
if (empty($servers)) {
return;
}
foreach ($servers as $key => $server) { class Memcached extends SessionHandler
if (empty($server[0])) { {
continue; protected $handler = null;
} protected $config = [
'host' => '127.0.0.1', // memcache主机
'port' => 1121, // memcache端口
'expire' => 3600, // session有效期
'timeout' => 0, // 连接超时时间(单位:毫秒)
'session_name' => '', // memcache key前缀
];
$this->addServer( public function __construct($config = [])
$server[0],
!empty($server[1]) ? $server[1] : 11211,
true,
!empty($server[2]) ? $server[1] : 1,
$this->timeout > 0 ? ($this->timeout / 1000) : 1
);
}
}
public function setOption(int $option, mixed $value)
{
switch ($option) {
case self::OPT_CONNECT_TIMEOUT:
$this->timeout = $value;
break;
default:
break;
}
}
public function set(string $key, mixed $value, $expiration = 0)
{
return $this->set($key, $value, MEMCACHE_COMPRESSED, $expiration);
}
}
}
}
namespace think\session\driver {
use SessionHandler;
class Memcached extends SessionHandler
{ {
protected $handler = null; $this->config = array_merge($this->config, $config);
protected $config = [ }
'host' => '127.0.0.1', // memcache主机
'port' => 1121, // memcache端口
'expire' => 3600, // session有效期
'timeout' => 0, // 连接超时时间(单位:毫秒)
'session_name' => '', // memcache key前缀
];
public function __construct($config = []) /**
{ * 打开Session
$this->config = array_merge($this->config, $config); * @access public
* @param string $savePath
* @param mixed $sessName
*/
public function open($savePath, $sessName)
{
// 检测php环境
if (!extension_loaded('memcached')) {
throw new Exception('_NOT_SUPPERT_:memcached');
} }
$this->handler = new \Memcached;
// 设置连接超时时间(单位:毫秒)
if ($this->config['timeout'] > 0) {
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']);
}
// 支持集群
$hosts = explode(',', $this->config['host']);
$ports = explode(',', $this->config['port']);
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
$servers = [];
foreach ((array) $hosts as $i => $host) {
$servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
}
$this->handler->addServers($servers);
return true;
}
/** /**
* 打开Session * 关闭Session
* @access public * @access public
* @param string $savePath */
* @param mixed $sessName public function close()
*/ {
public function open($savePath, $sessName) $this->gc(ini_get('session.gc_maxlifetime'));
{ $this->handler->close();
$this->handler = new \Memcached; $this->handler = null;
// 设置连接超时时间(单位:毫秒) return true;
if ($this->config['timeout'] > 0) { }
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']);
}
// 支持集群
$hosts = explode(',', $this->config['host']);
$ports = explode(',', $this->config['port']);
if (empty($ports[0])) {
$ports[0] = 11211;
}
// 建立连接
$servers = [];
foreach ((array) $hosts as $i => $host) {
$servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
}
$this->handler->addServers($servers);
return true;
}
/** /**
* 关闭Session * 读取Session
* @access public * @access public
*/ * @param string $sessID
public function close() */
{ public function read($sessID)
$this->gc(ini_get('session.gc_maxlifetime')); {
$this->handler->close(); return $this->handler->get($this->config['session_name'] . $sessID);
$this->handler = null; }
return true;
}
/** /**
* 读取Session * 写入Session
* @access public * @access public
* @param string $sessID * @param string $sessID
*/ * @param String $sessData
public function read($sessID) */
{ public function write($sessID, $sessData)
return $this->handler->get($this->config['session_name'] . $sessID); {
} return $this->handler->set($this->config['session_name'] . $sessID, $sessData, $this->config['expire']);
}
/** /**
* 写入Session * 删除Session
* @access public * @access public
* @param string $sessID * @param string $sessID
* @param String $sessData */
*/ public function destroy($sessID)
public function write($sessID, $sessData) {
{ return $this->handler->delete($this->config['session_name'] . $sessID);
return $this->handler->set($this->config['session_name'] . $sessID, $sessData, $this->config['expire']); }
}
/** /**
* 删除Session * Session 垃圾回收
* @access public * @access public
* @param string $sessID * @param string $sessMaxLifeTime
*/ */
public function destroy($sessID) public function gc($sessMaxLifeTime)
{ {
return $this->handler->delete($this->config['session_name'] . $sessID); return true;
}
/**
* Session 垃圾回收
* @access public
* @param string $sessMaxLifeTime
*/
public function gc($sessMaxLifeTime)
{
return true;
}
} }
} }

View File

@@ -23,7 +23,7 @@ class Redis extends SessionHandler
'password' => '', // 密码 'password' => '', // 密码
'expire' => 3600, // 有效期 'expire' => 3600, // 有效期
'timeout' => false, // 超时时间 'timeout' => false, // 超时时间
'persistent' => 0, // 是否长连接 'persistent' => true, // 是否长连接
'session_name' => '', // memcache key前缀 'session_name' => '', // memcache key前缀
]; ];

View File

@@ -24,6 +24,11 @@ class apcTest extends cacheTestCase
*/ */
protected function setUp() protected function setUp()
{ {
if (!extension_loaded("apc")) {
$this->markTestSkipped("APC没有安装已跳过测试");
} elseif ('cli' === PHP_SAPI && !ini_get('apc.enable_cli')) {
$this->markTestSkipped("APC模块没有开启已跳过测试");
}
\think\Cache::connect(array('type' => 'apc', 'expire' => 2)); \think\Cache::connect(array('type' => 'apc', 'expire' => 2));
} }
/** /**
@@ -31,11 +36,6 @@ class apcTest extends cacheTestCase
*/ */
protected function getCacheInstance() protected function getCacheInstance()
{ {
if (!extension_loaded("apc")) {
$this->markTestSkipped("APC没有安装已跳过测试");
} elseif ('cli' === PHP_SAPI && !ini_get('apc.enable_cli')) {
$this->markTestSkipped("APC模块没有开启已跳过测试");
}
if (null === $this->_cacheInstance) { if (null === $this->_cacheInstance) {
$this->_cacheInstance = new \think\cache\driver\Apc(); $this->_cacheInstance = new \think\cache\driver\Apc();
} }

View File

@@ -24,6 +24,9 @@ class memcachedTest extends cacheTestCase
*/ */
protected function setUp() protected function setUp()
{ {
if (!extension_loaded("memcached") && !extension_loaded('memcache')) {
$this->markTestSkipped("Memcached或Memcache没有安装已跳过测试");
}
\think\Cache::connect(array('type' => 'memcached', 'expire' => 2)); \think\Cache::connect(array('type' => 'memcached', 'expire' => 2));
} }
/** /**
@@ -31,9 +34,6 @@ class memcachedTest extends cacheTestCase
*/ */
protected function getCacheInstance() protected function getCacheInstance()
{ {
if (!extension_loaded("memcached")) {
$this->markTestSkipped("Memcached没有安装已跳过测试");
}
if (null === $this->_cacheInstance) { if (null === $this->_cacheInstance) {
$this->_cacheInstance = new \think\cache\driver\Memcached(); $this->_cacheInstance = new \think\cache\driver\Memcached();
} }

View File

@@ -22,14 +22,14 @@ class redisTest extends cacheTestCase
protected function setUp() protected function setUp()
{ {
if (!extension_loaded("redis")) {
$this->markTestSkipped("Redis没有安装已跳过测试");
}
\think\Cache::connect(array('type' => 'redis', 'expire' => 2)); \think\Cache::connect(array('type' => 'redis', 'expire' => 2));
} }
protected function getCacheInstance() protected function getCacheInstance()
{ {
if (!extension_loaded("redis")) {
$this->markTestSkipped("Redis没有安装已跳过测试");
}
if (null === $this->_cacheInstance) { if (null === $this->_cacheInstance) {
$this->_cacheInstance = new \think\cache\driver\Redis(); $this->_cacheInstance = new \think\cache\driver\Redis();
} }

View File

@@ -25,16 +25,15 @@ class inputTest extends \PHPUnit_Framework_TestCase
{ {
$input = ['a' => 'a', 'b' => ['c' => [' one ', 'two']]]; $input = ['a' => 'a', 'b' => ['c' => [' one ', 'two']]];
$this->assertEquals($input, Input::data($input)); $this->assertEquals($input, Input::data($input));
$this->assertEquals($input['a'], Input::data($input['a'])); $this->assertEquals($input['a'], Input::data($input, 'a'));
$this->assertEquals('one', Input::data('b.c.0/s', 'default', 'trim', false, $input)); $this->assertEquals('one', Input::data($input, 'b.c.0/s', 'default', 'trim'));
} }
public function testDefaultValue() public function testDefaultValue()
{ {
$input = ['a' => 'test']; $input = ['a' => 'test'];
$default = 'default'; $default = 'default';
$this->assertEquals($default, Input::data($input['b'], $default)); $this->assertEquals($default, Input::data($input, 'b', $default));
$this->assertEquals($default, Input::data($input, $default, '', false, $input));
$this->assertEquals($default, Input::get('a', $default)); $this->assertEquals($default, Input::get('a', $default));
} }
@@ -42,18 +41,18 @@ class inputTest extends \PHPUnit_Framework_TestCase
{ {
$input = ['a' => ' test ', 'b' => ' test<> ']; $input = ['a' => ' test ', 'b' => ' test<> '];
$filters = 'trim'; $filters = 'trim';
$this->assertEquals('test', Input::data('a', '', $filters, false, $input)); $this->assertEquals('test', Input::data($input, 'a', '', $filters));
$filters = 'trim,htmlspecialchars'; $filters = 'trim,htmlspecialchars';
$this->assertEquals('test&lt;&gt;', Input::data('b', '', $filters, false, $input)); $this->assertEquals('test&lt;&gt;', Input::data($input, 'b', '', $filters));
} }
public function testArrayFilter() public function testArrayFilter()
{ {
$input = ['a' => ' test ', 'b' => ' test<> ']; $input = ['a' => ' test ', 'b' => ' test<> '];
$filters = ['trim']; $filters = ['trim'];
$this->assertEquals('test', Input::data('a', '', $filters, false, $input)); $this->assertEquals('test', Input::data($input, 'a', '', $filters));
$filters = ['trim', 'htmlspecialchars']; $filters = ['trim', 'htmlspecialchars'];
$this->assertEquals('test&lt;&gt;', Input::data('b', '', $filters, false, $input)); $this->assertEquals('test&lt;&gt;', Input::data($input, 'b', '', $filters));
} }
public function testFilterExp() public function testFilterExp()
@@ -71,11 +70,11 @@ class inputTest extends \PHPUnit_Framework_TestCase
{ {
$input = ['a' => 'test1', 'b' => '_test2', 'c' => '']; $input = ['a' => 'test1', 'b' => '_test2', 'c' => ''];
$filters = '/^test/'; $filters = '/^test/';
$this->assertEquals('test1', Input::data('a', '', $filters, false, $input)); $this->assertEquals('test1', Input::data($input, 'a', '', $filters));
$default = 'default value'; $default = 'default value';
$this->assertEquals($default, Input::data('b', $default, $filters, false, $input)); $this->assertEquals($default, Input::data($input, 'b', $default, $filters));
$filters = '/.+/'; $filters = '/.+/';
$this->assertEquals('default value', Input::data('c', $default, $filters, false, $input)); $this->assertEquals('default value', Input::data($input, 'c', $default, $filters));
} }
public function testFiltrateWithFilterVar() public function testFiltrateWithFilterVar()
@@ -85,10 +84,10 @@ class inputTest extends \PHPUnit_Framework_TestCase
$default = false; $default = false;
$input = ['a' => $email, 'b' => $error]; $input = ['a' => $email, 'b' => $error];
$filters = FILTER_VALIDATE_EMAIL; $filters = FILTER_VALIDATE_EMAIL;
$this->assertEquals($email, Input::data('a', '', $filters, false, $input)); $this->assertEquals($email, Input::data($input, 'a', '', $filters));
$this->assertFalse(Input::data('b', $default, $filters, false, $input)); $this->assertFalse(Input::data($input, 'b', $default, $filters));
$filters = 'validate_email'; $filters = 'validate_email';
$this->assertFalse(Input::data('b', $default, $filters, false, $input)); $this->assertFalse(Input::data($input, 'b', $default, $filters));
} }
public function testAllInput() public function testAllInput()
@@ -110,7 +109,7 @@ class inputTest extends \PHPUnit_Framework_TestCase
'e' => 'NEQ ', 'e' => 'NEQ ',
'f' => 'gt ', 'f' => 'gt ',
]; ];
$this->assertEquals($excepted, Input::data($input, '', $filters)); $this->assertEquals($excepted, Input::data($input, '', '', $filters));
} }
public function testTypeCast() public function testTypeCast()
@@ -184,12 +183,12 @@ class inputTest extends \PHPUnit_Framework_TestCase
{ {
Input::setFilter('htmlspecialchars'); Input::setFilter('htmlspecialchars');
$input = ['a' => ' test<> ', 'b' => '<b\\ar />']; $input = ['a' => ' test<> ', 'b' => '<b\\ar />'];
$this->assertEquals(' test<> ', Input::data('a', '', '', false, $input)); $this->assertEquals(' test<> ', Input::data($input, 'a', '', ''));
$filters = ['trim']; $filters = ['trim'];
$this->assertEquals('test<>', Input::data('a', '', $filters, false, $input)); $this->assertEquals('test<>', Input::data($input, 'a', '', $filters));
$this->assertEquals('test&lt;&gt;', Input::data('a', '', $filters, true, $input)); $this->assertEquals('test&lt;&gt;', Input::data($input, 'a', '', $filters, true));
$filters = 'stripslashes'; $filters = 'stripslashes';
$this->assertEquals("&lt;bar /&gt;", Input::data('b', '', $filters, true, $input)); $this->assertEquals("&lt;bar /&gt;", Input::data($input, 'b', '', $filters, true));
} }
} }