1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArangoClient\Schema; |
4
|
|
|
|
5
|
|
|
use ArangoClient\Connector; |
6
|
|
|
use ArangoClient\Exceptions\ArangoException; |
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
8
|
|
|
|
9
|
|
|
/* |
10
|
|
|
* @see https://www.arangodb.com/docs/stable/http/database.html |
11
|
|
|
*/ |
12
|
|
|
trait ManagesDatabases |
13
|
|
|
{ |
14
|
|
|
protected Connector $connector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return array<mixed> |
18
|
|
|
* @throws ArangoException |
19
|
|
|
* @throws GuzzleException |
20
|
|
|
*/ |
21
|
1 |
|
public function getCurrentDatabase(): array |
22
|
|
|
{ |
23
|
1 |
|
return (array) $this->connector->request('get', '/_api/database/current'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param bool $full |
28
|
|
|
* @return array<mixed> |
29
|
|
|
* |
30
|
|
|
* @throws ArangoException |
31
|
|
|
* @throws GuzzleException |
32
|
|
|
*/ |
33
|
43 |
|
public function getDatabases(bool $full = false): array |
34
|
|
|
{ |
35
|
43 |
|
$user = $this->connector->getUser(); |
36
|
|
|
|
37
|
43 |
|
$uri = '/_api/user/' . $user . '/database'; |
38
|
|
|
$options = [ |
39
|
|
|
'query' => [ |
40
|
43 |
|
'full' => $full |
41
|
|
|
] |
42
|
|
|
]; |
43
|
|
|
|
44
|
43 |
|
return (array) $this->connector->request('get', $uri, $options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $database |
49
|
|
|
* @return bool |
50
|
|
|
* @throws ArangoException |
51
|
|
|
* @throws GuzzleException |
52
|
|
|
*/ |
53
|
43 |
|
public function hasDatabase(string $database): bool |
54
|
|
|
{ |
55
|
43 |
|
$databaseList = $this->getDatabases(); |
56
|
|
|
|
57
|
43 |
|
return isset($databaseList[$database]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $name |
62
|
|
|
* @param null $options |
|
|
|
|
63
|
|
|
* @param null $users |
|
|
|
|
64
|
|
|
* @return bool |
65
|
|
|
* @throws ArangoException |
66
|
|
|
* @throws GuzzleException |
67
|
|
|
*/ |
68
|
5 |
|
public function createDatabase(string $name, $options = null, $users = null): bool |
69
|
|
|
{ |
70
|
5 |
|
$database = json_encode((object)['name' => $name, 'options' => $options, 'users' => $users]); |
71
|
|
|
|
72
|
5 |
|
return (bool) $this->connector->request('post', '/_api/database', ['body' => $database]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @return bool |
78
|
|
|
* @throws ArangoException |
79
|
|
|
* @throws GuzzleException |
80
|
|
|
*/ |
81
|
3 |
|
public function deleteDatabase(string $name): bool |
82
|
|
|
{ |
83
|
3 |
|
$uri = '/_api/database/' . $name; |
84
|
|
|
|
85
|
3 |
|
return (bool) $this->connector->request('delete', $uri); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|