1 | <?php |
||
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) |
||
89 | |||
90 | /** |
||
91 | * @codeCoverageIgnore This is implicitly tested |
||
92 | * @param AnnotatedInterface $model |
||
93 | * @return static |
||
94 | */ |
||
95 | public static function create(AnnotatedInterface $model) |
||
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) |
||
131 | |||
132 | /** |
||
133 | * @codeCoverageIgnore This is implicitly tested |
||
134 | */ |
||
135 | public function init() |
||
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() |
||
177 | |||
178 | } |
||
179 |