2021-02-10 09:59:23 -05:00
|
|
|
<?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.
|
|
|
|
*/
|
2021-05-13 15:26:24 +01:00
|
|
|
public function __construct(string $searcherClass)
|
2021-02-10 09:59:23 -05:00
|
|
|
{
|
|
|
|
$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
|
2021-05-13 15:26:24 +01:00
|
|
|
* @return self
|
2021-02-10 09:59:23 -05:00
|
|
|
*/
|
2021-05-13 15:26:24 +01:00
|
|
|
public function addGambit(string $gambitClass): self
|
2021-02-10 09:59:23 -05:00
|
|
|
{
|
|
|
|
$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
|
2021-05-13 15:26:24 +01:00
|
|
|
* @return self
|
2021-02-10 09:59:23 -05:00
|
|
|
*/
|
2021-05-13 15:26:24 +01:00
|
|
|
public function setFullTextGambit(string $gambitClass): self
|
2021-02-10 09:59:23 -05:00
|
|
|
{
|
|
|
|
$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:
|
2021-05-13 15:26:24 +01:00
|
|
|
* - \Flarum\Search\SearchState $search
|
|
|
|
* - \Flarum\Query\QueryCriteria $criteria
|
|
|
|
*
|
|
|
|
* The callback should return void.
|
|
|
|
*
|
|
|
|
* @return self
|
2021-02-10 09:59:23 -05:00
|
|
|
*/
|
2021-05-13 15:26:24 +01:00
|
|
|
public function addSearchMutator($callback): self
|
2021-02-10 09:59:23 -05:00
|
|
|
{
|
|
|
|
$this->searchMutators[] = $callback;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function extend(Container $container, Extension $extension = null)
|
|
|
|
{
|
|
|
|
if (! is_null($this->fullTextGambit)) {
|
2021-04-19 16:59:53 -04:00
|
|
|
$container->extend('flarum.simple_search.fulltext_gambits', function ($oldFulltextGambits) {
|
2021-02-10 09:59:23 -05:00
|
|
|
$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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|