Paginator optimizations.

This commit is contained in:
Buster Neece 2022-11-04 19:20:42 -05:00
parent 870afbfecd
commit 657d97a4cb
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
1 changed files with 11 additions and 10 deletions

View File

@ -11,15 +11,14 @@ use Countable;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Generator;
use IteratorAggregate;
use loophp\collection\Collection as loophpCollection;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Doctrine\Collections\CollectionAdapter;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Traversable;
/**
* @template TKey of array-key
@ -120,9 +119,16 @@ final class Paginator implements IteratorAggregate, Countable
$this->isDisabled = $isDisabled;
}
public function getIterator(): Traversable
public function getIterator(): Generator
{
return $this->paginator->getIterator();
$iterator = $this->paginator->getIterator();
if ($this->postprocessor) {
foreach ($iterator as $row) {
yield ($this->postprocessor)($row, $this);
}
} else {
yield from $iterator;
}
}
public function count(): int
@ -144,12 +150,7 @@ final class Paginator implements IteratorAggregate, Countable
$totalPages = $this->paginator->getNbPages();
$collection = loophpCollection::fromIterable($this->getIterator());
if ($this->postprocessor) {
$collection = $collection->map($this->postprocessor);
}
$results = $collection->all();
$results = iterator_to_array($this->getIterator(), false);
if ($this->isDisabled) {
return $response->withJson($results);