-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathAggregator.php
284 lines (245 loc) · 6.77 KB
/
Aggregator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
declare(strict_types=1);
/**
* This file is part of Scout Extended.
*
* (c) Algolia Team <contact@algolia.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Algolia\ScoutExtended\Searchable;
use Algolia\ScoutExtended\Contracts\SearchableCountableContract;
use Algolia\ScoutExtended\Exceptions\ModelNotDefinedInAggregatorException;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use function in_array;
use Laravel\Scout\Events\ModelsImported;
use Laravel\Scout\Searchable;
abstract class Aggregator implements SearchableCountableContract
{
use Searchable;
/**
* The names of the models that should be aggregated.
*
* @var string[]
*/
protected $models = [];
/**
* The model being queried, if any.
*
* @var \Illuminate\Database\Eloquent\Model|null
*/
protected $model;
/**
* The relationships per model that should be loaded.
*
* @var mixed[]
*/
protected $relations = [];
/**
* Returns the index name.
*
* @var string
*/
protected $indexName;
/**
* Boot the aggregator.
*
* @return void
*/
public static function bootSearchable(): void
{
($self = new static)->registerSearchableMacros();
$observer = tap(app(AggregatorObserver::class))->setAggregator(static::class, $models = (new static)->getModels());
foreach ($models as $model) {
$model::observe($observer);
}
}
/**
* Creates an instance of the aggregator.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return \Algolia\ScoutExtended\Searchable\Aggregator
*/
public static function create(Model $model): Aggregator
{
return (new static)->setModel($model);
}
/**
* Get the names of the models that should be aggregated.
*
* @return string[]
*/
public function getModels(): array
{
return $this->models;
}
/**
* Get the model instance being queried.
*
* @return \Illuminate\Database\Eloquent\Model
*
* @throws \Algolia\ScoutExtended\Exceptions\ModelNotDefinedInAggregatorException
*/
public function getModel(): Model
{
if ($this->model === null) {
throw new ModelNotDefinedInAggregatorException();
}
return $this->model;
}
/**
* Set a model instance for the model being queried.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return \Algolia\ScoutExtended\Searchable\Aggregator
*/
public function setModel(Model $model): Aggregator
{
$this->model = $model;
return $this;
}
/**
* Get the relations to load.
*
* @param string $modelClass
*
* @return array
*/
public function getRelations($modelClass): array
{
return $this->relations[$modelClass] ?? [];
}
/**
* Get the value used to index the model.
*
* @return mixed
*/
public function getScoutKey()
{
if ($this->model === null) {
throw new ModelNotDefinedInAggregatorException();
}
return method_exists($this->model, 'getScoutKey') ? $this->model->getScoutKey() : $this->model->getKey();
}
/**
* Get the index name for the searchable.
*
* @return string
*/
public function searchableAs(): string
{
return config('scout.prefix').str_replace('\\', '', Str::snake(class_basename(static::class)));
}
/**
* Get the searchable array of the searchable.
*
* @return array
*/
public function toSearchableArray(): array
{
if ($this->model === null) {
throw new ModelNotDefinedInAggregatorException();
}
return method_exists($this->model, 'toSearchableArray') ? $this->model->toSearchableArray() :
$this->model->toArray();
}
/**
* Make all instances of the model searchable.
*
* @return void
*/
public static function makeAllSearchable()
{
foreach ((new static)->getModels() as $model) {
$instance = new $model;
$softDeletes =
in_array(SoftDeletes::class, class_uses_recursive($model)) && config('scout.soft_delete', false);
$instance->newQuery()->when($softDeletes, function ($query) {
$query->withTrashed();
})->orderBy($instance->getKeyName())->chunk(config('scout.chunk.searchable', 500), function ($models) {
$models = $models->map(function ($model) {
return static::create($model);
})->filter->shouldBeSearchable();
$models->searchable();
event(new ModelsImported($models));
});
}
}
/**
* {@inheritdoc}
*
* @internal
*/
public function searchable(): void
{
AggregatorCollection::make([$this])->searchable();
}
/**
* {@inheritdoc}
*
* @internal
*/
public function unsearchable(): void
{
AggregatorCollection::make([$this])->unsearchable();
}
/**
* {@inheritdoc}
*/
public function getSearchableCount(): int
{
$count = 0;
foreach ($this->getModels() as $model) {
$softDeletes =
in_array(SoftDeletes::class, class_uses_recursive($model), true) && config('scout.soft_delete', false);
$count += $model::query()->when($softDeletes, function ($query) {
$query->withTrashed();
})->count();
}
return (int) $count;
}
/**
* Create a new Eloquent Collection instance.
*
* @param array $searchables
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function newCollection(array $searchables = []): Collection
{
return new Collection($searchables);
}
/**
* Dispatch the job to make the given models unsearchable.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
public function queueRemoveFromSearch($models)
{
if ($models->isEmpty()) {
return;
}
$models->first()->searchableUsing()->delete($models);
}
/**
* Handle dynamic method calls into the model.
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
$model = $this->model ?? new class extends Model {
};
return $model->$method(...$parameters);
}
}