1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link http://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
14
|
|
|
|
15
|
|
|
use Elasticsearch\Client; |
16
|
|
|
use Elasticsearch\ClientBuilder; |
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
18
|
|
|
use Maslosoft\EmbeDi\EmbeDi; |
19
|
|
|
use Maslosoft\Manganel\Meta\ManganelMeta; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Manganel |
23
|
|
|
* |
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
25
|
|
|
*/ |
26
|
|
|
class Manganel |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
const DefaultIndexId = 'manganel'; |
30
|
|
|
|
31
|
|
|
public $hosts = [ |
32
|
|
|
'localhost:9200' |
33
|
|
|
]; |
34
|
|
|
public $auth = null; |
35
|
|
|
public $username = ''; |
36
|
|
|
public $password = ''; |
37
|
|
|
public $params = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* TODO Enforce lowercase |
41
|
|
|
*/ |
42
|
|
|
public $index = 'my_index'; |
43
|
|
|
public $indexId = self::DefaultIndexId; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @var Client |
48
|
|
|
*/ |
49
|
|
|
private $client = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Dependency injection container |
53
|
|
|
* @var EmbeDi |
54
|
|
|
*/ |
55
|
|
|
private $di = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Instances of manganel |
59
|
|
|
* @var Manganel[] |
60
|
|
|
*/ |
61
|
|
|
private static $mnl = []; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Hash map of class name to id. This is to reduce overhead of Mangan::fromModel() |
65
|
|
|
* @var string[] |
66
|
|
|
*/ |
67
|
|
|
private static $classToId = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Class constructor |
71
|
|
|
* @codeCoverageIgnore This is implicitly tested |
72
|
|
|
* @param string $indexId |
73
|
|
|
*/ |
74
|
|
|
public function __construct($indexId = self::DefaultIndexId) |
75
|
|
|
{ |
76
|
|
|
if (empty($indexId)) |
77
|
|
|
{ |
78
|
|
|
$indexId = self::DefaultIndexId; |
79
|
|
|
} |
80
|
|
|
$this->indexId = $indexId; |
81
|
|
|
$this->di = new EmbeDi($this->indexId); |
82
|
|
|
$this->di->configure($this); |
83
|
|
|
|
84
|
|
|
if (empty(self::$mnl[$indexId])) |
85
|
|
|
{ |
86
|
|
|
self::$mnl[$indexId] = $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @codeCoverageIgnore This is implicitly tested |
92
|
|
|
* @param AnnotatedInterface $model |
93
|
|
|
* @return static |
94
|
|
|
*/ |
95
|
|
|
public static function create(AnnotatedInterface $model) |
96
|
|
|
{ |
97
|
|
|
$key = get_class($model); |
98
|
|
|
if (isset(self::$classToId[$key])) |
99
|
|
|
{ |
100
|
|
|
$indexId = self::$classToId[$key]; |
101
|
|
|
} |
102
|
|
|
else |
103
|
|
|
{ |
104
|
|
|
$indexId = ManganelMeta::create($model)->type()->indexId; |
105
|
|
|
self::$classToId[$key] = $indexId; |
106
|
|
|
} |
107
|
|
|
return static::fly($indexId); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get flyweight instance of Manganel component. |
112
|
|
|
* Only one instance will be created for each `$indexId`. |
113
|
|
|
* |
114
|
|
|
* @codeCoverageIgnore This is implicitly tested |
115
|
|
|
* @new |
116
|
|
|
* @param string $indexId |
117
|
|
|
* @return Manganel |
118
|
|
|
*/ |
119
|
|
|
public static function fly($indexId = self::DefaultIndexId) |
120
|
|
|
{ |
121
|
|
|
if (empty($indexId)) |
122
|
|
|
{ |
123
|
|
|
$indexId = self::DefaultIndexId; |
124
|
|
|
} |
125
|
|
|
if (empty(self::$mnl[$indexId])) |
126
|
|
|
{ |
127
|
|
|
self::$mnl[$indexId] = new static($indexId); |
128
|
|
|
} |
129
|
|
|
return self::$mnl[$indexId]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @codeCoverageIgnore This is implicitly tested |
134
|
|
|
*/ |
135
|
|
|
public function init() |
136
|
|
|
{ |
137
|
|
|
$this->di->store($this); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Drop current index |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
1 |
|
public function drop() |
145
|
|
|
{ |
146
|
|
|
$params = [ |
147
|
1 |
|
'index' => strtolower($this->index) |
148
|
1 |
|
]; |
149
|
1 |
|
$result = $this->getClient()->indices()->delete($params); |
150
|
1 |
|
if (is_array($result) && array_key_exists('acknowledged', $result) && $result['acknowledged']) |
151
|
1 |
|
{ |
152
|
1 |
|
return true; |
153
|
|
|
} |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @codeCoverageIgnore This is implicitly tested |
159
|
|
|
* @return Client |
160
|
|
|
*/ |
161
|
|
|
public function getClient() |
162
|
|
|
{ |
163
|
|
|
if (null === $this->client) |
164
|
|
|
{ |
165
|
|
|
$this->params['hosts'] = $this->hosts; |
166
|
|
|
$this->params['connectionParams']['auth'] = [ |
167
|
|
|
$this->username, |
168
|
|
|
$this->password, |
169
|
|
|
$this->auth |
170
|
|
|
]; |
171
|
|
|
$cb = ClientBuilder::create(); |
172
|
|
|
$cb->setHosts($this->hosts); |
173
|
|
|
$this->client = $cb->build(); |
174
|
|
|
} |
175
|
|
|
return $this->client; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|