1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin; |
4
|
|
|
|
5
|
|
|
use Jaxon\Plugin\AbstractPackage; |
6
|
|
|
use Lagdo\DbAdmin\Ajax\App\Admin; |
7
|
|
|
|
8
|
|
|
use function in_array; |
9
|
|
|
use function is_array; |
10
|
|
|
use function realpath; |
11
|
|
|
use function Jaxon\cl; |
12
|
|
|
use function Jaxon\rq; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Jaxon DbAdmin package |
16
|
|
|
*/ |
17
|
|
|
class DbAdminPackage extends AbstractPackage |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Get the path to the config file |
21
|
|
|
* |
22
|
|
|
* @return string|array |
23
|
|
|
*/ |
24
|
|
|
public static function config(): string |
25
|
|
|
{ |
26
|
|
|
return realpath(__DIR__ . '/../config/dbadmin.php'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the database servers |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getServers(): array |
35
|
|
|
{ |
36
|
|
|
return $this->getOption('servers', []); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the name of a given server |
41
|
|
|
* |
42
|
|
|
* @param string $server The server name in the configuration |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getServerName(string $server): string |
47
|
|
|
{ |
48
|
|
|
return $this->getOption("servers.$server.name", ''); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get a given server options |
53
|
|
|
* |
54
|
|
|
* @param string $server The server name in the configuration |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getServerOptions(string $server): array |
59
|
|
|
{ |
60
|
|
|
return $this->getOption("servers.$server", []); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the driver of a given server |
65
|
|
|
* |
66
|
|
|
* @param string $server The server name in the configuration |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getServerDriver(string $server): string |
71
|
|
|
{ |
72
|
|
|
return $this->getOption("servers.$server.driver", ''); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if the user has access to a server |
77
|
|
|
* |
78
|
|
|
* @param string $server The database server |
79
|
|
|
* |
80
|
|
|
* return bool |
81
|
|
|
*/ |
82
|
|
|
public function getServerAccess(string $server): bool |
83
|
|
|
{ |
84
|
|
|
// Check in server options |
85
|
|
|
$serverAccess = $this->getOption("servers.$server.access.server", null); |
86
|
|
|
if($serverAccess === true || $serverAccess === false) |
87
|
|
|
{ |
88
|
|
|
return $serverAccess; |
89
|
|
|
} |
90
|
|
|
// Check in global options |
91
|
|
|
return $this->getOption('access.server', true) === true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function hasLoggingDatabase(): bool |
98
|
|
|
{ |
99
|
|
|
$options = $this->getOption('logging.database'); |
100
|
|
|
return is_array($options) && isset($options['driver']) && |
101
|
|
|
in_array($options['driver'], ['pgsql', 'mysql', 'sqlite']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the HTML tags to include CSS code and files into the page |
106
|
|
|
* |
107
|
|
|
* The code must be enclosed in the appropriate HTML tags. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getCss(): string |
112
|
|
|
{ |
113
|
|
|
return $this->view()->render('dbadmin::codes::css.html') . |
114
|
|
|
"\n<style>\n" . |
115
|
|
|
$this->view()->render('dbadmin::codes::styles.css') . |
116
|
|
|
"\n</style>\n<!-- Spinner CSS code. -->\n<style>\n" . |
117
|
|
|
$this->view()->render('dbadmin::codes::spin.css') . |
118
|
|
|
"\n</style>\n"; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the HTML tags to include javascript code and files into the page |
123
|
|
|
* |
124
|
|
|
* The code must be enclosed in the appropriate HTML tags. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getJs(): string |
129
|
|
|
{ |
130
|
|
|
return $this->view()->render('dbadmin::codes::js.html'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the javascript code to include into the page |
135
|
|
|
* |
136
|
|
|
* The code must NOT be enclosed in HTML tags. |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getScript(): string |
141
|
|
|
{ |
142
|
|
|
return $this->view()->render('dbadmin::codes::script.js') . |
143
|
|
|
"\n\n// Spinner javascript code.\n" . |
144
|
|
|
$this->view()->render('dbadmin::codes::spin.js'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the javascript code to include into the page |
149
|
|
|
* |
150
|
|
|
* The code must NOT be enclosed in HTML tags. |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getReadyScript(): string |
155
|
|
|
{ |
156
|
|
|
$defaultServer = $this->getOption('default'); |
157
|
|
|
return !$defaultServer || |
158
|
|
|
!$this->getOption("servers.$defaultServer") ? '' : |
159
|
|
|
rq(Admin::class)->server($defaultServer); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the HTML code of the package home page |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getHtml(): string |
168
|
|
|
{ |
169
|
|
|
return cl(Admin::class)->html(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|