|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mdiyakov\DoctrineSolrBundle\Finder; |
|
4
|
|
|
|
|
5
|
|
|
use Mdiyakov\DoctrineSolrBundle\Config\Config; |
|
6
|
|
|
use Mdiyakov\DoctrineSolrBundle\Query\Select\MultiClassSelectQuery; |
|
7
|
|
|
use Mdiyakov\DoctrineSolrBundle\Query\SelectQueryBuilder; |
|
8
|
|
|
use Mdiyakov\DoctrineSolrBundle\Schema\Schema; |
|
9
|
|
|
|
|
10
|
|
|
class SchemaFinder extends AbstractFinder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var SelectQueryBuilder |
|
14
|
|
|
*/ |
|
15
|
|
|
private $queryBuilder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var MultiClassSelectQuery |
|
19
|
|
|
*/ |
|
20
|
|
|
private $selectQuery; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array[][] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $entityConfigs; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $selectedClasses = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Schema |
|
34
|
|
|
*/ |
|
35
|
|
|
private $schema; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param SelectQueryBuilder $queryBuilder |
|
39
|
|
|
* @param Schema $schema |
|
40
|
|
|
* @param Config $config |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(SelectQueryBuilder $queryBuilder, Schema $schema, Config $config) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->queryBuilder = $queryBuilder; |
|
45
|
|
|
$this->schema = $schema; |
|
46
|
|
|
|
|
47
|
|
|
foreach ($config->getIndexedEntities() as $entityConfig) { |
|
48
|
|
|
if ($schema->getName() != $entityConfig['schema']) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
$this->entityConfigs[$entityConfig['class']] = $entityConfig; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $class |
|
57
|
|
|
*/ |
|
58
|
|
|
public function addSelectClass($class) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!array_key_exists($class, $this->entityConfigs)) { |
|
61
|
|
|
throw new \InvalidArgumentException( |
|
62
|
|
|
sprintf('Class "%s" is not configured to be used with schema "%s"', $class, $this->getSchema()->getName()) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (array_search($class, $this->selectedClasses) === false) { |
|
67
|
|
|
$this->selectedClasses[] = $class; |
|
68
|
|
|
$this->selectQuery = null; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $class |
|
74
|
|
|
*/ |
|
75
|
|
|
public function removeClass($class) |
|
76
|
|
|
{ |
|
77
|
|
|
$key = array_search($class, $this->selectedClasses); |
|
78
|
|
|
if ($key !== false) { |
|
79
|
|
|
unset($this->selectedClasses[$key]); |
|
80
|
|
|
$this->selectQuery = null; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return Schema |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getSchema() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->schema; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return MultiClassSelectQuery |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function getQuery() |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$this->selectQuery) { |
|
98
|
|
|
$this->selectQuery = $this->queryBuilder |
|
99
|
|
|
->buildMultiClassSelectQuery( |
|
100
|
|
|
$this->schema, |
|
101
|
|
|
$this->selectedClasses |
|
102
|
|
|
); |
|
103
|
|
|
} else { |
|
104
|
|
|
$this->selectQuery->reset(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->selectQuery; |
|
108
|
|
|
} |
|
109
|
|
|
} |