|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Manganel; |
|
10
|
|
|
|
|
11
|
|
|
use Elasticsearch\Client; |
|
12
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
13
|
|
|
use Maslosoft\EmbeDi\EmbeDi; |
|
14
|
|
|
use Maslosoft\Manganel\Meta\ManganelMeta; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Manganel |
|
18
|
|
|
* |
|
19
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Manganel |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
const DefaultIndexId = 'manganel'; |
|
25
|
|
|
|
|
26
|
|
|
public $hosts = [ |
|
27
|
|
|
'localhost:9200' |
|
28
|
|
|
]; |
|
29
|
|
|
public $auth = null; |
|
30
|
|
|
public $username = ''; |
|
31
|
|
|
public $password = ''; |
|
32
|
|
|
public $params = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* TODO Enforce lowercase |
|
36
|
|
|
*/ |
|
37
|
|
|
public $index = 'my_index'; |
|
38
|
|
|
public $indexId = self::DefaultIndexId; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @var Client |
|
43
|
|
|
*/ |
|
44
|
|
|
private $client = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Dependency injection container |
|
48
|
|
|
* @var EmbeDi |
|
49
|
|
|
*/ |
|
50
|
|
|
private $di = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Instances of manganel |
|
54
|
|
|
* @var Manganel[] |
|
55
|
|
|
*/ |
|
56
|
|
|
private static $mnl = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Hash map of class name to id. This is to reduce overhead of Mangan::fromModel() |
|
60
|
|
|
* @var string[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private static $classToId = []; |
|
63
|
|
|
|
|
64
|
|
|
public function __construct($indexId = self::DefaultIndexId) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$indexId) |
|
67
|
|
|
{ |
|
68
|
|
|
$indexId = self::DefaultIndexId; |
|
69
|
|
|
} |
|
70
|
|
|
$this->indexId = $indexId; |
|
71
|
|
|
$this->di = new EmbeDi($this->indexId); |
|
72
|
|
|
$this->di->configure($this); |
|
73
|
|
|
|
|
74
|
|
|
if (empty(self::$mnl[$indexId])) |
|
75
|
|
|
{ |
|
76
|
|
|
self::$mnl[$indexId] = $this; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public static function create(AnnotatedInterface $model) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$key = get_class($model); |
|
83
|
3 |
|
if (isset(self::$classToId[$key])) |
|
84
|
3 |
|
{ |
|
85
|
2 |
|
$indexId = self::$classToId[$key]; |
|
86
|
2 |
|
} |
|
87
|
|
|
else |
|
88
|
|
|
{ |
|
89
|
1 |
|
$indexId = ManganelMeta::create($model)->type()->indexId; |
|
|
|
|
|
|
90
|
1 |
|
self::$classToId[$key] = $indexId; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
3 |
|
return static::fly($indexId); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get flyweight instance of Manganel component. |
|
98
|
|
|
* Only one instance will be created for each `$indexId`. |
|
99
|
|
|
* |
|
100
|
|
|
* @new |
|
101
|
|
|
* @param string $indexId |
|
102
|
|
|
* @return Manganel |
|
103
|
|
|
*/ |
|
104
|
8 |
|
public static function fly($indexId = self::DefaultIndexId) |
|
105
|
|
|
{ |
|
106
|
8 |
|
if (empty($indexId)) |
|
107
|
8 |
|
{ |
|
108
|
|
|
$indexId = self::DefaultIndexId; |
|
109
|
|
|
} |
|
110
|
8 |
|
if (empty(self::$mnl[$indexId])) |
|
111
|
8 |
|
{ |
|
112
|
|
|
self::$mnl[$indexId] = new static($indexId); |
|
113
|
|
|
} |
|
114
|
8 |
|
return self::$mnl[$indexId]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function init() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->di->store($this); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
8 |
|
public function getClient() |
|
123
|
|
|
{ |
|
124
|
8 |
|
if (null === $this->client) |
|
125
|
8 |
|
{ |
|
126
|
|
|
$this->params['hosts'] = $this->hosts; |
|
127
|
|
|
$this->params['connectionParams']['auth'] = [ |
|
128
|
|
|
$this->username, |
|
129
|
|
|
$this->password, |
|
130
|
|
|
$this->auth |
|
131
|
|
|
]; |
|
132
|
|
|
$cb = \Elasticsearch\ClientBuilder::create(); |
|
133
|
|
|
$cb->setHosts($this->hosts); |
|
134
|
|
|
$this->client = $cb->build(); |
|
135
|
|
|
} |
|
136
|
8 |
|
return $this->client; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.