Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit 29b6cf7

Browse files
committed
The SearchableModel class is now deprecated, use the Searchable trait instead.
1 parent f1ff1ee commit 29b6cf7

File tree

5 files changed

+86
-68
lines changed

5 files changed

+86
-68
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,19 @@ After executing the command you'll find the file `MyModel.php` in you `app` fold
155155

156156
namespace App;
157157

158-
use ScoutElastic\SearchableModel;
158+
use ScoutElastic\Searchable;
159+
use Illuminate\Database\Eloquent\Model;
159160

160-
class MyModel extends SearchableModel
161+
class MyModel extends Model
161162
{
163+
use Searchable;
164+
162165
protected $indexConfigurator = MyIndexConfigurator::class;
163166

167+
protected $searchRules = [
168+
//
169+
];
170+
164171
// Here you can specify a mapping for a model fields.
165172
protected $mapping = [
166173
'properties' => [
@@ -315,10 +322,13 @@ To determine default search rules for a model just add a property:
315322

316323
namespace App;
317324

318-
use ScoutElastic\SearchableModel;
325+
use ScoutElastic\Searchable;
326+
use Illuminate\Database\Eloquent\Model;
319327

320-
class MyModel extends SearchableModel
328+
class MyModel extends Model
321329
{
330+
use Searchable;
331+
322332
// You can set several rules for one model. In this case, the first not empty result will be returned.
323333
protected $searchRules = [
324334
MySearchRule::class

src/Console/SearchableModelMakeCommand.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,11 @@ protected function buildClass($name)
4343
{
4444
$stub = parent::buildClass($name);
4545

46-
4746
$indexConfigurator = $this->getIndexConfigurator();
48-
49-
if ($indexConfigurator) {
50-
$stub = str_replace('DummyIndexConfigurator', "protected \$indexConfigurator = {$indexConfigurator}::class;", $stub);
51-
} else {
52-
$stub = preg_replace('#DummyIndexConfigurator\s+#', '', $stub);
53-
}
54-
47+
$stub = str_replace('DummyIndexConfigurator', $indexConfigurator ? "{$indexConfigurator}::class" : 'null', $stub);
5548

5649
$searchRule = $this->getSearchRule();
57-
58-
if ($searchRule) {
59-
$stub = str_replace('DummySearchRules', "protected \$searchRules = [\n {$searchRule}::class\n ];", $stub);
60-
} else {
61-
$stub = preg_replace('#DummySearchRules\s+#', '', $stub);
62-
}
50+
$stub = str_replace('DummySearchRule', $searchRule ? "{$searchRule}::class" : '//', $stub);
6351

6452
return $stub;
6553
}

src/Console/stubs/searchable_model.stub

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
namespace DummyNamespace;
44

5-
use ScoutElastic\SearchableModel;
5+
use ScoutElastic\Searchable;
6+
use Illuminate\Database\Eloquent\Model;
67

7-
class DummyClass extends SearchableModel
8+
class DummyClass extends Model
89
{
9-
DummyIndexConfigurator
10+
use Searchable;
1011

11-
DummySearchRules
12+
protected $indexConfigurator = DummyIndexConfigurator;
13+
14+
protected $searchRules = [
15+
DummySearchRule
16+
];
1217

1318
protected $mapping = [
1419
//

src/Searchable.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace ScoutElastic;
4+
5+
use Laravel\Scout\Searchable as ScoutSearchable;
6+
use ScoutElastic\Builders\FilterBuilder;
7+
use ScoutElastic\Builders\SearchBuilder;
8+
use \Exception;
9+
10+
trait Searchable {
11+
use ScoutSearchable;
12+
13+
/**
14+
* @return IndexConfigurator
15+
* @throws Exception If an index configurator is not specified
16+
*/
17+
public function getIndexConfigurator()
18+
{
19+
static $indexConfigurator;
20+
21+
if (!$indexConfigurator) {
22+
if (!isset($this->indexConfigurator) || empty($this->indexConfigurator)) {
23+
throw new Exception(sprintf('An index configurator for the %s model is not specified.', __CLASS__));
24+
}
25+
26+
$indexConfiguratorClass = $this->indexConfigurator;
27+
$indexConfigurator = new $indexConfiguratorClass;
28+
}
29+
30+
return $indexConfigurator;
31+
}
32+
33+
public function getMapping()
34+
{
35+
return isset($this->mapping) ? $this->mapping : [];
36+
}
37+
38+
public function getSearchRules()
39+
{
40+
return isset($this->searchRules) && count($this->searchRules) > 0 ? $this->searchRules : [SearchRule::class];
41+
}
42+
43+
public static function search($query, $callback = null)
44+
{
45+
if ($query == '*') {
46+
return new FilterBuilder(new static, $callback);
47+
} else {
48+
return new SearchBuilder(new static, $query, $callback);
49+
}
50+
}
51+
52+
public static function searchRaw($query)
53+
{
54+
$model = new static();
55+
56+
return $model->searchableUsing()
57+
->searchRaw($model, $query);
58+
}
59+
}

src/SearchableModel.php

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
namespace ScoutElastic;
44

5-
use Laravel\Scout\Searchable;
65
use Illuminate\Database\Eloquent\Model;
7-
use ScoutElastic\Builders\FilterBuilder;
8-
use ScoutElastic\Builders\SearchBuilder;
96

7+
/** @deprecated Use the \ScoutElastic\Searchable trait instead. */
108
abstract class SearchableModel extends Model
119
{
12-
use Searchable;
13-
1410
protected $indexConfigurator;
1511

1612
protected $mapping = [];
@@ -19,45 +15,5 @@ abstract class SearchableModel extends Model
1915
SearchRule::class
2016
];
2117

22-
/**
23-
* @return IndexConfigurator
24-
*/
25-
public function getIndexConfigurator()
26-
{
27-
static $indexConfigurator;
28-
29-
if (!$indexConfigurator) {
30-
$indexConfiguratorClass = $this->indexConfigurator;
31-
$indexConfigurator = new $indexConfiguratorClass;
32-
}
33-
34-
return $indexConfigurator;
35-
}
36-
37-
public function getMapping()
38-
{
39-
return $this->mapping;
40-
}
41-
42-
public function getSearchRules()
43-
{
44-
return $this->searchRules;
45-
}
46-
47-
public static function search($query, $callback = null)
48-
{
49-
if ($query == '*') {
50-
return new FilterBuilder(new static, $callback);
51-
} else {
52-
return new SearchBuilder(new static, $query, $callback);
53-
}
54-
}
55-
56-
public static function searchRaw($query)
57-
{
58-
$model = new static();
59-
60-
return $model->searchableUsing()
61-
->searchRaw($model, $query);
62-
}
18+
use Searchable;
6319
}

0 commit comments

Comments
 (0)