-
-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple Flarum Search Extender and tests (#2483)
- Loading branch information
1 parent
58d7070
commit 34149b2
Showing
12 changed files
with
524 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?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($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 | ||
*/ | ||
public function addGambit($gambitClass) | ||
{ | ||
$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 | ||
*/ | ||
public function setFullTextGambit($gambitClass) | ||
{ | ||
$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\AbstractSearch $search | ||
* - Flarum\Search\SearchCriteria $criteria | ||
*/ | ||
public function addSearchMutator($callback) | ||
{ | ||
$this->searchMutators[] = $callback; | ||
|
||
return $this; | ||
} | ||
|
||
public function extend(Container $container, Extension $extension = null) | ||
{ | ||
if (! is_null($this->fullTextGambit)) { | ||
$container->resolving('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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?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\Search; | ||
|
||
use Flarum\User\User; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
abstract class AbstractSearcher | ||
{ | ||
use ApplySearchParametersTrait; | ||
|
||
/** | ||
* @var GambitManager | ||
*/ | ||
protected $gambits; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $searchMutators; | ||
|
||
public function __construct(GambitManager $gambits, array $searchMutators) | ||
{ | ||
$this->gambits = $gambits; | ||
$this->searchMutators = $searchMutators; | ||
} | ||
|
||
abstract protected function getQuery(User $actor): Builder; | ||
|
||
abstract protected function getSearch(Builder $query, User $actor): AbstractSearch; | ||
|
||
protected function mutateSearch(AbstractSearch $search, SearchCriteria $criteria) | ||
{ | ||
foreach ($this->searchMutators as $mutator) { | ||
$mutator($search, $criteria); | ||
} | ||
} | ||
|
||
/** | ||
* @param SearchCriteria $criteria | ||
* @param int|null $limit | ||
* @param int $offset | ||
* | ||
* @return SearchResults | ||
*/ | ||
public function search(SearchCriteria $criteria, $limit = null, $offset = 0, array $load = []) | ||
{ | ||
$actor = $criteria->actor; | ||
|
||
$query = $this->getQuery($actor); | ||
|
||
$search = $this->getSearch($query, $actor); | ||
|
||
$this->gambits->apply($search, $criteria->query); | ||
$this->applySort($search, $criteria->sort); | ||
$this->applyOffset($search, $offset); | ||
$this->applyLimit($search, $limit + 1); | ||
|
||
$this->mutateSearch($search, $criteria); | ||
|
||
// Execute the search query and retrieve the results. We get one more | ||
// results than the user asked for, so that we can say if there are more | ||
// results. If there are, we will get rid of that extra result. | ||
$results = $query->get(); | ||
|
||
if ($areMoreResults = $limit > 0 && $results->count() > $limit) { | ||
$results->pop(); | ||
} | ||
|
||
$results->load($load); | ||
|
||
return new SearchResults($results, $areMoreResults); | ||
} | ||
} |
Oops, something went wrong.