-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
SimpleFlarumSearch.php
105 lines (88 loc) · 3.08 KB
/
SimpleFlarumSearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
class SimpleFlarumSearch implements ExtenderInterface
{
private $fullTextGambit;
private $gambits = [];
private $searcher;
private $searchMutators = [];
/**
* @param string $searcherClass: The ::class attribute of the Searcher you are modifying.
* This searcher must extend \Flarum\Search\AbstractSearcher.
*/
public function __construct(string $searcherClass)
{
$this->searcher = $searcherClass;
}
/**
* Add a gambit to this searcher. Gambits are used to filter search queries.
*
* @param string $gambitClass: The ::class attribute of the gambit you are adding.
* This gambit must extend \Flarum\Search\AbstractRegexGambit
* @return self
*/
public function addGambit(string $gambitClass): self
{
$this->gambits[] = $gambitClass;
return $this;
}
/**
* Set the full text gambit for this searcher. The full text gambit actually executes the search.
*
* @param string $gambitClass: The ::class attribute of the full test gambit you are adding.
* This gambit must implement \Flarum\Search\GambitInterface
* @return self
*/
public function setFullTextGambit(string $gambitClass): self
{
$this->fullTextGambit = $gambitClass;
return $this;
}
/**
* Add a callback through which to run all search queries after gambits have been applied.
*
* @param callable|string $callback
*
* The callback can be a closure or an invokable class, and should accept:
* - \Flarum\Search\SearchState $search
* - \Flarum\Query\QueryCriteria $criteria
*
* The callback should return void.
*
* @return self
*/
public function addSearchMutator($callback): self
{
$this->searchMutators[] = $callback;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
if (! is_null($this->fullTextGambit)) {
$container->extend('flarum.simple_search.fulltext_gambits', function ($oldFulltextGambits) {
$oldFulltextGambits[$this->searcher] = $this->fullTextGambit;
return $oldFulltextGambits;
});
}
$container->extend('flarum.simple_search.gambits', function ($oldGambits) {
foreach ($this->gambits as $gambit) {
$oldGambits[$this->searcher][] = $gambit;
}
return $oldGambits;
});
$container->extend('flarum.simple_search.search_mutators', function ($oldMutators) {
foreach ($this->searchMutators as $mutator) {
$oldMutators[$this->searcher][] = $mutator;
}
return $oldMutators;
});
}
}