modify
This commit is contained in:
+150
-150
@@ -17,186 +17,186 @@ use Swoole\Coroutine;
|
|||||||
class Pagination extends Component
|
class Pagination extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var ActiveQuery */
|
/** @var ActiveQuery */
|
||||||
private ActiveQuery $activeQuery;
|
private ActiveQuery $activeQuery;
|
||||||
|
|
||||||
/** @var int 从第几个开始查 */
|
/** @var int 从第几个开始查 */
|
||||||
private int $_offset = 0;
|
private int $_offset = 0;
|
||||||
|
|
||||||
/** @var int 每页数量 */
|
/** @var int 每页数量 */
|
||||||
private int $_limit = 100;
|
private int $_limit = 100;
|
||||||
|
|
||||||
/** @var int 最大查询数量 */
|
/** @var int 最大查询数量 */
|
||||||
private int $_max = 0;
|
private int $_max = 0;
|
||||||
|
|
||||||
/** @var int 当前已查询数量 */
|
/** @var int 当前已查询数量 */
|
||||||
private int $_length = 0;
|
private int $_length = 0;
|
||||||
|
|
||||||
/** @var Closure */
|
/** @var Closure */
|
||||||
private Closure $_callback;
|
private Closure $_callback;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PaginationIteration constructor.
|
* PaginationIteration constructor.
|
||||||
* @param ActiveQuery $activeQuery
|
* @param ActiveQuery $activeQuery
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
||||||
{
|
{
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
$this->activeQuery = $activeQuery;
|
$this->activeQuery = $activeQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function clean()
|
public function clean()
|
||||||
{
|
{
|
||||||
unset($this->activeQuery, $this->_callback, $this->_group);
|
unset($this->activeQuery, $this->_callback, $this->_group);
|
||||||
$this->_offset = 0;
|
$this->_offset = 0;
|
||||||
$this->_limit = 100;
|
$this->_limit = 100;
|
||||||
$this->_max = 0;
|
$this->_max = 0;
|
||||||
$this->_length = 0;;
|
$this->_length = 0;;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* recover class by clone
|
* recover class by clone
|
||||||
*/
|
*/
|
||||||
public function __clone()
|
public function __clone()
|
||||||
{
|
{
|
||||||
$this->clean();
|
$this->clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure $callback
|
* @param array|Closure $callback
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setCallback(array|Closure $callback)
|
public function setCallback(array|Closure $callback)
|
||||||
{
|
{
|
||||||
if (!is_callable($callback, true)) {
|
if (!is_callable($callback, true)) {
|
||||||
throw new Exception('非法回调函数~');
|
throw new Exception('非法回调函数~');
|
||||||
}
|
}
|
||||||
$this->_callback = $callback;
|
$this->_callback = $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setOffset(int $number): static
|
public function setOffset(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
$number = 0;
|
$number = 0;
|
||||||
}
|
}
|
||||||
$this->_offset = $number;
|
$this->_offset = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setLimit(int $number): static
|
public function setLimit(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 1) {
|
if ($number < 1) {
|
||||||
$number = 100;
|
$number = 100;
|
||||||
} else if ($number > 5000) {
|
} else if ($number > 5000) {
|
||||||
$number = 5000;
|
$number = 5000;
|
||||||
}
|
}
|
||||||
$this->_limit = $number;
|
$this->_limit = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setMax(int $number): static
|
public function setMax(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
$this->_max = $number;
|
$this->_max = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $param
|
* @param array $param
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function plunk($param = [])
|
public function plunk($param = [])
|
||||||
{
|
{
|
||||||
$this->loop($param);
|
$this->loop($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 轮训
|
* 轮训
|
||||||
* @param $param
|
* @param $param
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function loop($param): array
|
public function loop($param): array
|
||||||
{
|
{
|
||||||
if ($this->_max > 0 && $this->_length >= $this->_max) {
|
if ($this->_max > 0 && $this->_length >= $this->_max) {
|
||||||
return $this->output();
|
return $this->output();
|
||||||
}
|
}
|
||||||
[$length, $data] = $this->get();
|
[$length, $data] = $this->get();
|
||||||
|
|
||||||
$this->executed($data, $param);
|
$this->executed($data, $param);
|
||||||
|
|
||||||
unset($data);
|
unset($data);
|
||||||
if ($length < $this->_limit) {
|
if ($length < $this->_limit) {
|
||||||
return $this->output();
|
return $this->output();
|
||||||
}
|
}
|
||||||
return $this->loop($param);
|
return $this->loop($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function output(): array
|
public function output(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $data
|
||||||
* @param $param
|
* @param $param
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function executed($data, $param): void
|
private function executed($data, $param): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
call_user_func($this->_callback, $data, $param);
|
call_user_func($this->_callback, $data, $param);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->addError($exception, 'throwable');
|
$this->addError($exception, 'throwable');
|
||||||
} finally {
|
} finally {
|
||||||
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
logger()->insert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Collection
|
* @return array|Collection
|
||||||
*/
|
*/
|
||||||
private function get(): Collection|array
|
private function get(): Collection|array
|
||||||
{
|
{
|
||||||
if ($this->_length + $this->_limit > $this->_max) {
|
if ($this->_length + $this->_limit > $this->_max) {
|
||||||
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
||||||
}
|
}
|
||||||
$data = $this->activeQuery->limit($this->_offset, $this->_limit)->get();
|
$data = $this->activeQuery->limit($this->_offset, $this->_limit)->get();
|
||||||
$this->_offset += $this->_limit;
|
$this->_offset += $this->_limit;
|
||||||
$this->_length += $data->size();
|
$this->_length += $data->size();
|
||||||
return [$data->size(), $data];
|
return [$data->size(), $data];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user