1 <?php
2
3 4 5 6 7 8 9
10
11 namespace Omines\DirectAdmin\Objects;
12
13 use Omines\DirectAdmin\Context\UserContext;
14 use Omines\DirectAdmin\Objects\Users\User;
15
16 17 18 19 20
21 class Database extends BaseObject
22 {
23 const CACHE_ACCESS_HOSTS = 'access_hosts';
24
25
26 private $owner;
27
28
29 private $databaseName;
30
31 32 33 34 35 36 37
38 public function __construct($name, User $owner, UserContext $context)
39 {
40 parent::__construct($name, $context);
41 $this->owner = $owner;
42 $this->databaseName = $this->owner->getUsername() . '_' . $this->getName();
43 }
44
45 46 47 48 49 50 51 52 53
54 public static function create(User $user, $name, $username, $password)
55 {
56 $options = [
57 'action' => 'create',
58 'name' => $name,
59 ];
60 if (!empty($password)) {
61 $options += ['user' => $username, 'passwd' => $password, 'passwd2' => $password];
62 } else {
63 $options += ['userlist' => $username];
64 }
65 $user->getContext()->invokeApiPost('DATABASES', $options);
66 return new self($name, $user, $user->getContext());
67 }
68
69 70 71
72 public function delete()
73 {
74 $this->getContext()->invokeApiPost('DATABASES', [
75 'action' => 'delete',
76 'select0' => $this->getDatabaseName(),
77 ]);
78 $this->getContext()->getContextUser()->clearCache();
79 }
80
81 82 83
84 public function getAccessHosts()
85 {
86 return $this->getCache(self::CACHE_ACCESS_HOSTS, function () {
87 $accessHosts = $this->getContext()->invokeApiGet('DATABASES', [
88 'action' => 'accesshosts',
89 'db' => $this->getDatabaseName(),
90 ]);
91
92 return array_map(function ($name) {
93 return new Database\AccessHost($name, $this);
94 }, $accessHosts);
95 });
96 }
97
98 99 100 101
102 public function createAccessHost($name)
103 {
104 $accessHost = Database\AccessHost::create($this, $name);
105 $this->getContext()->getContextUser()->clearCache();
106 return $accessHost;
107 }
108
109 110 111
112 public function getDatabaseName()
113 {
114 return $this->databaseName;
115 }
116
117 118 119
120 public function getOwner()
121 {
122 return $this->owner;
123 }
124 }
125