1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Firesphere\SolrSearch\Helpers\FieldResolver; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use Solarium\Client; |
10
|
|
|
use Solarium\QueryType\Server\CoreAdmin\Query\Query; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait CoreServiceTrait to have simple methods that don't really need to be in core. |
14
|
|
|
* |
15
|
|
|
* Trait to support basic settings for the Solr Core Service |
16
|
|
|
* |
17
|
|
|
* @package Firesphere\SolrSearch\Traits |
18
|
|
|
*/ |
19
|
|
|
trait CoreServiceTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Add debugging information |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $inDebugMode = false; |
27
|
|
|
/** |
28
|
|
|
* @var Client The current client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
/** |
32
|
|
|
* @var Query A core admin object |
33
|
|
|
*/ |
34
|
|
|
protected $admin; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check if we are in debug mode |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function isInDebugMode(): bool |
42
|
|
|
{ |
43
|
|
|
return $this->inDebugMode; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the debug mode |
48
|
|
|
* |
49
|
|
|
* @param bool $inDebugMode |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public function setInDebugMode(bool $inDebugMode): self |
53
|
|
|
{ |
54
|
|
|
$this->inDebugMode = $inDebugMode; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Is the given class a valid class to index |
61
|
|
|
* Does not discriminate against the indexes. All indexes are worth the same |
62
|
|
|
* |
63
|
|
|
* @param string $class |
64
|
|
|
* @return bool |
65
|
|
|
* @throws ReflectionException |
66
|
|
|
*/ |
67
|
|
|
public function isValidClass($class): bool |
68
|
|
|
{ |
69
|
|
|
$classes = $this->getValidClasses(); |
70
|
|
|
|
71
|
|
|
return in_array($class, $classes, true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get all classes from all indexes and return them. |
76
|
|
|
* Used to get all classes that are to be indexed on change |
77
|
|
|
* Note, only base classes are in this object. A publish recursive is required |
78
|
|
|
* when any change from a relation is published. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
* @throws ReflectionException |
82
|
|
|
*/ |
83
|
|
|
public function getValidClasses(): array |
84
|
|
|
{ |
85
|
|
|
$indexes = $this->getValidIndexes(); |
|
|
|
|
86
|
|
|
$classes = []; |
87
|
|
|
foreach ($indexes as $index) { |
88
|
|
|
$classes = $this->getClassesInHierarchy($index, $classes); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return array_unique($classes); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the classes in hierarchy to see if it's valid |
96
|
|
|
* |
97
|
|
|
* @param $index |
98
|
|
|
* @param array $classes |
99
|
|
|
* @return array |
100
|
|
|
* @throws ReflectionException |
101
|
|
|
*/ |
102
|
|
|
protected function getClassesInHierarchy($index, array $classes): array |
103
|
|
|
{ |
104
|
|
|
$indexClasses = singleton($index)->getClasses(); |
105
|
|
|
foreach ($indexClasses as $class) { |
106
|
|
|
$classes = array_merge($classes, FieldResolver::getHierarchy($class, true)); |
107
|
|
|
} |
108
|
|
|
return $classes; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the client |
113
|
|
|
* |
114
|
|
|
* @return Client |
115
|
|
|
*/ |
116
|
|
|
public function getClient(): Client |
117
|
|
|
{ |
118
|
|
|
return $this->client; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the client |
123
|
|
|
* |
124
|
|
|
* @param Client $client |
125
|
|
|
* @return self |
126
|
|
|
*/ |
127
|
|
|
public function setClient($client): self |
128
|
|
|
{ |
129
|
|
|
$this->client = $client; |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Query |
136
|
|
|
*/ |
137
|
|
|
public function getAdmin(): Query |
138
|
|
|
{ |
139
|
|
|
return $this->admin; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Query $admin |
144
|
|
|
*/ |
145
|
|
|
public function setAdmin(Query $admin): void |
146
|
|
|
{ |
147
|
|
|
$this->admin = $admin; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|