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