-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
SetupScout.php
92 lines (80 loc) · 2.59 KB
/
SetupScout.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
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @codeCoverageIgnore
*/
class SetupScout extends Command
{
use ConfirmableTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:setup
{--flush : Flush the indexes.}
{--import : Import the models into the search index.}
{--force : Force the operation to run when in production.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup scout indexes.';
/**
* Execute the console command.
*/
public function handle(): void
{
if ($this->confirmToProceed()) {
$this->scoutConfigure();
$this->scoutFlush();
$this->scoutImport();
}
}
/**
* Configure scout.
*/
protected function scoutConfigure(): void
{
if (config('scout.driver') === 'meilisearch' && config('scout.meilisearch.host') !== '') {
$this->artisan('☐ Updating indexes on Meilisearch', 'scout:sync-index-settings', ['--verbose' => true]);
}
}
/**
* Import models.
*/
protected function scoutFlush(): void
{
if (config('scout.driver') !== null && $this->option('flush')) {
foreach (config('scout.meilisearch.index-settings') as $index => $settings) {
$name = (new $index)->getTable();
$this->artisan("☐ Flush {$name} index", 'scout:flush', ['model' => $index, '--verbose' => true]);
}
$this->info('✓ Indexes flushed');
}
}
/**
* Import models.
*/
protected function scoutImport(): void
{
if (config('scout.driver') !== null && $this->option('import')) {
foreach (config('scout.meilisearch.index-settings') as $index => $settings) {
$name = (new $index)->getTable();
$this->artisan("☐ Import {$name}", 'scout:import', ['model' => $index, '--verbose' => true]);
}
$this->info('✓ Indexes imported');
}
}
private function artisan(string $message, string $command, array $options = [])
{
$this->info($message);
$this->getOutput()->getOutput()->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE
? $this->call($command, $options)
: $this->callSilent($command, $options);
}
}