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