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