Exception
9 months ago
CountOutputWalker.php
9 months ago
CountWalker.php
9 months ago
LimitSubqueryOutputWalker.php
9 months ago
LimitSubqueryWalker.php
9 months ago
Paginator.php
9 months ago
RootTypeWalker.php
9 months ago
RowNumberOverFunction.php
9 months ago
WhereInWalker.php
9 months ago
index.php
9 months ago
Paginator.php
178 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Tools\Pagination; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use ArrayIterator; |
| 6 | use Countable; |
| 7 | use MailPoetVendor\Doctrine\Common\Collections\Collection; |
| 8 | use MailPoetVendor\Doctrine\ORM\Internal\SQLResultCasing; |
| 9 | use MailPoetVendor\Doctrine\ORM\NoResultException; |
| 10 | use MailPoetVendor\Doctrine\ORM\Query; |
| 11 | use MailPoetVendor\Doctrine\ORM\Query\Parameter; |
| 12 | use MailPoetVendor\Doctrine\ORM\Query\Parser; |
| 13 | use MailPoetVendor\Doctrine\ORM\Query\ResultSetMapping; |
| 14 | use MailPoetVendor\Doctrine\ORM\QueryBuilder; |
| 15 | use IteratorAggregate; |
| 16 | use ReturnTypeWillChange; |
| 17 | use Traversable; |
| 18 | use function array_key_exists; |
| 19 | use function array_map; |
| 20 | use function array_sum; |
| 21 | use function assert; |
| 22 | use function is_string; |
| 23 | class Paginator implements Countable, IteratorAggregate |
| 24 | { |
| 25 | use SQLResultCasing; |
| 26 | public const HINT_ENABLE_DISTINCT = 'paginator.distinct.enable'; |
| 27 | private $query; |
| 28 | private $fetchJoinCollection; |
| 29 | private $useOutputWalkers; |
| 30 | private $count; |
| 31 | public function __construct($query, $fetchJoinCollection = \true) |
| 32 | { |
| 33 | if ($query instanceof QueryBuilder) { |
| 34 | $query = $query->getQuery(); |
| 35 | } |
| 36 | $this->query = $query; |
| 37 | $this->fetchJoinCollection = (bool) $fetchJoinCollection; |
| 38 | } |
| 39 | public function getQuery() |
| 40 | { |
| 41 | return $this->query; |
| 42 | } |
| 43 | public function getFetchJoinCollection() |
| 44 | { |
| 45 | return $this->fetchJoinCollection; |
| 46 | } |
| 47 | public function getUseOutputWalkers() |
| 48 | { |
| 49 | return $this->useOutputWalkers; |
| 50 | } |
| 51 | public function setUseOutputWalkers($useOutputWalkers) |
| 52 | { |
| 53 | $this->useOutputWalkers = $useOutputWalkers; |
| 54 | return $this; |
| 55 | } |
| 56 | #[\ReturnTypeWillChange] |
| 57 | public function count() |
| 58 | { |
| 59 | if ($this->count === null) { |
| 60 | try { |
| 61 | $this->count = (int) array_sum(array_map('current', $this->getCountQuery()->getScalarResult())); |
| 62 | } catch (NoResultException $e) { |
| 63 | $this->count = 0; |
| 64 | } |
| 65 | } |
| 66 | return $this->count; |
| 67 | } |
| 68 | #[\ReturnTypeWillChange] |
| 69 | public function getIterator() |
| 70 | { |
| 71 | $offset = $this->query->getFirstResult(); |
| 72 | $length = $this->query->getMaxResults(); |
| 73 | if ($this->fetchJoinCollection && $length !== null) { |
| 74 | $subQuery = $this->cloneQuery($this->query); |
| 75 | if ($this->useOutputWalker($subQuery)) { |
| 76 | $subQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class); |
| 77 | } else { |
| 78 | $this->appendTreeWalker($subQuery, LimitSubqueryWalker::class); |
| 79 | $this->unbindUnusedQueryParams($subQuery); |
| 80 | } |
| 81 | $subQuery->setFirstResult($offset)->setMaxResults($length); |
| 82 | $foundIdRows = $subQuery->getScalarResult(); |
| 83 | // don't do this for an empty id array |
| 84 | if ($foundIdRows === []) { |
| 85 | return new ArrayIterator([]); |
| 86 | } |
| 87 | $whereInQuery = $this->cloneQuery($this->query); |
| 88 | $ids = array_map('current', $foundIdRows); |
| 89 | $this->appendTreeWalker($whereInQuery, WhereInWalker::class); |
| 90 | $whereInQuery->setHint(WhereInWalker::HINT_PAGINATOR_HAS_IDS, \true); |
| 91 | $whereInQuery->setFirstResult(0)->setMaxResults(null); |
| 92 | $whereInQuery->setCacheable($this->query->isCacheable()); |
| 93 | $databaseIds = $this->convertWhereInIdentifiersToDatabaseValues($ids); |
| 94 | $whereInQuery->setParameter(WhereInWalker::PAGINATOR_ID_ALIAS, $databaseIds); |
| 95 | $result = $whereInQuery->getResult($this->query->getHydrationMode()); |
| 96 | } else { |
| 97 | $result = $this->cloneQuery($this->query)->setMaxResults($length)->setFirstResult($offset)->setCacheable($this->query->isCacheable())->getResult($this->query->getHydrationMode()); |
| 98 | } |
| 99 | return new ArrayIterator($result); |
| 100 | } |
| 101 | private function cloneQuery(Query $query) : Query |
| 102 | { |
| 103 | $cloneQuery = clone $query; |
| 104 | $cloneQuery->setParameters(clone $query->getParameters()); |
| 105 | $cloneQuery->setCacheable(\false); |
| 106 | foreach ($query->getHints() as $name => $value) { |
| 107 | $cloneQuery->setHint($name, $value); |
| 108 | } |
| 109 | return $cloneQuery; |
| 110 | } |
| 111 | private function useOutputWalker(Query $query) : bool |
| 112 | { |
| 113 | if ($this->useOutputWalkers === null) { |
| 114 | return (bool) $query->getHint(Query::HINT_CUSTOM_OUTPUT_WALKER) === \false; |
| 115 | } |
| 116 | return $this->useOutputWalkers; |
| 117 | } |
| 118 | private function appendTreeWalker(Query $query, string $walkerClass) : void |
| 119 | { |
| 120 | $hints = $query->getHint(Query::HINT_CUSTOM_TREE_WALKERS); |
| 121 | if ($hints === \false) { |
| 122 | $hints = []; |
| 123 | } |
| 124 | $hints[] = $walkerClass; |
| 125 | $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, $hints); |
| 126 | } |
| 127 | private function getCountQuery() : Query |
| 128 | { |
| 129 | $countQuery = new Query($this->query->getEntityManager()); |
| 130 | $countQuery->setDQL($this->query->getDQL()); |
| 131 | $countQuery->setParameters(clone $this->query->getParameters()); |
| 132 | $countQuery->setCacheable(\false); |
| 133 | foreach ($this->query->getHints() as $name => $value) { |
| 134 | $countQuery->setHint($name, $value); |
| 135 | } |
| 136 | if (!$countQuery->hasHint(CountWalker::HINT_DISTINCT)) { |
| 137 | $countQuery->setHint(CountWalker::HINT_DISTINCT, \true); |
| 138 | } |
| 139 | if ($this->useOutputWalker($countQuery)) { |
| 140 | $platform = $countQuery->getEntityManager()->getConnection()->getDatabasePlatform(); |
| 141 | // law of demeter win |
| 142 | $rsm = new ResultSetMapping(); |
| 143 | $rsm->addScalarResult($this->getSQLResultCasing($platform, 'dctrn_count'), 'count'); |
| 144 | $countQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountOutputWalker::class); |
| 145 | $countQuery->setResultSetMapping($rsm); |
| 146 | } else { |
| 147 | $this->appendTreeWalker($countQuery, CountWalker::class); |
| 148 | $this->unbindUnusedQueryParams($countQuery); |
| 149 | } |
| 150 | $countQuery->setFirstResult(0)->setMaxResults(null); |
| 151 | return $countQuery; |
| 152 | } |
| 153 | private function unbindUnusedQueryParams(Query $query) : void |
| 154 | { |
| 155 | $parser = new Parser($query); |
| 156 | $parameterMappings = $parser->parse()->getParameterMappings(); |
| 157 | $parameters = $query->getParameters(); |
| 158 | foreach ($parameters as $key => $parameter) { |
| 159 | $parameterName = $parameter->getName(); |
| 160 | if (!(isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings))) { |
| 161 | unset($parameters[$key]); |
| 162 | } |
| 163 | } |
| 164 | $query->setParameters($parameters); |
| 165 | } |
| 166 | private function convertWhereInIdentifiersToDatabaseValues(array $identifiers) : array |
| 167 | { |
| 168 | $query = $this->cloneQuery($this->query); |
| 169 | $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, RootTypeWalker::class); |
| 170 | $connection = $this->query->getEntityManager()->getConnection(); |
| 171 | $type = $query->getSQL(); |
| 172 | assert(is_string($type)); |
| 173 | return array_map(static function ($id) use($connection, $type) { |
| 174 | return $connection->convertToDatabaseValue($id, $type); |
| 175 | }, $identifiers); |
| 176 | } |
| 177 | } |
| 178 |