改进分页类

This commit is contained in:
yunwuxin
2016-07-14 10:44:04 +08:00
parent e57a60f974
commit 30e25f938b
6 changed files with 179 additions and 134 deletions

View File

@@ -45,7 +45,7 @@ abstract class Paginator
'fragment' => ''
];
public function __construct($items, $listRows, $currentPage = null, $simple = false, $total = null, $options = [])
protected function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
{
$this->options = array_merge($this->options, $options);
@@ -71,12 +71,21 @@ abstract class Paginator
$this->items = PaginatorCollection::make($items, $this);
}
public function items()
/**
* @param $items
* @param $listRows
* @param null $currentPage
* @param bool $simple
* @param null $total
* @param array $options
* @return PaginatorCollection
*/
public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
{
return $this->items;
$paginator = new static($items, $listRows, $currentPage, $total, $simple, $options);
return $paginator->items;
}
protected function setCurrentPage($currentPage)
{
if (!$this->simple && $currentPage > $this->lastPage) {

View File

@@ -54,7 +54,7 @@ class Query
* 架构函数
* @access public
* @param Connection $connection 数据库对象实例
* @param string $model 模型名
* @param string $model 模型名
*/
public function __construct(Connection $connection = null, $model = '')
{
@@ -67,8 +67,8 @@ class Query
/**
* 利用__call方法实现一些特殊的Model方法
* @access public
* @param string $method 方法名称
* @param array $args 调用参数
* @param string $method 方法名称
* @param array $args 调用参数
* @return mixed
* @throws DbException
* @throws Exception
@@ -176,10 +176,10 @@ class Query
/**
* 执行查询 返回数据集
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $master 是否在主服务器读操作
* @param bool|string $class 指定返回的数据集对象
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $master 是否在主服务器读操作
* @param bool|string $class 指定返回的数据集对象
* @return mixed
* @throws BindParamException
* @throws PDOException
@@ -192,10 +192,10 @@ class Query
/**
* 执行语句
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $getLastInsID 是否获取自增ID
* @param boolean $sequence 自增序列名
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param boolean $getLastInsID 是否获取自增ID
* @param boolean $sequence 自增序列名
* @return int
* @throws BindParamException
* @throws PDOException
@@ -294,9 +294,9 @@ class Query
/**
* 得到分表的的数据表名
* @access public
* @param array $data 操作的数据
* @param string $field 分表依据的字段
* @param array $rule 分表规则
* @param array $data 操作的数据
* @param string $field 分表依据的字段
* @param array $rule 分表规则
* @return string
*/
public function getPartitionTableName($data, $field, $rule = [])
@@ -357,7 +357,7 @@ class Query
protected function builder()
{
static $builder = [];
$driver = $this->driver;
$driver = $this->driver;
if (!isset($builder[$driver])) {
$class = '\\think\\db\\builder\\' . ucfirst($driver);
$builder[$driver] = new $class($this->connection);
@@ -370,8 +370,8 @@ class Query
/**
* 得到某个字段的值
* @access public
* @param string $field 字段名
* @param mixed $default 默认值
* @param string $field 字段名
* @param mixed $default 默认值
* @return mixed
*/
public function value($field, $default = null)
@@ -410,8 +410,8 @@ class Query
/**
* 得到某个列的数组
* @access public
* @param string $field 字段名 多个字段用逗号分隔
* @param string $key 索引
* @param string $field 字段名 多个字段用逗号分隔
* @param string $key 索引
* @return array
*/
public function column($field, $key = '')
@@ -531,8 +531,8 @@ class Query
* 设置记录的某个字段值
* 支持使用数据库字段和方法
* @access public
* @param string|array $field 字段名
* @param mixed $value 字段值
* @param string|array $field 字段名
* @param mixed $value 字段值
* @return integer
*/
public function setField($field, $value = '')
@@ -548,9 +548,9 @@ class Query
/**
* 字段值(延迟)增长
* @access public
* @param string $field 字段名
* @param integer $step 增长值
* @param integer $lazyTime 延时时间(s)
* @param string $field 字段名
* @param integer $step 增长值
* @param integer $lazyTime 延时时间(s)
* @return integer|true
* @throws Exception
*/
@@ -575,9 +575,9 @@ class Query
/**
* 字段值(延迟)减少
* @access public
* @param string $field 字段名
* @param integer $step 减少值
* @param integer $lazyTime 延时时间(s)
* @param string $field 字段名
* @param integer $step 减少值
* @param integer $lazyTime 延时时间(s)
* @return integer|true
* @throws Exception
*/
@@ -603,9 +603,9 @@ class Query
* 延时更新检查 返回false表示需要延时
* 否则返回实际写入的数值
* @access public
* @param string $guid 写入标识
* @param integer $step 写入步进值
* @param integer $lazyTime 延时时间(s)
* @param string $guid 写入标识
* @param integer $step 写入步进值
* @param integer $lazyTime 延时时间(s)
* @return false|integer
*/
protected function lazyWrite($guid, $step, $lazyTime)
@@ -634,9 +634,9 @@ class Query
/**
* 查询SQL组装 join
* @access public
* @param mixed $join 关联的表名
* @param mixed $condition 条件
* @param string $type JOIN类型
* @param mixed $join 关联的表名
* @param mixed $condition 条件
* @param string $type JOIN类型
* @return $this
*/
public function join($join, $condition = null, $type = 'INNER')
@@ -660,7 +660,7 @@ class Query
}
if (count($join)) {
// 有设置第二个元素则把第二元素作为表前缀
$table = (string) current($join) . $table;
$table = (string)current($join) . $table;
} elseif (false === strpos($table, '.')) {
// 加上默认的表前缀
$table = $prefix . $table;
@@ -687,8 +687,8 @@ class Query
/**
* 查询SQL组装 union
* @access public
* @param mixed $union
* @param boolean $all
* @param mixed $union
* @param boolean $all
* @return $this
*/
public function union($union, $all = false)
@@ -706,11 +706,11 @@ class Query
/**
* 指定查询字段 支持字段排除和指定数据表
* @access public
* @param mixed $field
* @param boolean $except 是否排除
* @param string $tableName 数据表名
* @param string $prefix 字段前缀
* @param string $alias 别名前缀
* @param mixed $field
* @param boolean $except 是否排除
* @param string $tableName 数据表名
* @param string $prefix 字段前缀
* @param string $alias 别名前缀
* @return $this
*/
public function field($field, $except = false, $tableName = '', $prefix = '', $alias = '')
@@ -751,10 +751,10 @@ class Query
/**
* 指定JOIN查询字段
* @access public
* @param string|array $table 数据表
* @param string|array $field 查询字段
* @param string|array $on JOIN条件
* @param string $type JOIN类型
* @param string|array $table 数据表
* @param string|array $field 查询字段
* @param string|array $on JOIN条件
* @param string $type JOIN类型
* @return $this
*/
public function view($join, $field = null, $on = null, $type = 'INNER')
@@ -807,9 +807,9 @@ class Query
/**
* 设置分表规则
* @access public
* @param array $data 操作的数据
* @param string $field 分表依据的字段
* @param array $rule 分表规则
* @param array $data 操作的数据
* @param string $field 分表依据的字段
* @param array $rule 分表规则
* @return $this
*/
public function partition($data, $field, $rule = [])
@@ -821,9 +821,9 @@ class Query
/**
* 指定AND查询条件
* @access public
* @param mixed $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @param mixed $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @return $this
*/
public function where($field, $op = null, $condition = null)
@@ -837,9 +837,9 @@ class Query
/**
* 指定OR查询条件
* @access public
* @param mixed $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @param mixed $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @return $this
*/
public function whereOr($field, $op = null, $condition = null)
@@ -853,9 +853,9 @@ class Query
/**
* 指定XOR查询条件
* @access public
* @param mixed $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @param mixed $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @return $this
*/
public function whereXor($field, $op = null, $condition = null)
@@ -869,11 +869,11 @@ class Query
/**
* 分析查询表达式
* @access public
* @param string $logic 查询逻辑 and or xor
* @param string|array|\Closure $field 查询字段
* @param mixed $op 查询表达式
* @param string $logic 查询逻辑 and or xor
* @param string|array|\Closure $field 查询字段
* @param mixed $op 查询表达式
* @param mixed $condition 查询条件
* @param array $param 查询参数
* @param array $param 查询参数
* @return void
*/
protected function parseWhereExp($logic, $field, $op, $condition, $param = [])
@@ -940,7 +940,7 @@ class Query
/**
* 指定分页
* @access public
* @param mixed $page 页数
* @param mixed $page 页数
* @param mixed $listRows 每页数量
* @return $this
*/
@@ -955,16 +955,16 @@ class Query
/**
* 分页查询
* @param int|null $listRows 每页数量
* @param bool $simple 简洁模式
* @param array $config 配置参数
* page:当前页,
* path:url路径,
* query:url额外参数,
* fragment:url锚点,
* var_page:分页变量,
* list_rows:每页数量
* type:分页类名
* @param int|null $listRows 每页数量
* @param bool $simple 简洁模式
* @param array $config 配置参数
* page:当前页,
* path:url路径,
* query:url额外参数,
* fragment:url锚点,
* var_page:分页变量,
* list_rows:每页数量
* type:分页类名
* @return \think\paginator\Collection
* @throws DbException
*/
@@ -972,8 +972,10 @@ class Query
{
$config = array_merge(Config::get('paginate'), $config);
$listRows = $listRows ?: $config['list_rows'];
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
$page = isset($config['page']) ? (int) $config['page'] : call_user_func([
/** @var Paginator $class */
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
$page = isset($config['page']) ? (int)$config['page'] : call_user_func([
$class,
'getCurrentPage',
], $config['var_page']);
@@ -981,8 +983,7 @@ class Query
$page = $page < 1 ? 1 : $page;
$config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']);
/** @var Paginator $paginator */
if (!$simple) {
$options = $this->getOptions();
$total = $this->count();
@@ -992,8 +993,7 @@ class Query
$total = null;
}
$paginator = new $class($results, $listRows, $page, $simple, $total, $config);
return $paginator->items();
return $class::make($results, $listRows, $page, $total, $simple, $config);
}
/**
@@ -1023,8 +1023,8 @@ class Query
/**
* 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])
* @access public
* @param string|array $field 排序字段
* @param string $order 排序
* @param string|array $field 排序字段
* @param string $order 排序
* @return $this
*/
public function order($field, $order = null)
@@ -1053,8 +1053,8 @@ class Query
/**
* 查询缓存
* @access public
* @param mixed $key 缓存key
* @param integer $expire 缓存有效期
* @param mixed $key 缓存key
* @param integer $expire 缓存有效期
* @return $this
*/
public function cache($key = true, $expire = null)
@@ -1241,9 +1241,9 @@ class Query
/**
* 查询日期或者时间
* @access public
* @param string $field 日期字段名
* @param string $op 比较运算符或者表达式
* @param string|array $range 比较范围
* @param string $field 日期字段名
* @param string $op 比较运算符或者表达式
* @param string|array $range 比较范围
* @return $this
*/
public function whereTime($field, $op, $range = null)
@@ -1303,7 +1303,7 @@ class Query
* 获取数据表信息
* @access public
* @param string $tableName 数据表名 留空自动获取
* @param string $fetch 获取信息类型 包括 fields type bind pk
* @param string $fetch 获取信息类型 包括 fields type bind pk
* @return mixed
*/
public function getTableInfo($tableName = '', $fetch = '')
@@ -1326,7 +1326,7 @@ class Query
if (!isset($this->info[$guid])) {
$info = $this->connection->getFields($tableName);
$fields = array_keys($info);
$bind = $type = [];
$bind = $type = [];
foreach ($info as $key => $val) {
// 记录字段类型
$type[$key] = $val['type'];
@@ -1366,9 +1366,9 @@ class Query
/**
* 参数绑定
* @access public
* @param mixed $key 参数名
* @param mixed $value 绑定变量值
* @param integer $type 绑定类型
* @param mixed $key 参数名
* @param mixed $value 绑定变量值
* @param integer $type 绑定类型
* @return $this
*/
public function bind($key, $value = false, $type = PDO::PARAM_STR)
@@ -1444,7 +1444,7 @@ class Query
$relation = $key;
$with[$key] = $key;
} elseif (is_string($relation) && strpos($relation, '.')) {
$with[$key] = $relation;
$with[$key] = $relation;
list($relation, $subRelation) = explode('.', $relation, 2);
}
@@ -1479,7 +1479,7 @@ class Query
if ($closure) {
// 执行闭包查询
call_user_func_array($closure, [ & $this]);
call_user_func_array($closure, [& $this]);
//指定获取关联的字段
//需要在 回调中 调方法 withField 方法,如
// $query->where(['id'=>1])->withField('id,name');
@@ -1542,8 +1542,8 @@ class Query
/**
* 把主键值转换为查询条件 支持复合主键
* @access public
* @param array|string $data 主键数据
* @param mixed $options 表达式参数
* @param array|string $data 主键数据
* @param mixed $options 表达式参数
* @return void
* @throws Exception
*/
@@ -1587,10 +1587,10 @@ class Query
/**
* 插入记录
* @access public
* @param mixed $data 数据
* @param boolean $replace 是否replace
* @param boolean $getLastInsID 是否获取自增ID
* @param string $sequence 自增序列名
* @param mixed $data 数据
* @param boolean $replace 是否replace
* @param boolean $getLastInsID 是否获取自增ID
* @param string $sequence 自增序列名
* @return integer|string
*/
public function insert(array $data, $replace = false, $getLastInsID = false, $sequence = null)
@@ -1611,9 +1611,9 @@ class Query
/**
* 插入记录并获取自增ID
* @access public
* @param mixed $data 数据
* @param boolean $replace 是否replace
* @param string $sequence 自增序列名
* @param mixed $data 数据
* @param boolean $replace 是否replace
* @param string $sequence 自增序列名
* @return integer|string
*/
public function insertGetId(array $data, $replace = false, $sequence = null)
@@ -1648,8 +1648,8 @@ class Query
/**
* 通过Select方式插入记录
* @access public
* @param string $fields 要插入的数据表字段名
* @param string $table 要插入的数据表名
* @param string $fields 要插入的数据表字段名
* @param string $table 要插入的数据表名
* @return integer|string
* @throws PDOException
*/
@@ -1736,7 +1736,7 @@ class Query
if ($data instanceof Query) {
return $data->select();
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $this]);
call_user_func_array($data, [& $this]);
$data = null;
}
// 分析查询表达式
@@ -1819,7 +1819,7 @@ class Query
if ($data instanceof Query) {
return $data->find();
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $this]);
call_user_func_array($data, [& $this]);
$data = null;
}
// 分析查询表达式
@@ -1935,9 +1935,9 @@ class Query
/**
* 分批数据返回处理
* @access public
* @param integer $count 每次处理的数据数量
* @param callable $callback 处理回调方法
* @param string $column 分批处理的字段名
* @param integer $count 每次处理的数据数量
* @param callable $callback 处理回调方法
* @param string $column 分批处理的字段名
* @return boolean
*/
public function chunk($count, $callback, $column = null)
@@ -2112,10 +2112,10 @@ class Query
if (isset($options['page'])) {
// 根据页数计算limit
list($page, $listRows) = $options['page'];
$page = $page > 0 ? $page : 1;
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$options['limit'] = $offset . ',' . $listRows;
$page = $page > 0 ? $page : 1;
$listRows = $listRows > 0 ? $listRows : (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$options['limit'] = $offset . ',' . $listRows;
}
$this->options = [];

View File

@@ -23,6 +23,8 @@ use think\Paginator;
* @method string render()
* @method Paginator fragment($fragment)
* @method Paginator appends($key, $value)
* @method integer lastPage()
* @method boolean hasPages()
*/
class Collection extends \think\Collection
{
@@ -40,17 +42,7 @@ class Collection extends \think\Collection
{
return new static($items, $paginator);
}
public function setPaginator(Paginator $paginator)
{
$this->paginator = $paginator;
}
public function getPaginator()
{
return $this->paginator;
}
public function toArray()
{
if ($this->paginator) {

4
tests/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
/runtime/
/application/common.php
/application/demo/
/application/runtime/

View File

@@ -48,16 +48,12 @@ class appTest extends \PHPUnit_Framework_TestCase
{
public function testRun()
{
App::run(Request::create("http://www.example.com"))->send();
$response = App::run(Request::create("http://www.example.com"));
$expectOutputString = '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
$this->expectOutputString($expectOutputString);
$rc = new ReflectionClass('\think\Loader');
$ns = $rc->getProperty('prefixDirsPsr4');
$ns->setAccessible(true);
$namespace = $ns->getValue();
$this->assertEquals([realpath(TEST_PATH)], $namespace['tests\\']);
$this->assertEquals($expectOutputString, $response->getContent());
$this->assertEquals(200, $response->getCode());
$this->assertEquals(true, function_exists('lang'));
$this->assertEquals(true, function_exists('config'));

View File

@@ -0,0 +1,44 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace tests\thinkphp\library\think;
use think\paginator\driver\Bootstrap;
class paginateTest extends \PHPUnit_Framework_TestCase
{
public function testPaginatorInfo()
{
$p = Bootstrap::make($array = ['item3', 'item4'], 2, 2, 4);
$this->assertEquals(4, $p->total());
$this->assertEquals(2, $p->listRows());
$this->assertEquals(2, $p->currentPage());
$p2 = Bootstrap::make($array2 = ['item3', 'item4'], 2, 2, 2);
$this->assertEquals(1, $p2->currentPage());
}
public function testPaginatorRender()
{
$p = Bootstrap::make($array = ['item3', 'item4'], 2, 2, 100);
$render = '<ul class="pagination"><li><a href="/?page=1">&laquo;</a></li> <li><a href="/?page=1">1</a></li><li class="active"><span>2</span></li><li><a href="/?page=3">3</a></li><li><a href="/?page=4">4</a></li><li><a href="/?page=5">5</a></li><li><a href="/?page=6">6</a></li><li><a href="/?page=7">7</a></li><li><a href="/?page=8">8</a></li><li class="disabled"><span>...</span></li><li><a href="/?page=49">49</a></li><li><a href="/?page=50">50</a></li> <li><a href="/?page=3">&raquo;</a></li></ul>';
$this->assertEquals($render, $p->render());
}
}