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