vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ORM/QuerySubscriber.php line 16

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM;
  3. use Doctrine\ORM\Query;
  4. use Doctrine\ORM\Tools\Pagination\CountWalker;
  5. use Doctrine\ORM\Tools\Pagination\Paginator;
  6. use Knp\Component\Pager\Event\ItemsEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class QuerySubscriber implements EventSubscriberInterface
  9. {
  10.     public const HINT_COUNT 'knp_paginator.count';
  11.     public const HINT_FETCH_JOIN_COLLECTION 'knp_paginator.fetch_join_collection';
  12.     public function items(ItemsEvent $event): void
  13.     {
  14.         if (!$event->target instanceof Query) {
  15.             return;
  16.         }
  17.         $event->stopPropagation();
  18.         $useOutputWalkers $event->options['wrap-queries'] ?? false;
  19.         $event->target
  20.             ->setFirstResult($event->getOffset())
  21.             ->setMaxResults($event->getLimit())
  22.             ->setHint(CountWalker::HINT_DISTINCT$event->options['distinct'])
  23.         ;
  24.         $fetchJoinCollection true;
  25.         if ($event->target->hasHint(self::HINT_FETCH_JOIN_COLLECTION)) {
  26.             $fetchJoinCollection $event->target->getHint(self::HINT_FETCH_JOIN_COLLECTION);
  27.         } else if (isset($event->options['distinct'])) {
  28.             $fetchJoinCollection $event->options['distinct'];
  29.         }
  30.         $paginator = new Paginator($event->target$fetchJoinCollection);
  31.         $paginator->setUseOutputWalkers($useOutputWalkers);
  32.         if (($count $event->target->getHint(self::HINT_COUNT)) !== false) {
  33.             $event->count = (int) $count;
  34.         } else {
  35.             $event->count count($paginator);
  36.         }
  37.         $event->items iterator_to_array($paginator);
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             'knp_pager.items' => ['items'0],
  43.         ];
  44.     }
  45. }