mirror of
https://github.com/flarum/core.git
synced 2025-10-18 18:26:07 +02:00
Simple Flarum Search Extender and tests (#2483)
This commit is contained in:
committed by
GitHub
parent
5df22e92ae
commit
76d6442557
@@ -12,6 +12,9 @@ namespace Flarum\Discussion\Event;
|
||||
use Flarum\Discussion\Search\DiscussionSearch;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
|
||||
/**
|
||||
* @deprecated beta 16, remove beta 17
|
||||
*/
|
||||
class Searching
|
||||
{
|
||||
/**
|
||||
|
@@ -11,21 +11,16 @@ namespace Flarum\Discussion\Search;
|
||||
|
||||
use Flarum\Discussion\DiscussionRepository;
|
||||
use Flarum\Discussion\Event\Searching;
|
||||
use Flarum\Search\ApplySearchParametersTrait;
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\Search\AbstractSearcher;
|
||||
use Flarum\Search\GambitManager;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\Search\SearchResults;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class DiscussionSearcher
|
||||
class DiscussionSearcher extends AbstractSearcher
|
||||
{
|
||||
use ApplySearchParametersTrait;
|
||||
|
||||
/**
|
||||
* @var GambitManager
|
||||
*/
|
||||
protected $gambits;
|
||||
|
||||
/**
|
||||
* @var DiscussionRepository
|
||||
*/
|
||||
@@ -37,53 +32,36 @@ class DiscussionSearcher
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @param GambitManager $gambits
|
||||
* @param DiscussionRepository $discussions
|
||||
* @param Dispatcher $events
|
||||
* @param GambitManager $gambits
|
||||
* @param array $searchMutators
|
||||
*/
|
||||
public function __construct(GambitManager $gambits, DiscussionRepository $discussions, Dispatcher $events)
|
||||
public function __construct(DiscussionRepository $discussions, Dispatcher $events, GambitManager $gambits, array $searchMutators)
|
||||
{
|
||||
$this->gambits = $gambits;
|
||||
parent::__construct($gambits, $searchMutators);
|
||||
|
||||
$this->discussions = $discussions;
|
||||
$this->events = $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchCriteria $criteria
|
||||
* @param int|null $limit
|
||||
* @param int $offset
|
||||
*
|
||||
* @return SearchResults
|
||||
*/
|
||||
public function search(SearchCriteria $criteria, $limit = null, $offset = 0)
|
||||
protected function getQuery(User $actor): Builder
|
||||
{
|
||||
$actor = $criteria->actor;
|
||||
return $this->discussions->query()->select('discussions.*')->whereVisibleTo($actor);
|
||||
}
|
||||
|
||||
$query = $this->discussions->query()->select('discussions.*')->whereVisibleTo($actor);
|
||||
protected function getSearch(Builder $query, User $actor): AbstractSearch
|
||||
{
|
||||
return new DiscussionSearch($query->getQuery(), $actor);
|
||||
}
|
||||
|
||||
// Construct an object which represents this search for discussions.
|
||||
// Apply gambits to it, sort, and paging criteria. Also give extensions
|
||||
// an opportunity to modify it.
|
||||
$search = new DiscussionSearch($query->getQuery(), $actor);
|
||||
|
||||
$this->gambits->apply($search, $criteria->query);
|
||||
$this->applySort($search, $criteria->sort);
|
||||
$this->applyOffset($search, $offset);
|
||||
$this->applyLimit($search, $limit + 1);
|
||||
/**
|
||||
* @deprecated along with the Searching event, remove in Beta 17.
|
||||
*/
|
||||
protected function mutateSearch(AbstractSearch $search, SearchCriteria $criteria)
|
||||
{
|
||||
parent::mutateSearch($search, $criteria);
|
||||
|
||||
$this->events->dispatch(new Searching($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.
|
||||
$discussions = $query->get();
|
||||
|
||||
$areMoreResults = $limit > 0 && $discussions->count() > $limit;
|
||||
|
||||
if ($areMoreResults) {
|
||||
$discussions->pop();
|
||||
}
|
||||
|
||||
return new SearchResults($discussions, $areMoreResults);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user