| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the FOSElasticaBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace FOS\ElasticaBundle\Doctrine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Doctrine\Common\Persistence\ManagerRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Doctrine\ORM\Query\Expr\From; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Doctrine\ORM\QueryBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use FOS\ElasticaBundle\Provider\PagerfantaPager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use FOS\ElasticaBundle\Provider\PagerProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Pagerfanta\Adapter\DoctrineORMAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Pagerfanta\Pagerfanta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class ORMPagerProvider implements PagerProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     const ENTITY_ALIAS = 'a'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $objectClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var ManagerRegistry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $doctrine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $baseOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var RegisterListenersService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $registerListenersService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param ManagerRegistry $doctrine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param RegisterListenersService $registerListenersService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $objectClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 4 |  |      * @param array $baseOptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |     public function __construct(ManagerRegistry $doctrine, RegisterListenersService $registerListenersService, $objectClass, array $baseOptions) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 4 |  |         $this->doctrine = $doctrine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |         $this->objectClass = $objectClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  |         $this->baseOptions = $baseOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->registerListenersService = $registerListenersService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 63 | 3 |  |     public function provide(array $options = array()) | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 65 | 3 |  |         $options = array_replace($this->baseOptions, $options); | 
            
                                                        
            
                                    
            
            
                | 66 | 3 |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         $manager = $this->doctrine->getManagerForClass($this->objectClass); | 
            
                                                        
            
                                    
            
            
                | 68 | 3 |  |         $repository = $manager->getRepository($this->objectClass); | 
            
                                                        
            
                                    
            
            
                | 69 | 3 |  |  | 
            
                                                        
            
                                    
            
            
                | 70 | 3 |  |         $qb = \call_user_func([$repository, $options['query_builder_method']], self::ENTITY_ALIAS); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         // Ensure that the query builder has a sorting configured. Without a ORDER BY clause, the SQL standard does not | 
            
                                                        
            
                                    
            
            
                | 73 | 3 |  |         // guarantee any order, which breaks the pagination (second page might use a different sorting that when retrieving | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         // the first page). | 
            
                                                        
            
                                    
            
            
                | 75 | 3 |  |         // If the QueryBuilder already has its own ordering, or the method returned a Query instead of a QueryBuilder, we | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         // assume that the query already provides a proper sorting. This allows giving full control over sorting if wanted | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         // when using a custom method. | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         if ($qb instanceof QueryBuilder && empty($qb->getDQLPart('orderBy'))) { | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             // When getting root aliases, the QueryBuilder normalizes all from parts to From objects, in case they were added as string using the low-level API. | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             // This side-effect allows us to be sure to get only From objects in the next call. | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             $qb->getRootAliases(); | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |             /** @var From[] $fromClauses */ | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |             $fromClauses = $qb->getDQLPart('from'); | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |             foreach ($fromClauses as $fromClause) { | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |                 $identifiers = $manager->getClassMetadata($fromClause->getFrom())->getIdentifierFieldNames(); | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |                 foreach ($identifiers as $identifier) { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |                     $qb->addOrderBy($fromClause->getAlias().'.'.$identifier); | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         $pager = new PagerfantaPager(new Pagerfanta(new DoctrineORMAdapter($qb))); | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         $this->registerListenersService->register($manager, $pager, $options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         return $pager; | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                        
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: