1 | <?php |
||
14 | abstract class SearchVariant |
||
15 | { |
||
16 | use Configurable; |
||
17 | |||
18 | /** |
||
19 | * Whether this variant is enabled |
||
20 | * |
||
21 | * @config |
||
22 | * @var boolean |
||
23 | */ |
||
24 | private static $enabled = true; |
||
25 | |||
26 | public function __construct() |
||
27 | { |
||
28 | } |
||
29 | |||
30 | /*** OVERRIDES start here */ |
||
31 | |||
32 | /** |
||
33 | * Variants can provide any functions they want, but they _must_ override these functions |
||
34 | * with specific ones |
||
35 | */ |
||
36 | |||
37 | /** |
||
38 | * Return false if there is something missing from the environment (probably a |
||
39 | * not installed module) that means this variant can't apply to any class |
||
40 | */ |
||
41 | public function appliesToEnvironment() |
||
42 | { |
||
43 | return $this->config()->get('enabled'); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Return true if this variant applies to the passed class & subclass |
||
48 | */ |
||
49 | abstract public function appliesTo($class, $includeSubclasses); |
||
50 | |||
51 | /** |
||
52 | * Return the current state |
||
53 | */ |
||
54 | abstract public function currentState(); |
||
55 | /** |
||
56 | * Return all states to step through to reindex all items |
||
57 | */ |
||
58 | abstract public function reindexStates(); |
||
59 | /** |
||
60 | * Activate the passed state |
||
61 | */ |
||
62 | abstract public function activateState($state); |
||
63 | |||
64 | /** |
||
65 | * Apply this variant to a search query |
||
66 | * |
||
67 | * @param SearchQuery $query |
||
68 | * @param SearchIndex $index |
||
69 | */ |
||
70 | abstract public function alterQuery($query, $index); |
||
71 | |||
72 | /*** OVERRIDES end here*/ |
||
73 | |||
74 | /** Holds a cache of all variants */ |
||
75 | protected static $variants = null; |
||
76 | /** Holds a cache of the variants keyed by "class!" "1"? (1 = include subclasses) */ |
||
77 | protected static $class_variants = array(); |
||
78 | |||
79 | /** |
||
80 | * Returns an array of variants. |
||
81 | * |
||
82 | * With no arguments, returns all variants |
||
83 | * |
||
84 | * With a classname as the first argument, returns the variants that apply to that class |
||
85 | * (optionally including subclasses) |
||
86 | * |
||
87 | * @static |
||
88 | * @param string $class - The class name to get variants for |
||
89 | * @param bool $includeSubclasses - True if variants should be included if they apply to at least one subclass of $class |
||
90 | * @return array - An array of (string)$variantClassName => (Object)$variantInstance pairs |
||
91 | */ |
||
92 | public static function variants($class = null, $includeSubclasses = true) |
||
93 | { |
||
94 | if (!$class) { |
||
|
|||
95 | // Build up and cache a list of all search variants (subclasses of SearchVariant) |
||
96 | if (self::$variants === null) { |
||
97 | $classes = ClassInfo::subclassesFor(static::class); |
||
98 | |||
99 | $concrete = array(); |
||
100 | foreach ($classes as $variantclass) { |
||
101 | $ref = new ReflectionClass($variantclass); |
||
102 | if ($ref->isInstantiable()) { |
||
103 | $variant = singleton($variantclass); |
||
104 | if ($variant->appliesToEnvironment()) { |
||
105 | $concrete[$variantclass] = $variant; |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | self::$variants = $concrete; |
||
111 | } |
||
112 | |||
113 | return self::$variants; |
||
114 | } else { |
||
115 | $key = $class . '!' . $includeSubclasses; |
||
116 | |||
117 | if (!isset(self::$class_variants[$key])) { |
||
118 | self::$class_variants[$key] = array(); |
||
119 | |||
120 | foreach (self::variants() as $variantclass => $instance) { |
||
121 | if ($instance->appliesTo($class, $includeSubclasses)) { |
||
122 | self::$class_variants[$key][$variantclass] = $instance; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | return self::$class_variants[$key]; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Clear the cached variants |
||
133 | */ |
||
134 | public static function clear_variant_cache() |
||
138 | |||
139 | /** Holds a cache of SearchVariant_Caller instances, one for each class/includeSubclasses setting */ |
||
140 | protected static $call_instances = array(); |
||
141 | |||
142 | /** |
||
143 | * Lets you call any function on all variants that support it, in the same manner as "Object#extend" calls |
||
144 | * a method from extensions. |
||
145 | * |
||
146 | * Usage: SearchVariant::with(...)->call($method, $arg1, ...); |
||
147 | * |
||
148 | * @static |
||
149 | * |
||
150 | * @param string $class - (Optional) a classname. If passed, only variants that apply to that class will be checked / called |
||
151 | * |
||
152 | * @param bool $includeSubclasses - (Optional) If false, only variants that apply strictly to the passed class or its super-classes |
||
153 | * will be checked. If true (the default), variants that apply to any sub-class of the passed class with also be checked |
||
154 | * |
||
155 | * @return SearchVariant_Caller An object with one method, call() |
||
156 | */ |
||
157 | public static function with($class = null, $includeSubclasses = true) |
||
168 | |||
169 | /** |
||
170 | * Similar to {@link SearchVariant::with}, except will only use variants that apply to at least one of the classes |
||
171 | * in the input array, where {@link SearchVariant::with} will run the query on the specific class you give it. |
||
172 | * |
||
173 | * @param string[] $classes |
||
174 | * @return SearchVariant_Caller |
||
175 | */ |
||
176 | public static function withCommon(array $classes = []) |
||
204 | |||
205 | /** |
||
206 | * A shortcut to with when calling without passing in a class, |
||
207 | * |
||
208 | * SearchVariant::call(...) ==== SearchVariant::with()->call(...); |
||
209 | */ |
||
210 | |||
211 | public static function call($method, &...$args) |
||
215 | |||
216 | /** |
||
217 | * Get the current state of every variant |
||
218 | * @static |
||
219 | * @return array |
||
220 | */ |
||
221 | public static function current_state($class = null, $includeSubclasses = true) |
||
229 | |||
230 | /** |
||
231 | * Activate all the states in the passed argument |
||
232 | * @static |
||
233 | * @param array $state A set of (string)$variantClass => (any)$state pairs , e.g. as returned by |
||
234 | * SearchVariant::current_state() |
||
235 | * @return void |
||
236 | */ |
||
237 | public static function activate_state($state) |
||
245 | |||
246 | /** |
||
247 | * Return an iterator that, when used in a for loop, activates one combination of reindex states per loop, and restores |
||
248 | * back to the original state at the end |
||
249 | * @static |
||
250 | * @param string $class - The class name to get variants for |
||
251 | * @param bool $includeSubclasses - True if variants should be included if they apply to at least one subclass of $class |
||
252 | * @return SearchVariant_ReindexStateIteratorRet - The iterator to foreach loop over |
||
253 | */ |
||
254 | public static function reindex_states($class = null, $includeSubclasses = true) |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Add new filter field to index safely. |
||
270 | * |
||
271 | * This method will respect existing filters with the same field name that |
||
272 | * correspond to multiple classes |
||
273 | * |
||
274 | * @param SearchIndex $index |
||
275 | * @param string $name Field name |
||
276 | * @param array $field Field spec |
||
277 | */ |
||
278 | protected function addFilterField($index, $name, $field) |
||
294 | |||
295 | /** |
||
296 | * Merge sets of (or individual) class names together for a search index field. |
||
297 | * |
||
298 | * If there is only one unique class name, then just return it as a string instead of array. |
||
299 | * |
||
300 | * @param array|string $left Left class(es) |
||
301 | * @param array|string $right Right class(es) |
||
302 | * @return array|string List of classes, or single class |
||
303 | */ |
||
304 | protected function mergeClasses($left, $right) |
||
321 | } |
||
322 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: