|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Laravel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
7
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RebuildModels |
|
12
|
|
|
* @package BestServedCold\LaravelZendSearch\Laravel |
|
13
|
|
|
*/ |
|
14
|
|
|
class RebuildModels |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ProgressBar $progressBar |
|
18
|
|
|
*/ |
|
19
|
|
|
private $progressBar; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Store $store |
|
23
|
|
|
*/ |
|
24
|
|
|
private $store; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var OutputInterface $store |
|
28
|
|
|
*/ |
|
29
|
|
|
private $output; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array $models |
|
33
|
|
|
*/ |
|
34
|
|
|
private $models = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* RebuildModels constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param ProgressBar $progressBar |
|
40
|
|
|
* @param Store $store |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function __construct(ProgressBar $progressBar, Store $store, OutputInterface $output) |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->progressBar = $progressBar; |
|
46
|
5 |
|
$this->store = $store; |
|
47
|
5 |
|
$this->output = $output; |
|
48
|
5 |
|
$this->models = $this->getModels(); |
|
49
|
5 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $models |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
5 |
|
private function getModels(array $models = []) |
|
56
|
|
|
{ |
|
57
|
5 |
|
foreach (get_declared_classes() as $class) { |
|
58
|
|
|
// Ignoring PHP bug #53727 here, Eloquent Models implement several interfaces. |
|
59
|
5 |
|
if (is_subclass_of($class, Model::class) && method_exists($class, 'searchFields')) { |
|
60
|
5 |
|
$models[] = $class; |
|
61
|
5 |
|
} |
|
62
|
5 |
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
return $models; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $models |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function setModels(array $models = []) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->models = $models; |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $type |
|
77
|
|
|
* @param string $string |
|
78
|
|
|
*/ |
|
79
|
5 |
|
private function output($type, $string) |
|
80
|
|
|
{ |
|
81
|
5 |
|
if (!$this->output instanceof NullOutput) { |
|
82
|
4 |
|
$this->output->$type($string); |
|
83
|
4 |
|
} |
|
84
|
5 |
|
} |
|
85
|
|
|
|
|
86
|
5 |
|
public function rebuild() |
|
87
|
|
|
{ |
|
88
|
5 |
|
if (empty($this->models)) { |
|
89
|
1 |
|
$this->output('error', 'No models configured for search.'); |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
$this->loopModels(); |
|
93
|
|
|
|
|
94
|
5 |
|
$this->output('comment', PHP_EOL.'Search engine rebuild complete.'); |
|
95
|
5 |
|
return $this->output; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Loop Models |
|
100
|
|
|
* |
|
101
|
|
|
* return @void |
|
102
|
|
|
*/ |
|
103
|
5 |
|
private function loopModels() |
|
104
|
|
|
{ |
|
105
|
5 |
|
foreach ($this->models as $modelName) { |
|
106
|
4 |
|
$this->rebuildModel(new $modelName); |
|
107
|
5 |
|
} |
|
108
|
5 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param Model $model |
|
112
|
|
|
*/ |
|
113
|
4 |
|
private function rebuildModel(Model $model) |
|
114
|
|
|
{ |
|
115
|
4 |
|
$this->output('comment', 'Creating index for model ['.$model->getTable().']'); |
|
116
|
|
|
|
|
117
|
4 |
|
if ($model->count() === 0) { |
|
118
|
2 |
|
$this->output( |
|
119
|
2 |
|
'comment', |
|
120
|
2 |
|
'No records for model ['.$model->getTable().'].' |
|
121
|
2 |
|
); |
|
122
|
2 |
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
$this->progressBar->start($model->count()); |
|
126
|
2 |
|
$this->chunk($model); |
|
127
|
2 |
|
$this->progressBar->finish(); |
|
128
|
2 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Model $object |
|
132
|
|
|
*/ |
|
133
|
|
|
private function chunk(Model $object) |
|
134
|
|
|
{ |
|
135
|
2 |
|
$object->chunk(1000, function($chunk) { |
|
136
|
1 |
|
foreach ($chunk as $record) { |
|
137
|
1 |
|
$this->store->insertModel($record, false); |
|
138
|
1 |
|
$this->progressBar->advance(); |
|
139
|
1 |
|
} |
|
140
|
2 |
|
}); |
|
141
|
2 |
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|