From 3d38c39c17b9ebbd451029d92e9720bec4049f32 Mon Sep 17 00:00:00 2001 From: huangdijia Date: Fri, 29 Jan 2016 09:09:09 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E9=A9=B1=E5=8A=A8=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E6=81=A2=E5=A4=8DMemcached=E9=A9=B1=E5=8A=A8=EF=BC=8C?= =?UTF-8?q?=E8=A1=A5=E5=85=85memcache=E9=A9=B1=E5=8A=A8=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3windows=E4=B8=8B=E6=B2=A1=E6=9C=89memcached=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/cache/driver/Memcache.php | 134 +++++++++++ library/think/cache/driver/Memcached.php | 267 +++++++++------------ library/think/session/driver/Memcache.php | 115 +++++++++ library/think/session/driver/Memcached.php | 232 +++++++----------- 4 files changed, 452 insertions(+), 296 deletions(-) create mode 100644 library/think/cache/driver/Memcache.php create mode 100644 library/think/session/driver/Memcache.php diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php new file mode 100644 index 00000000..8ac18dd7 --- /dev/null +++ b/library/think/cache/driver/Memcache.php @@ -0,0 +1,134 @@ + +// +---------------------------------------------------------------------- + +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(); + } +} diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index ffc33180..ea4778d9 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -9,175 +9,128 @@ // | Author: liu21st // +---------------------------------------------------------------------- -namespace { - // 检测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; +namespace think\cache\driver; - public function addServers(array $servers = []) - { - if (empty($servers)) { - return; - } +use think\Cache; +use think\Exception; - foreach ($servers as $key => $server) { - if (empty($server[0])) { - continue; - } +class Memcached +{ + 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, - 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\cache\driver { - use think\Cache; - - class Memcached + /** + * 架构函数 + * @param array $options 缓存参数 + * @access public + */ + public function __construct($options = []) { - protected $handler = null; - protected $options = [ - '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 (!extension_loaded('memcached')) { + throw new Exception('_NOT_SUPPERT_:memcached'); } - - /** - * 读取缓存 - * @access public - * @param string $name 缓存变量名 - * @return mixed - */ - public function get($name) - { - Cache::$readTimes++; - return $this->handler->get($this->options['prefix'] . $name); + 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); + } - /** - * 写入缓存 - * @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, $expire)) { - if ($this->options['length'] > 0) { - // 记录缓存队列 - $queue = $this->handler->get('__info__'); - if (!$queue) { - $queue = []; - } - if (false === array_search($name, $queue)) { - array_push($queue, $name); - } + /** + * 读取缓存 + * @access public + * @param string $name 缓存变量名 + * @return mixed + */ + public function get($name) + { + Cache::$readTimes++; + return $this->handler->get($this->options['prefix'] . $name); + } - if (count($queue) > $this->options['length']) { - // 出列 - $key = array_shift($queue); - // 删除缓存 - $this->handler->delete($key); - } - $this->handler->set('__info__', $queue); + /** + * 写入缓存 + * @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, $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 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); - } + /** + * 删除缓存 + * + * @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(); - } + /** + * 清除缓存 + * @access public + * @return bool + */ + public function clear() + { + return $this->handler->flush(); } } diff --git a/library/think/session/driver/Memcache.php b/library/think/session/driver/Memcache.php new file mode 100644 index 00000000..d8058550 --- /dev/null +++ b/library/think/session/driver/Memcache.php @@ -0,0 +1,115 @@ + +// +---------------------------------------------------------------------- + +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; + } +} diff --git a/library/think/session/driver/Memcached.php b/library/think/session/driver/Memcached.php index 7d2445f7..bccfdda0 100644 --- a/library/think/session/driver/Memcached.php +++ b/library/think/session/driver/Memcached.php @@ -9,155 +9,109 @@ // | Author: liu21st // +---------------------------------------------------------------------- -namespace { - // 检测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; +namespace think\session\driver; - public function addServers(array $servers = []) - { - if (empty($servers)) { - return; - } +use SessionHandler; +use think\Exception; - foreach ($servers as $key => $server) { - if (empty($server[0])) { - continue; - } +class Memcached extends SessionHandler +{ + 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( - $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 + public function __construct($config = []) { - protected $handler = null; - protected $config = [ - 'host' => '127.0.0.1', // memcache主机 - 'port' => 1121, // memcache端口 - 'expire' => 3600, // session有效期 - 'timeout' => 0, // 连接超时时间(单位:毫秒) - 'session_name' => '', // memcache key前缀 - ]; + $this->config = array_merge($this->config, $config); + } - 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('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 - * @access public - * @param string $savePath - * @param mixed $sessName - */ - public function open($savePath, $sessName) - { - $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 + * @access public + */ + public function close() + { + $this->gc(ini_get('session.gc_maxlifetime')); + $this->handler->close(); + $this->handler = null; + 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 - */ - 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, $this->config['expire']); + } - /** - * 写入Session - * @access public - * @param string $sessID - * @param String $sessData - */ - public function write($sessID, $sessData) - { - return $this->handler->set($this->config['session_name'] . $sessID, $sessData, $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 $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; - } + /** + * Session 垃圾回收 + * @access public + * @param string $sessMaxLifeTime + */ + public function gc($sessMaxLifeTime) + { + return true; } } From d7f0526229d4f68b87ac91cd62a4a0f20133e333 Mon Sep 17 00:00:00 2001 From: huangdijia Date: Fri, 29 Jan 2016 10:24:45 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E9=95=BF=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/session/driver/Redis.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/think/session/driver/Redis.php b/library/think/session/driver/Redis.php index 43954629..415cd85d 100644 --- a/library/think/session/driver/Redis.php +++ b/library/think/session/driver/Redis.php @@ -23,7 +23,7 @@ class Redis extends SessionHandler 'password' => '', // 密码 'expire' => 3600, // 有效期 'timeout' => false, // 超时时间 - 'persistent' => 0, // 是否长连接 + 'persistent' => true, // 是否长连接 'session_name' => '', // memcache key前缀 ]; From 84dd868d7e1aa9651194e0187d5ee8a59afe65f9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Jan 2016 11:16:26 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Input=E7=B1=BB=E7=9A=84ha?= =?UTF-8?q?s=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Input.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/think/Input.php b/library/think/Input.php index 73928820..25072c88 100644 --- a/library/think/Input.php +++ b/library/think/Input.php @@ -206,10 +206,6 @@ class Input if (0 === strpos($name, '?')) { return self::has(substr($name, 1), $input); } - if (is_null($input) && !empty($name)) { - $input = $name; - $name = ''; - } if (!empty($input)) { $data = $input; $name = (string) $name; @@ -257,6 +253,8 @@ class Input foreach (explode('.', $name) as $val) { if (!isset($data[$val])) { return false; + } else { + $data = $data[$val]; } } return true; From cb25e9112035cc704f456df32f13ab8ca39f2f79 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Jan 2016 11:32:53 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/thinkphp/library/think/inputTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/thinkphp/library/think/inputTest.php b/tests/thinkphp/library/think/inputTest.php index dc91b29e..e1855635 100644 --- a/tests/thinkphp/library/think/inputTest.php +++ b/tests/thinkphp/library/think/inputTest.php @@ -24,8 +24,8 @@ class inputTest extends \PHPUnit_Framework_TestCase public function testInputName() { $input = ['a' => 'a', 'b' => ['c' => [' one ', 'two']]]; - $this->assertEquals($input, Input::data($input)); - $this->assertEquals($input['a'], Input::data($input['a'])); + $this->assertEquals($input, Input::data('', '', '', false, $input)); + $this->assertEquals($input['a'], Input::data('a', '', '', false, $input)); $this->assertEquals('one', Input::data('b.c.0/s', 'default', 'trim', false, $input)); } @@ -60,7 +60,7 @@ class inputTest extends \PHPUnit_Framework_TestCase { $src = 'EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN'; $regexs = explode('|', $src); - $data = Input::data($regexs); + $data = Input::data('', '', '', false, $regexs); foreach ($regexs as $key => $value) { $expected = $value . ' '; $this->assertEquals($expected, $data[$key]); @@ -110,7 +110,7 @@ class inputTest extends \PHPUnit_Framework_TestCase 'e' => 'NEQ ', 'f' => 'gt ', ]; - $this->assertEquals($excepted, Input::data($input, '', $filters)); + $this->assertEquals($excepted, Input::data('', '', $filters, false, $input)); } public function testTypeCast() From 7609595eba8b0851be9e7c397fbcded1cd7735e7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Jan 2016 12:21:32 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E6=94=B9=E8=BF=9BInput=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Input.php | 28 +++++++-------- tests/thinkphp/library/think/inputTest.php | 41 +++++++++++----------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/library/think/Input.php b/library/think/Input.php index 25072c88..9610bbe7 100644 --- a/library/think/Input.php +++ b/library/think/Input.php @@ -26,7 +26,7 @@ class Input */ 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) { - 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)) { 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) { - 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) { - 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) { - 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) { - 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) { - 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) { - 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'])) { $depr = \think\Config::get('pathinfo_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 { return $default; } @@ -189,19 +189,19 @@ class Input */ 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 mixed $default 默认值 * @param mixed $filter 过滤函数 * @param boolean $merge 是否与默认的过虑方法合并 - * @param array $input 数据源 * @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, '?')) { return self::has(substr($name, 1), $input); diff --git a/tests/thinkphp/library/think/inputTest.php b/tests/thinkphp/library/think/inputTest.php index e1855635..096ba3fa 100644 --- a/tests/thinkphp/library/think/inputTest.php +++ b/tests/thinkphp/library/think/inputTest.php @@ -24,17 +24,16 @@ class inputTest extends \PHPUnit_Framework_TestCase public function testInputName() { $input = ['a' => 'a', 'b' => ['c' => [' one ', 'two']]]; - $this->assertEquals($input, Input::data('', '', '', false, $input)); - $this->assertEquals($input['a'], Input::data('a', '', '', false, $input)); - $this->assertEquals('one', Input::data('b.c.0/s', 'default', 'trim', false, $input)); + $this->assertEquals($input, Input::data($input)); + $this->assertEquals($input['a'], Input::data($input, 'a')); + $this->assertEquals('one', Input::data($input, 'b.c.0/s', 'default', 'trim')); } public function testDefaultValue() { $input = ['a' => 'test']; $default = 'default'; - $this->assertEquals($default, Input::data($input['b'], $default)); - $this->assertEquals($default, Input::data($input, $default, '', false, $input)); + $this->assertEquals($default, Input::data($input, 'b', $default)); $this->assertEquals($default, Input::get('a', $default)); } @@ -42,25 +41,25 @@ class inputTest extends \PHPUnit_Framework_TestCase { $input = ['a' => ' test ', 'b' => ' test<> ']; $filters = 'trim'; - $this->assertEquals('test', Input::data('a', '', $filters, false, $input)); + $this->assertEquals('test', Input::data($input, 'a', '', $filters)); $filters = 'trim,htmlspecialchars'; - $this->assertEquals('test<>', Input::data('b', '', $filters, false, $input)); + $this->assertEquals('test<>', Input::data($input, 'b', '', $filters)); } public function testArrayFilter() { $input = ['a' => ' test ', 'b' => ' test<> ']; $filters = ['trim']; - $this->assertEquals('test', Input::data('a', '', $filters, false, $input)); + $this->assertEquals('test', Input::data($input, 'a', '', $filters)); $filters = ['trim', 'htmlspecialchars']; - $this->assertEquals('test<>', Input::data('b', '', $filters, false, $input)); + $this->assertEquals('test<>', Input::data($input, 'b', '', $filters)); } public function testFilterExp() { $src = 'EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN'; $regexs = explode('|', $src); - $data = Input::data('', '', '', false, $regexs); + $data = Input::data($regexs); foreach ($regexs as $key => $value) { $expected = $value . ' '; $this->assertEquals($expected, $data[$key]); @@ -71,11 +70,11 @@ class inputTest extends \PHPUnit_Framework_TestCase { $input = ['a' => 'test1', 'b' => '_test2', 'c' => '']; $filters = '/^test/'; - $this->assertEquals('test1', Input::data('a', '', $filters, false, $input)); + $this->assertEquals('test1', Input::data($input, 'a', '', $filters)); $default = 'default value'; - $this->assertEquals($default, Input::data('b', $default, $filters, false, $input)); + $this->assertEquals($default, Input::data($input, 'b', $default, $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() @@ -85,10 +84,10 @@ class inputTest extends \PHPUnit_Framework_TestCase $default = false; $input = ['a' => $email, 'b' => $error]; $filters = FILTER_VALIDATE_EMAIL; - $this->assertEquals($email, Input::data('a', '', $filters, false, $input)); - $this->assertFalse(Input::data('b', $default, $filters, false, $input)); + $this->assertEquals($email, Input::data($input, 'a', '', $filters)); + $this->assertFalse(Input::data($input, 'b', $default, $filters)); $filters = 'validate_email'; - $this->assertFalse(Input::data('b', $default, $filters, false, $input)); + $this->assertFalse(Input::data($input, 'b', $default, $filters)); } public function testAllInput() @@ -110,7 +109,7 @@ class inputTest extends \PHPUnit_Framework_TestCase 'e' => 'NEQ ', 'f' => 'gt ', ]; - $this->assertEquals($excepted, Input::data('', '', $filters, false, $input)); + $this->assertEquals($excepted, Input::data($input, '', '', $filters)); } public function testTypeCast() @@ -184,12 +183,12 @@ class inputTest extends \PHPUnit_Framework_TestCase { Input::setFilter('htmlspecialchars'); $input = ['a' => ' test<> ', 'b' => '']; - $this->assertEquals(' test<> ', Input::data('a', '', '', false, $input)); + $this->assertEquals(' test<> ', Input::data($input, 'a', '', '')); $filters = ['trim']; - $this->assertEquals('test<>', Input::data('a', '', $filters, false, $input)); - $this->assertEquals('test<>', Input::data('a', '', $filters, true, $input)); + $this->assertEquals('test<>', Input::data($input, 'a', '', $filters)); + $this->assertEquals('test<>', Input::data($input, 'a', '', $filters, true)); $filters = 'stripslashes'; - $this->assertEquals("<bar />", Input::data('b', '', $filters, true, $input)); + $this->assertEquals("<bar />", Input::data($input, 'b', '', $filters, true)); } } From 609d5a705ca2ff8bf9bad766c07499c6679053fd Mon Sep 17 00:00:00 2001 From: tale Date: Fri, 29 Jan 2016 15:01:49 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E5=B0=8F=E7=BB=86=E8=8A=82=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/Route.php | 2 +- library/think/Template.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/helper.php b/helper.php index 61cb5c98..b2fb7828 100644 --- a/helper.php +++ b/helper.php @@ -294,7 +294,7 @@ function trace($log = '[think]', $level = 'log') * @param array $vars 模板变量 * @return string */ -function V($template, $vars) +function V($template = '', $vars = []) { return \think\View::instance(\think\Config::get())->fetch($template, $vars); } diff --git a/library/think/Route.php b/library/think/Route.php index 693a9fc0..269c0a89 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -499,7 +499,7 @@ class Route if (false !== $match = self::match($url, $rule, $pattern)) { // 匹配到路由规则 // 检测是否定义路由 - if ($option['after_behavior']) { + if (!empty($option['after_behavior'])) { Hook::exec($option['after_behavior'], $route); } if ($route instanceof \Closure) { diff --git a/library/think/Template.php b/library/think/Template.php index e967429d..3f07e510 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -56,7 +56,7 @@ class Template */ 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['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']); $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']); From be430c15bb96c338b4c2af95da5a86f5b4bb4351 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Jan 2016 16:25:16 +0800 Subject: [PATCH 7/9] =?UTF-8?q?Model=E7=B1=BB=E6=8A=9B=E5=87=BA=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=20create=E6=96=B9=E6=B3=95=E9=BB=98=E8=AE=A4=E4=BC=A0?= =?UTF-8?q?=E5=85=A5=20\think\Input::post();?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 01b715a0..81a45494 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -265,8 +265,7 @@ class Model // 重置数据 $this->data = []; } else { - $this->error = Lang::get('_DATA_TYPE_INVALID_'); - return false; + throw new Exception('invalid data'); } } // 数据处理 @@ -309,8 +308,7 @@ class Model public function addAll($dataList, $options = [], $replace = false) { if (empty($dataList)) { - $this->error = Lang::get('_DATA_TYPE_INVALID_'); - return false; + throw new Exception('no data to write') } // 数据处理 foreach ($dataList as $key => $data) { @@ -344,16 +342,14 @@ class Model // 重置数据 $this->data = []; } else { - $this->error = Lang::get('_DATA_TYPE_INVALID_'); - return false; + throw new Exception('no data require update'); } } // 数据处理 $data = $this->_write_data($data, 'update'); if (empty($data)) { // 没有数据则不执行 - $this->error = Lang::get('_DATA_TYPE_INVALID_'); - return false; + throw new Exception('no data require update'); } // 分析表达式 $options = $this->_parseOptions(); @@ -370,16 +366,14 @@ class Model $where[$field] = $data[$field]; } else { // 如果缺少复合主键数据则不执行 - $this->error = Lang::get('_OPERATION_WRONG_'); - return false; + throw new Exception('miss complex primary data'); } unset($data[$field]); } } if (!isset($where)) { // 如果没有任何更新条件则不执行 - $this->error = Lang::get('_OPERATION_WRONG_'); - return false; + throw new Exception('no data to update without where'); } else { $options['where'] = $where; } @@ -421,7 +415,7 @@ class Model if (!empty($this->data) && isset($this->data[$pk])) { return $this->delete($this->data[$pk]); } else { - return false; + throw new Exception('no data to delete without where'); } } if (is_numeric($options) || is_string($options)) { @@ -450,14 +444,14 @@ class Model } $options['where'] = $where; } else { - return false; + throw new Exception('miss complex primary data'); } } // 分析表达式 $options = $this->_parseOptions($options); if (empty($options['where'])) { // 如果条件为空 不进行删除操作 除非设置 1=1 - return false; + throw new Exception('no data to delete without where'); } if (isset($options['where'][$pk])) { $pkValue = $options['where'][$pk]; @@ -511,7 +505,7 @@ class Model } $options['where'] = $where; } else { - return false; + throw new Exception('miss complex primary data'); } } elseif (false === $options) { // 用于子查询 不查询只返回SQL @@ -689,7 +683,7 @@ class Model $condition = $this->options['where']; if (empty($condition)) { // 没有条件不做任何更新 - return false; + throw new Exception('no data to update'); } if ($lazyTime > 0) { // 延迟写入 @@ -717,7 +711,7 @@ class Model $condition = $this->options['where']; if (empty($condition)) { // 没有条件不做任何更新 - return false; + throw new Exception('no data to update'); } if ($lazyTime > 0) { // 延迟写入 @@ -867,7 +861,7 @@ class Model } $options['where'] = $where; } else { - return false; + throw new Exception('miss complex primary data'); } } // 总是查找一条记录 @@ -941,14 +935,13 @@ class Model { // 如果没有传值默认取POST数据 if (empty($data)) { - $data = $_POST; + $data = \think\Input::post(); } elseif (is_object($data)) { $data = get_object_vars($data); } // 验证数据 if (empty($data) || !is_array($data)) { - $this->error = Lang::get('_DATA_TYPE_INVALID_'); - return false; + throw new Exception('invalid data type'); } // 状态 From 57346357066537e17ed21594ae4bb1e06b070ab0 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 29 Jan 2016 16:27:16 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 81a45494..b3e5ca5e 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -308,7 +308,7 @@ class Model public function addAll($dataList, $options = [], $replace = false) { if (empty($dataList)) { - throw new Exception('no data to write') + throw new Exception('no data to write'); } // 数据处理 foreach ($dataList as $key => $data) { @@ -415,7 +415,7 @@ class Model if (!empty($this->data) && isset($this->data[$pk])) { return $this->delete($this->data[$pk]); } else { - throw new Exception('no data to delete without where'); + throw new Exception('no data to delete without where'); } } if (is_numeric($options) || is_string($options)) { @@ -451,7 +451,7 @@ class Model $options = $this->_parseOptions($options); if (empty($options['where'])) { // 如果条件为空 不进行删除操作 除非设置 1=1 - throw new Exception('no data to delete without where'); + throw new Exception('no data to delete without where'); } if (isset($options['where'][$pk])) { $pkValue = $options['where'][$pk]; From 7f1a1cae65b35356b3c238ed77716df22a9218d9 Mon Sep 17 00:00:00 2001 From: 7IN0SAN9 Date: Fri, 29 Jan 2016 16:31:15 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=9C=A8setUp()=E6=96=B9=E6=B3=95=E4=B8=AD?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/thinkphp/library/think/cache/driver/apcTest.php | 10 +++++----- .../library/think/cache/driver/memcachedTest.php | 6 +++--- .../thinkphp/library/think/cache/driver/redisTest.php | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/thinkphp/library/think/cache/driver/apcTest.php b/tests/thinkphp/library/think/cache/driver/apcTest.php index 0557c412..981f114e 100644 --- a/tests/thinkphp/library/think/cache/driver/apcTest.php +++ b/tests/thinkphp/library/think/cache/driver/apcTest.php @@ -24,6 +24,11 @@ class apcTest extends cacheTestCase */ 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)); } /** @@ -31,11 +36,6 @@ class apcTest extends cacheTestCase */ 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) { $this->_cacheInstance = new \think\cache\driver\Apc(); } diff --git a/tests/thinkphp/library/think/cache/driver/memcachedTest.php b/tests/thinkphp/library/think/cache/driver/memcachedTest.php index 95328482..051083c2 100644 --- a/tests/thinkphp/library/think/cache/driver/memcachedTest.php +++ b/tests/thinkphp/library/think/cache/driver/memcachedTest.php @@ -24,6 +24,9 @@ class memcachedTest extends cacheTestCase */ protected function setUp() { + if (!extension_loaded("memcached") && !extension_loaded('memcache')) { + $this->markTestSkipped("Memcached或Memcache没有安装,已跳过测试!"); + } \think\Cache::connect(array('type' => 'memcached', 'expire' => 2)); } /** @@ -31,9 +34,6 @@ class memcachedTest extends cacheTestCase */ protected function getCacheInstance() { - if (!extension_loaded("memcached")) { - $this->markTestSkipped("Memcached没有安装,已跳过测试!"); - } if (null === $this->_cacheInstance) { $this->_cacheInstance = new \think\cache\driver\Memcached(); } diff --git a/tests/thinkphp/library/think/cache/driver/redisTest.php b/tests/thinkphp/library/think/cache/driver/redisTest.php index 29a6e301..c40cb68b 100644 --- a/tests/thinkphp/library/think/cache/driver/redisTest.php +++ b/tests/thinkphp/library/think/cache/driver/redisTest.php @@ -22,14 +22,14 @@ class redisTest extends cacheTestCase protected function setUp() { + if (!extension_loaded("redis")) { + $this->markTestSkipped("Redis没有安装,已跳过测试!"); + } \think\Cache::connect(array('type' => 'redis', 'expire' => 2)); } protected function getCacheInstance() { - if (!extension_loaded("redis")) { - $this->markTestSkipped("Redis没有安装,已跳过测试!"); - } if (null === $this->_cacheInstance) { $this->_cacheInstance = new \think\cache\driver\Redis(); }