Skip to content

Commit

Permalink
[9.x] Add a DatabaseEngine (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Jan 8, 2022
1 parent 0716725 commit 0ff8f08
Show file tree
Hide file tree
Showing 8 changed files with 599 additions and 15 deletions.
28 changes: 28 additions & 0 deletions src/Attributes/SearchUsingFullText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Scout\Attributes;

use Attribute;
use Illuminate\Support\Arr;

#[Attribute]
class SearchUsingFullText
{
/**
* The full-text columns.
*
* @var array
*/
public $columns = [];

/**
* Create a new attribute instance.
*
* @param array|string $columns
* @return void
*/
public function __construct($columns)
{
$this->columns = Arr::wrap($columns);
}
}
28 changes: 28 additions & 0 deletions src/Attributes/SearchUsingPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Scout\Attributes;

use Attribute;
use Illuminate\Support\Arr;

#[Attribute]
class SearchUsingPrefix
{
/**
* The prefix search columns.
*
* @var array
*/
public $columns = [];

/**
* Create a new attribute instance.
*
* @param array|string $columns
* @return void
*/
public function __construct($columns)
{
$this->columns = Arr::wrap($columns);
}
}
37 changes: 22 additions & 15 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Laravel\Scout\Contracts\PaginatesEloquentModels;

class Builder
{
Expand Down Expand Up @@ -295,6 +296,10 @@ public function simplePaginate($perPage = null, $pageName = 'page', $page = null
{
$engine = $this->engine();

if ($engine instanceof PaginatesEloquentModels) {
return $engine->simplePaginate($this, $perPage, $page)->appends('query', $this->query);
}

$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();
Expand All @@ -303,8 +308,6 @@ public function simplePaginate($perPage = null, $pageName = 'page', $page = null
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
)->all());

$hasMorePages = ($perPage * $page) < $engine->getTotalCount($rawResults);

$paginator = Container::getInstance()->makeWith(Paginator::class, [
'items' => $results,
'perPage' => $perPage,
Expand All @@ -313,7 +316,7 @@ public function simplePaginate($perPage = null, $pageName = 'page', $page = null
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
],
])->hasMorePagesWhen($hasMorePages);
])->hasMorePagesWhen(($perPage * $page) < $engine->getTotalCount($rawResults));

return $paginator->appends('query', $this->query);
}
Expand All @@ -330,16 +333,16 @@ public function simplePaginateRaw($perPage = null, $pageName = 'page', $page = n
{
$engine = $this->engine();

if ($engine instanceof PaginatesEloquentModels) {
return $engine->simplePaginate($this, $perPage, $page)->appends('query', $this->query);
}

$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();

$results = $engine->paginate($this, $perPage, $page);

$total = $engine->getTotalCount($rawResults);

$hasMorePages = ($perPage * $page) < $engine->getTotalCount($rawResults);

$paginator = Container::getInstance()->makeWith(Paginator::class, [
'items' => $results,
'perPage' => $perPage,
Expand All @@ -348,7 +351,7 @@ public function simplePaginateRaw($perPage = null, $pageName = 'page', $page = n
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
],
])->hasMorePagesWhen($hasMorePages);
])->hasMorePagesWhen(($perPage * $page) < $engine->getTotalCount($results));

return $paginator->appends('query', $this->query);
}
Expand All @@ -365,6 +368,10 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
{
$engine = $this->engine();

if ($engine instanceof PaginatesEloquentModels) {
return $engine->paginate($this, $perPage, $page)->appends('query', $this->query);
}

$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();
Expand All @@ -373,7 +380,7 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
)->all());

$paginator = Container::getInstance()->makeWith(LengthAwarePaginator::class, [
return Container::getInstance()->makeWith(LengthAwarePaginator::class, [
'items' => $results,
'total' => $this->getTotalCount($rawResults),
'perPage' => $perPage,
Expand All @@ -382,9 +389,7 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
],
]);

return $paginator->appends('query', $this->query);
])->appends('query', $this->query);
}

/**
Expand All @@ -399,6 +404,10 @@ public function paginateRaw($perPage = null, $pageName = 'page', $page = null)
{
$engine = $this->engine();

if ($engine instanceof PaginatesEloquentModels) {
return $engine->paginate($this, $perPage, $page)->appends('query', $this->query);
}

$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();
Expand All @@ -414,9 +423,7 @@ public function paginateRaw($perPage = null, $pageName = 'page', $page = null)
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
],
]);

return $paginator->appends('query', $this->query);
])->appends('query', $this->query);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/Contracts/PaginatesEloquentModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Laravel\Scout\Contracts;

use Laravel\Scout\Builder;

interface PaginatesEloquentModels
{
/**
* Paginate the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param int $perPage
* @param int $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginate(Builder $builder, $perPage, $page);

/**
* Paginate the given search on the engine using simple pagination.
*
* @param \Laravel\Scout\Builder $builder
* @param int $perPage
* @param int $page
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginate(Builder $builder, $perPage, $page);
}
11 changes: 11 additions & 0 deletions src/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Manager;
use Laravel\Scout\Engines\AlgoliaEngine;
use Laravel\Scout\Engines\CollectionEngine;
use Laravel\Scout\Engines\DatabaseEngine;
use Laravel\Scout\Engines\MeiliSearchEngine;
use Laravel\Scout\Engines\NullEngine;
use MeiliSearch\Client as MeiliSearch;
Expand Down Expand Up @@ -136,6 +137,16 @@ protected function ensureMeiliSearchClientIsInstalled()
throw new Exception('Please install the MeiliSearch client: meilisearch/meilisearch-php.');
}

/**
* Create a database engine instance.
*
* @return \Laravel\Scout\Engines\DatabaseEngine
*/
public function createDatabaseDriver()
{
return new DatabaseEngine;
}

/**
* Create a collection engine instance.
*
Expand Down
Loading

0 comments on commit 0ff8f08

Please sign in to comment.