1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mina |
5
|
|
|
* Date: 3/26/17 |
6
|
|
|
* Time: 3:38 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MAbadir\ElasticLaravel\Console; |
10
|
|
|
|
11
|
|
|
use MAbadir\ElasticLaravel\ElasticClient; |
12
|
|
|
|
13
|
|
|
trait IndexInitializationTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Basic standard configuration |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $params = [ |
20
|
|
|
"body" => [ |
21
|
|
|
"settings" => [ |
22
|
|
|
"number_of_shards" => 1, |
23
|
|
|
"number_of_replicas" => 0, |
24
|
|
|
], |
25
|
|
|
], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Holds the ElasticClient |
30
|
|
|
* @var \MAbadir\ElasticLaravel\ElasticClient |
31
|
|
|
*/ |
32
|
|
|
protected $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* IndexInitializationTrait constructor. |
36
|
|
|
* |
37
|
|
|
* @param \MAbadir\ElasticLaravel\ElasticClient $client |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ElasticClient $client) |
40
|
|
|
{ |
41
|
|
|
parent::__construct(); |
42
|
|
|
$this->client = $client; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the console command. |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function handle() |
51
|
|
|
{ |
52
|
|
|
if ($this->confirm('Are you sure you want to proceed, this will destroy the Search Index? [y|N]')) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return $this->confirmPassword(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Confirms user awareness before proceeding |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
* @author Mina Abadir <[email protected]> |
63
|
|
|
* @copyright Copyright (c) 2016, Mina Abadir |
64
|
|
|
*/ |
65
|
|
|
protected function confirmPassword() |
66
|
|
|
{ |
67
|
|
|
$password = str_random(7); |
68
|
|
|
$input = $this->ask('For safety please enter this text "'.$password.'"'); |
|
|
|
|
69
|
|
|
if($input == $password){ |
70
|
|
|
return $this->buildIndex(); |
71
|
|
|
} |
72
|
|
|
else { |
73
|
|
|
return $this->error('Password is incorrect!'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Deletes, Creates the Index then puts the settings |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
* @author Mina Abadir <[email protected]> |
82
|
|
|
* @copyright Copyright (c) 2016, Mina Abadir |
83
|
|
|
*/ |
84
|
|
|
protected function buildIndex() |
85
|
|
|
{ |
86
|
|
|
$this->client->dropIndex(); |
87
|
|
|
$response = $this->client->createIndex($this->params); |
88
|
|
|
|
89
|
|
|
if(is_array($response) && $response['acknowledged'] == true) |
90
|
|
|
{ |
91
|
|
|
$this->info('Index Initialization Successful'); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
else{ |
94
|
|
|
$this->error('Index Initialization Failed'); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.