1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Matchish\ScoutElasticSearch\Console\Commands; |
6
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Matchish\ScoutElasticSearch\Jobs\Import; |
9
|
|
|
use Matchish\ScoutElasticSearch\Jobs\QueueableJob; |
10
|
|
|
use Matchish\ScoutElasticSearch\Searchable\SearchableListFactory; |
11
|
|
|
|
12
|
|
|
final class ImportCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'scout:import {searchable?* : The name of the searchable}'; |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Create new index and import all searchable into the one'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
9 |
|
public function handle(): void |
27
|
|
|
{ |
28
|
9 |
|
$this->searchableList($this->argument('searchable')) |
29
|
|
|
->each(function ($searchable) { |
30
|
9 |
|
$this->import($searchable); |
31
|
9 |
|
}); |
32
|
9 |
|
} |
33
|
|
|
|
34
|
9 |
|
private function searchableList($argument) |
35
|
|
|
{ |
36
|
|
|
return collect($argument)->whenEmpty(function () { |
37
|
8 |
|
$factory = new SearchableListFactory(app()->getNamespace(), app()->path()); |
|
|
|
|
38
|
|
|
|
39
|
8 |
|
return $factory->make(); |
40
|
9 |
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
9 |
|
private function import($searchable) |
44
|
|
|
{ |
45
|
9 |
|
$job = new Import($searchable); |
46
|
|
|
|
47
|
9 |
|
if (config('scout.queue')) { |
48
|
2 |
|
$job = (new QueueableJob())->chain([$job]); |
49
|
|
|
} |
50
|
|
|
|
51
|
9 |
|
$bar = (new ProgressBarFactory($this->output))->create(); |
52
|
9 |
|
$job->withProgressReport($bar); |
53
|
|
|
|
54
|
9 |
|
$startMessage = trans('scout::import.start', ['searchable' => "<comment>$searchable</comment>"]); |
55
|
9 |
|
$this->line($startMessage); |
|
|
|
|
56
|
|
|
|
57
|
9 |
|
dispatch($job)->allOnQueue((new $searchable)->syncWithSearchUsingQueue()) |
58
|
9 |
|
->allOnConnection(config((new $searchable)->syncWithSearchUsing())); |
59
|
|
|
|
60
|
9 |
|
$doneMessage = trans(config('scout.queue') ? 'scout::import.done.queue' : 'scout::import.done', [ |
61
|
9 |
|
'searchable' => $searchable, |
62
|
|
|
]); |
63
|
9 |
|
$this->output->success($doneMessage); |
64
|
9 |
|
} |
65
|
|
|
} |
66
|
|
|
|