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
|
|
|
* @return array<mixed> |
28
|
|
|
* |
29
|
|
|
* @throws GuzzleException|ArangoException |
30
|
|
|
*/ |
31
|
21 |
|
public function listDatabases(): array |
32
|
|
|
{ |
33
|
21 |
|
return (array) $this->connector->request('get', '/_api/database'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $database |
38
|
|
|
* @return bool |
39
|
|
|
* @throws ArangoException |
40
|
|
|
* @throws GuzzleException |
41
|
|
|
*/ |
42
|
21 |
|
public function hasDatabase(string $database): bool |
43
|
|
|
{ |
44
|
21 |
|
$databaseList = $this->listDatabases(); |
45
|
21 |
|
return in_array($database, $databaseList); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array<mixed> |
50
|
|
|
* |
51
|
|
|
* @throws GuzzleException|ArangoException |
52
|
|
|
*/ |
53
|
1 |
|
public function listMyDatabases(): array |
54
|
|
|
{ |
55
|
1 |
|
return (array) $this->connector->request('get', '/_api/database/user'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $name |
60
|
|
|
* @param null $options |
|
|
|
|
61
|
|
|
* @param null $users |
|
|
|
|
62
|
|
|
* @return bool |
63
|
|
|
* @throws ArangoException |
64
|
|
|
* @throws GuzzleException |
65
|
|
|
*/ |
66
|
3 |
|
public function createDatabase(string $name, $options = null, $users = null): bool |
67
|
|
|
{ |
68
|
3 |
|
$database = json_encode((object)['name' => $name, 'options' => $options, 'users' => $users]); |
69
|
|
|
|
70
|
3 |
|
return (bool) $this->connector->request('post', '/_api/database', ['body' => $database]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @return bool |
76
|
|
|
* @throws ArangoException |
77
|
|
|
* @throws GuzzleException |
78
|
|
|
*/ |
79
|
1 |
|
public function deleteDatabase(string $name): bool |
80
|
|
|
{ |
81
|
1 |
|
$uri = '/_api/database/' . $name; |
82
|
|
|
|
83
|
1 |
|
return (bool) $this->connector->request('delete', $uri); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|