|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Omatech\Enigma\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use HaydenPierce\ClassFinder\ClassFinder; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Omatech\Enigma\Database\Eloquent\HasEnigma; |
|
9
|
|
|
use Omatech\Enigma\Enigma; |
|
10
|
|
|
use Omatech\Enigma\Exceptions\InvalidClassException; |
|
11
|
|
|
use Omatech\Enigma\Jobs\IndexHydrate; |
|
12
|
|
|
|
|
13
|
|
|
class IndexHydrateCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
private $enigma; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The name and signature of the console command. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $signature = 'enigma:hydrate { namespace : Fully qualified namespace }'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Rehydrate the indexes given a namespace'; |
|
30
|
|
|
|
|
31
|
63 |
|
public function __construct(Enigma $enigma) |
|
32
|
|
|
{ |
|
33
|
63 |
|
parent::__construct(); |
|
34
|
63 |
|
$this->enigma = $enigma; |
|
35
|
63 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Execute the console command. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Enigma $enigma |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
* @throws InvalidClassException |
|
43
|
|
|
*/ |
|
44
|
8 |
|
public function handle() |
|
45
|
|
|
{ |
|
46
|
8 |
|
$namespace = (string) $this->argument('namespace'); |
|
47
|
8 |
|
$foundClasses = ClassFinder::getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE); |
|
48
|
|
|
|
|
49
|
8 |
|
$classes = []; |
|
50
|
8 |
|
foreach ($foundClasses as $class) { |
|
51
|
8 |
|
if (isset(class_uses($class)[HasEnigma::class]) === true) { |
|
52
|
8 |
|
$classes[] = $class; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
$choice = (array) $this->choice( |
|
57
|
8 |
|
'Which models would you like to hydrate?', |
|
58
|
8 |
|
array_merge([0 => 'All'], $classes), |
|
59
|
8 |
|
0, |
|
60
|
8 |
|
null, |
|
61
|
8 |
|
true |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
8 |
|
if (array_search('All', $choice) !== false) { |
|
65
|
4 |
|
$choice = $classes; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
8 |
|
foreach ($choice as $class) { |
|
69
|
8 |
|
$this->hydrate(new $class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
$this->info('The hydration indexes has been enqueued / completed.'); |
|
73
|
4 |
|
} |
|
74
|
|
|
|
|
75
|
8 |
|
private function hydrate(Model $model) |
|
76
|
|
|
{ |
|
77
|
8 |
|
$this->info(get_class($model)."\n"); |
|
78
|
8 |
|
$bar = $this->output->createProgressBar((int) $model::count()); |
|
79
|
8 |
|
$bar->start(); |
|
80
|
|
|
|
|
81
|
|
|
$model::chunk(100, function ($rows) use ($model, $bar) { |
|
82
|
8 |
|
foreach ($rows as $row) { |
|
83
|
8 |
|
dispatch(new IndexHydrate(get_class($model), $row->id)) |
|
84
|
8 |
|
->onQueue('enigma'); |
|
85
|
8 |
|
$bar->advance(); |
|
86
|
|
|
} |
|
87
|
8 |
|
}); |
|
88
|
8 |
|
$bar->finish(); |
|
89
|
8 |
|
$this->info("\n"); |
|
90
|
8 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|