1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\States; |
5
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Compat\FluentExtension; |
7
|
|
|
use Firesphere\SolrSearch\Helpers\FieldResolver; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use SilverStripe\Core\ClassInfo; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SiteState |
14
|
|
|
* |
15
|
|
|
* Determine and apply the state of the site currently. This is used at index-time to figure out what state to index. |
16
|
|
|
* An example of this is the FluentSearchVariant extension from Fluent. |
17
|
|
|
* |
18
|
|
|
* Fluent uses the old SearchVariant method, which is actually not that bad a concept. These "Variants", now called |
19
|
|
|
* "States" set the state of the site to a required setting for each available state. |
20
|
|
|
* |
21
|
|
|
* States SHOULD add their own states through an extension, otherwise it won't be called. |
22
|
|
|
* {@see FluentExtension::onBeforeInit()} |
23
|
|
|
* |
24
|
|
|
* States, options, etc. are simplified for a more streamlined approach |
25
|
|
|
* |
26
|
|
|
* @package Firesphere\SolrSearch\States |
27
|
|
|
*/ |
28
|
|
|
abstract class SiteState |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* States that can be applied |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
public static $states = [ |
37
|
|
|
'default', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Variants of SiteState that can be activated |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
public static $variants; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool Is this State enabled |
49
|
|
|
*/ |
50
|
|
|
public $enabled = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Does this state apply to the current object/environment settings |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function appliesToEnvironment(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->enabled; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get available states |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public static function getStates(): array |
68
|
|
|
{ |
69
|
|
|
return self::$states; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set states |
74
|
|
|
* |
75
|
|
|
* @param array $states |
76
|
|
|
*/ |
77
|
|
|
public static function setStates(array $states): void |
78
|
|
|
{ |
79
|
|
|
self::$states = $states; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add a state |
84
|
|
|
* |
85
|
|
|
* @param $state |
86
|
|
|
*/ |
87
|
|
|
public static function addState($state): void |
88
|
|
|
{ |
89
|
|
|
self::$states[] = $state; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add multiple states |
94
|
|
|
* |
95
|
|
|
* @param array $states |
96
|
|
|
*/ |
97
|
|
|
public static function addStates(array $states): void |
98
|
|
|
{ |
99
|
|
|
self::$states = array_merge(self::$states, $states); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Does this class, it's parent (or optionally one of it's children) have the passed extension attached? |
104
|
|
|
* |
105
|
|
|
* @param $class |
106
|
|
|
* @param $extension |
107
|
|
|
* @return bool |
108
|
|
|
* @throws \ReflectionException |
109
|
|
|
*/ |
110
|
|
|
public static function hasExtension($class, $extension): bool |
111
|
|
|
{ |
112
|
|
|
/** @var DataObject $relatedclass */ |
113
|
|
|
foreach (FieldResolver::gethierarchy($class) as $relatedclass) { |
114
|
|
|
if ($relatedclass::has_extension($extension)) { |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns an array of variants. |
124
|
|
|
* |
125
|
|
|
* With no arguments, returns all variants |
126
|
|
|
* |
127
|
|
|
* With a classname as the first argument, returns the variants that apply to that class |
128
|
|
|
* (optionally including subclasses) |
129
|
|
|
* |
130
|
|
|
* @static |
131
|
|
|
* @return array - An array of (string)$variantClassName => (Object)$variantInstance pairs |
132
|
|
|
* @throws \ReflectionException |
133
|
|
|
*/ |
134
|
|
|
public static function variants(): ?array |
135
|
|
|
{ |
136
|
|
|
// Build up and cache a list of all search variants (subclasses of SearchVariant) |
137
|
|
|
if (self::$variants === null) { |
|
|
|
|
138
|
|
|
$classes = ClassInfo::subclassesFor(static::class); |
139
|
|
|
|
140
|
|
|
$concrete = []; |
141
|
|
|
foreach ($classes as $variantclass) { |
142
|
|
|
$ref = new ReflectionClass($variantclass); |
143
|
|
|
if ($ref->isInstantiable()) { |
144
|
|
|
$variant = singleton($variantclass); |
145
|
|
|
if ($variant->appliesToEnvironment()) { |
146
|
|
|
$concrete[$variantclass] = $variant; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
self::$variants = $concrete; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return self::$variants; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the current state of every variant |
160
|
|
|
* |
161
|
|
|
* @static |
162
|
|
|
* @return array |
163
|
|
|
* @throws \ReflectionException |
164
|
|
|
*/ |
165
|
|
|
public static function currentStates(): array |
166
|
|
|
{ |
167
|
|
|
$state = []; |
168
|
|
|
foreach (self::variants() as $variant => $instance) { |
169
|
|
|
$state[$variant] = $instance->currentState(); |
170
|
|
|
} |
171
|
|
|
return $state; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Activate a site state for indexing |
176
|
|
|
* |
177
|
|
|
* @param $state |
178
|
|
|
* @throws \ReflectionException |
179
|
|
|
*/ |
180
|
|
|
public static function withState($state): void |
181
|
|
|
{ |
182
|
|
|
/** |
183
|
|
|
* @var string $variant |
184
|
|
|
* @var static $instance |
185
|
|
|
*/ |
186
|
|
|
foreach (self::variants() as $variant => $instance) { |
187
|
|
|
$instance->activateState($state); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|