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 false;
  19.         if (isset($event->options['wrap-queries'])) {
  20.             $useOutputWalkers $event->options['wrap-queries'];
  21.         }
  22.         $event->target
  23.             ->setFirstResult($event->getOffset())
  24.             ->setMaxResults($event->getLimit())
  25.             ->setHint(CountWalker::HINT_DISTINCT$event->options['distinct'])
  26.         ;
  27.         $fetchJoinCollection true;
  28.         if ($event->target->hasHint(self::HINT_FETCH_JOIN_COLLECTION)) {
  29.             $fetchJoinCollection $event->target->getHint(self::HINT_FETCH_JOIN_COLLECTION);
  30.         } else if (isset($event->options['distinct'])) {
  31.             $fetchJoinCollection $event->options['distinct'];
  32.         }
  33.         $paginator = new Paginator($event->target$fetchJoinCollection);
  34.         $paginator->setUseOutputWalkers($useOutputWalkers);
  35.         if (($count $event->target->getHint(self::HINT_COUNT)) !== false) {
  36.             $event->count = (int) $count;
  37.         } else {
  38.             $event->count count($paginator);
  39.         }
  40.         $event->items iterator_to_array($paginator);
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             'knp_pager.items' => ['items'0],
  46.         ];
  47.     }
  48. }