1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Donii Sergii <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace sonrac\Arango; |
7
|
|
|
|
8
|
|
|
use ArangoDBClient\CollectionHandler as ArangoDBCollectionHandler; |
9
|
|
|
use ArangoDBClient\Connection as ArangoDBConnection; |
10
|
|
|
use ArangoDBClient\ConnectionOptions as ArangoDBConnectionOptions; |
11
|
|
|
use ArangoDBClient\Exception as ArangoException; |
12
|
|
|
use ArangoDBClient\UpdatePolicy as ArangoDBUpdatePolicy; |
13
|
|
|
use Illuminate\Database\Connection as IlluminateConnection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Connection. |
17
|
|
|
* Arango connection. |
18
|
|
|
* |
19
|
|
|
* @author Donii Sergii <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Connection extends IlluminateConnection |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Arango.DB Query Builder |
25
|
|
|
* |
26
|
|
|
* @var |
27
|
|
|
* |
28
|
|
|
* @author Donii Sergii <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
protected $db; |
31
|
|
|
|
32
|
|
|
protected $database = '_system'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Arango.DB connection |
36
|
|
|
* |
37
|
|
|
* @var \ArangoDBClient\Connection |
38
|
|
|
* |
39
|
|
|
* @author Donii Sergii <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
protected $arangoConnection; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Connection constructor. |
45
|
|
|
* |
46
|
|
|
* @param array $config Connection options |
47
|
|
|
* |
48
|
|
|
* @author Donii Sergii <[email protected]> |
49
|
|
|
*/ |
50
|
11 |
|
public function __construct(array $config = []) |
51
|
|
|
{ |
52
|
11 |
|
$this->config = $config; |
53
|
|
|
|
54
|
11 |
|
$this->arangoConnection = $this->createConnection(); |
55
|
|
|
|
56
|
11 |
|
$this->db = new ArangoDBCollectionHandler($this->arangoConnection); |
57
|
11 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get Arango.DB Query Builder |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
* |
64
|
|
|
* @author Donii Sergii <[email protected]> |
65
|
|
|
*/ |
66
|
1 |
|
public function getArangoDB() |
67
|
|
|
{ |
68
|
1 |
|
return $this->db; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get Arango.DB client |
73
|
|
|
* |
74
|
|
|
* @return \ArangoDBClient\Connection |
75
|
|
|
* |
76
|
|
|
* @author Donii Sergii <[email protected]> |
77
|
|
|
*/ |
78
|
1 |
|
public function getArangoClient() |
79
|
|
|
{ |
80
|
1 |
|
return $this->arangoConnection; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create new arango.db connection. |
85
|
|
|
* |
86
|
|
|
* @param array $config Config |
87
|
|
|
* |
88
|
|
|
* @return \ArangoDBClient\Connection |
89
|
|
|
* |
90
|
|
|
* @author Donii Sergii <[email protected]> |
91
|
|
|
*/ |
92
|
11 |
|
public function createConnection(array $config = []) |
93
|
|
|
{ |
94
|
11 |
|
$config = $this->config + $config; |
95
|
|
|
|
96
|
11 |
|
ArangoException::enableLogging(); |
97
|
|
|
|
98
|
11 |
|
return new ArangoDBConnection($this->getMainConnectionOption() + $config); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get database name. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
* |
106
|
|
|
* @author Donii Sergii <[email protected]> |
107
|
|
|
*/ |
108
|
11 |
|
public function getDatabaseName() |
109
|
|
|
{ |
110
|
11 |
|
$this->database = $this->getOption(ArangoDBConnectionOptions::OPTION_DATABASE, $this->database); |
111
|
|
|
|
112
|
11 |
|
return parent::getDatabaseName(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get arango.db endpoint. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
* |
120
|
|
|
* @author Donii Sergii <[email protected]> |
121
|
|
|
*/ |
122
|
11 |
|
public function getEndPoint() |
123
|
|
|
{ |
124
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_ENDPOINT, 'tcp://127.0.0.1:8529'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get auth type |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
* |
132
|
|
|
* @author Donii Sergii <[email protected]> |
133
|
|
|
*/ |
134
|
11 |
|
public function getAuthType() |
135
|
|
|
{ |
136
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_AUTH_TYPE, 'Basic'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get auth type |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
* |
144
|
|
|
* @author Donii Sergii <[email protected]> |
145
|
|
|
*/ |
146
|
11 |
|
public function getAuthUser() |
147
|
|
|
{ |
148
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_AUTH_USER, 'root'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get auth type |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
* |
156
|
|
|
* @author Donii Sergii <[email protected]> |
157
|
|
|
*/ |
158
|
11 |
|
public function getAuthPassword() |
159
|
|
|
{ |
160
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_AUTH_PASSWD, ''); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get connection option |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
* |
168
|
|
|
* @author Donii Sergii <[email protected]> |
169
|
|
|
*/ |
170
|
11 |
|
public function getConnectionOption() |
171
|
|
|
{ |
172
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_CONNECTION, 'Keep-Alive'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get timeout option |
177
|
|
|
* |
178
|
|
|
* @return int |
179
|
|
|
* |
180
|
|
|
* @author Donii Sergii <[email protected]> |
181
|
|
|
*/ |
182
|
11 |
|
public function getTimeout() |
183
|
|
|
{ |
184
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_TIMEOUT, 3); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get reconnect option |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
* |
192
|
|
|
* @author Donii Sergii <[email protected]> |
193
|
|
|
*/ |
194
|
11 |
|
public function getReconnect() |
195
|
|
|
{ |
196
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_RECONNECT, true); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get create option |
201
|
|
|
* |
202
|
|
|
* @return mixed |
203
|
|
|
* |
204
|
|
|
* @author Donii Sergii <[email protected]> |
205
|
|
|
*/ |
206
|
11 |
|
public function getCreate() |
207
|
|
|
{ |
208
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_CREATE, true); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get update policy option |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
* |
216
|
|
|
* @author Donii Sergii <[email protected]> |
217
|
|
|
*/ |
218
|
11 |
|
public function getUpdatePolicy() |
219
|
|
|
{ |
220
|
11 |
|
return $this->getOption(ArangoDBConnectionOptions::OPTION_UPDATE_POLICY, ArangoDBUpdatePolicy::LAST); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get option value |
225
|
|
|
* |
226
|
|
|
* @param string $optionName |
227
|
|
|
* @param mixed $default |
228
|
|
|
* |
229
|
|
|
* @return mixed |
230
|
|
|
* |
231
|
|
|
* @author Donii Sergii <[email protected]> |
232
|
|
|
*/ |
233
|
11 |
|
private function getOption($optionName, $default) |
234
|
|
|
{ |
235
|
11 |
|
if (!isset($this->config[$optionName])) { |
236
|
11 |
|
$this->config[$optionName] = $default; |
237
|
|
|
} |
238
|
|
|
|
239
|
11 |
|
return $this->config[$optionName]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get main connection option |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
* |
247
|
|
|
* @author Donii Sergii <[email protected]> |
248
|
|
|
*/ |
249
|
11 |
|
private function getMainConnectionOption() |
250
|
|
|
{ |
251
|
|
|
return [ |
252
|
|
|
// database name |
253
|
11 |
|
ArangoDBConnectionOptions::OPTION_DATABASE => $this->getDatabaseName(), |
254
|
|
|
// server endpoint to connect to |
255
|
11 |
|
ArangoDBConnectionOptions::OPTION_ENDPOINT => $this->getEndPoint(), |
256
|
|
|
// authorization type to use (currently supported: 'Basic') |
257
|
11 |
|
ArangoDBConnectionOptions::OPTION_AUTH_TYPE => $this->getAuthType(), |
258
|
|
|
// user for basic authorization |
259
|
11 |
|
ArangoDBConnectionOptions::OPTION_AUTH_USER => $this->getAuthUser(), |
260
|
|
|
// password for basic authorization |
261
|
11 |
|
ArangoDBConnectionOptions::OPTION_AUTH_PASSWD => $this->getAuthPassword(), |
262
|
|
|
// connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections) |
263
|
11 |
|
ArangoDBConnectionOptions::OPTION_CONNECTION => $this->getConnectionOption(), |
264
|
|
|
// connect timeout in seconds |
265
|
11 |
|
ArangoDBConnectionOptions::OPTION_TIMEOUT => $this->getTimeout(), |
266
|
|
|
// whether or not to reconnect when a keep-alive connection has timed out on server |
267
|
11 |
|
ArangoDBConnectionOptions::OPTION_RECONNECT => $this->getReconnect(), |
268
|
|
|
// optionally create new collections when inserting documents |
269
|
11 |
|
ArangoDBConnectionOptions::OPTION_CREATE => $this->getCreate(), |
270
|
|
|
// optionally create new collections when inserting documents |
271
|
11 |
|
ArangoDBConnectionOptions::OPTION_UPDATE_POLICY => $this->getUpdatePolicy(), |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|