1
0
mirror of https://github.com/flarum/core.git synced 2025-08-06 08:27:42 +02:00

Fix registering custom searchers, allow searchers without fulltext (#2755)

This commit is contained in:
Alexander Skvortsov
2021-04-19 16:59:53 -04:00
committed by GitHub
parent 4fea0ebdee
commit 42a9de5a11
4 changed files with 62 additions and 22 deletions

View File

@@ -73,7 +73,7 @@ class SimpleFlarumSearch implements ExtenderInterface
public function extend(Container $container, Extension $extension = null)
{
if (! is_null($this->fullTextGambit)) {
$container->resolving('flarum.simple_search.fulltext_gambits', function ($oldFulltextGambits) {
$container->extend('flarum.simple_search.fulltext_gambits', function ($oldFulltextGambits) {
$oldFulltextGambits[$this->searcher] = $this->fullTextGambit;
return $oldFulltextGambits;

View File

@@ -12,7 +12,7 @@ namespace Flarum\Search;
use LogicException;
/**
* @todo This whole gambits thing needs way better documentation.
* @internal
*/
class GambitManager
{
@@ -26,12 +26,20 @@ class GambitManager
*/
protected $fulltextGambit;
/**
* @param GambitInterface $gambit
*/
public function __construct(GambitInterface $fulltextGambit)
{
$this->fulltextGambit = $fulltextGambit;
}
/**
* Add a gambit.
*
* @param GambitInterface $gambit
*/
public function add($gambit)
public function add(GambitInterface $gambit)
{
$this->gambits[] = $gambit;
}
@@ -51,16 +59,6 @@ class GambitManager
}
}
/**
* Set the gambit to handle fulltext searching.
*
* @param GambitInterface $gambit
*/
public function setFulltextGambit($gambit)
{
$this->fulltextGambit = $gambit;
}
/**
* Explode a search query into an array of bits.
*
@@ -110,10 +108,6 @@ class GambitManager
*/
protected function applyFulltext(SearchState $search, $query)
{
if (! $this->fulltextGambit) {
return;
}
$search->addActiveGambit($this->fulltextGambit);
$this->fulltextGambit->apply($search, $query);
}

View File

@@ -58,9 +58,6 @@ class SearchServiceProvider extends AbstractServiceProvider
*/
public function boot()
{
// The rest of these we can resolve in the when->needs->give callback,
// but we need to resolve at least one regardless so we know which
// searchers we need to register gambits for.
$fullTextGambits = $this->container->make('flarum.simple_search.fulltext_gambits');
foreach ($fullTextGambits as $searcher => $fullTextGambitClass) {
@@ -68,8 +65,7 @@ class SearchServiceProvider extends AbstractServiceProvider
->when($searcher)
->needs(GambitManager::class)
->give(function () use ($searcher, $fullTextGambitClass) {
$gambitManager = new GambitManager();
$gambitManager->setFulltextGambit($this->container->make($fullTextGambitClass));
$gambitManager = new GambitManager($this->container->make($fullTextGambitClass));
foreach (Arr::get($this->container->make('flarum.simple_search.gambits'), $searcher, []) as $gambit) {
$gambitManager->add($this->container->make($gambit));
}