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 FOS\ElasticaBundle\Provider\PagerfantaPager; |
16
|
|
|
use FOS\ElasticaBundle\Provider\PagerProviderInterface; |
17
|
|
|
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; |
18
|
|
|
use Pagerfanta\Pagerfanta; |
19
|
|
|
|
20
|
|
|
final class MongoDBPagerProvider implements PagerProviderInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $objectClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ManagerRegistry |
29
|
|
|
*/ |
30
|
|
|
private $doctrine; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $baseOptions; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RegisterListenersService |
39
|
|
|
*/ |
40
|
|
|
private $registerListenersService; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ManagerRegistry $doctrine |
44
|
|
|
* @param RegisterListenersService $registerListenersService |
45
|
|
|
* @param string $objectClass |
46
|
|
|
* @param array $baseOptions |
47
|
|
|
*/ |
48
|
|
|
public function __construct(ManagerRegistry $doctrine, RegisterListenersService $registerListenersService, $objectClass, array $baseOptions) |
49
|
|
|
{ |
50
|
|
|
$this->doctrine = $doctrine; |
51
|
|
|
$this->objectClass = $objectClass; |
52
|
|
|
$this->baseOptions = $baseOptions; |
53
|
|
|
$this->registerListenersService = $registerListenersService; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function provide(array $options = array()) |
60
|
|
|
{ |
61
|
|
|
$options = array_replace($this->baseOptions, $options); |
62
|
|
|
|
63
|
|
|
$manager = $this->doctrine->getManagerForClass($this->objectClass); |
64
|
|
|
$repository = $manager->getRepository($this->objectClass); |
65
|
|
|
|
66
|
|
|
$pager = new PagerfantaPager(new Pagerfanta( |
67
|
|
|
new DoctrineODMMongoDBAdapter(call_user_func([$repository, $options['query_builder_method']])) |
68
|
|
|
)); |
69
|
|
|
|
70
|
|
|
$this->registerListenersService->register($manager, $pager, $options); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $pager; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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: