1 | <?php |
||||
2 | |||||
3 | namespace Lagdo\DbAdmin\Db\Facades; |
||||
4 | |||||
5 | use function array_key_exists; |
||||
6 | use function is_array; |
||||
7 | use function array_intersect; |
||||
8 | use function array_values; |
||||
9 | use function compact; |
||||
10 | use function is_string; |
||||
11 | use function array_keys; |
||||
12 | |||||
13 | /** |
||||
14 | * Facade to server functions |
||||
15 | */ |
||||
16 | class ServerFacade extends AbstractFacade |
||||
17 | { |
||||
18 | /** |
||||
19 | * The final database list |
||||
20 | * |
||||
21 | * @var array|null |
||||
22 | */ |
||||
23 | protected $finalDatabases = null; |
||||
24 | |||||
25 | /** |
||||
26 | * The databases the user has access to |
||||
27 | * |
||||
28 | * @var array|null |
||||
29 | */ |
||||
30 | protected $userDatabases = null; |
||||
31 | |||||
32 | /** |
||||
33 | * The constructor |
||||
34 | * |
||||
35 | * @param AbstractFacade $dbFacade |
||||
36 | * @param array $options The server config options |
||||
37 | */ |
||||
38 | public function __construct(AbstractFacade $dbFacade, array $options) |
||||
39 | { |
||||
40 | parent::__construct($dbFacade); |
||||
41 | // Set the user databases, if defined. |
||||
42 | if (is_array(($userDatabases = $options['access']['databases'] ?? null))) { |
||||
43 | $this->userDatabases = $userDatabases; |
||||
44 | } |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * Check if a feature is supported |
||||
49 | * |
||||
50 | * @param string $feature |
||||
51 | * |
||||
52 | * @return bool |
||||
53 | */ |
||||
54 | public function support(string $feature) |
||||
55 | { |
||||
56 | return $this->driver->support($feature); |
||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * Get the databases from the connected server |
||||
61 | * |
||||
62 | * @return array |
||||
63 | */ |
||||
64 | protected function databases(): array |
||||
65 | { |
||||
66 | if ($this->finalDatabases === null) { |
||||
67 | // Get the database lists |
||||
68 | // Passing false as parameter to this call prevent from using the slow_query() function, |
||||
69 | // which outputs data to the browser are prepended to the Jaxon response. |
||||
70 | $this->finalDatabases = $this->driver->databases(false); |
||||
71 | if (is_array($this->userDatabases)) { |
||||
72 | // Only keep databases that appear in the config. |
||||
73 | $this->finalDatabases = array_values(array_intersect($this->finalDatabases, $this->userDatabases)); |
||||
74 | } |
||||
75 | } |
||||
76 | return $this->finalDatabases; |
||||
77 | } |
||||
78 | |||||
79 | /** |
||||
80 | * Connect to a database server |
||||
81 | * |
||||
82 | * @return array |
||||
83 | */ |
||||
84 | public function getServerInfo(): array |
||||
85 | { |
||||
86 | $server = $this->utils->trans->lang( |
||||
87 | '%s version: %s. PHP extension %s.', |
||||
88 | $this->driver->name(), |
||||
89 | "<b>" . $this->utils->str->html($this->driver->serverInfo()) . "</b>", |
||||
0 ignored issues
–
show
|
|||||
90 | "<b>{$this->driver->extension()}</b>" |
||||
91 | ); |
||||
92 | $user = $this->utils->trans->lang('Logged as: %s.', "<b>" . $this->utils->str->html($this->driver->user()) . "</b>"); |
||||
93 | |||||
94 | return compact('server', 'user'); |
||||
95 | } |
||||
96 | |||||
97 | /** |
||||
98 | * Create a database |
||||
99 | * |
||||
100 | * @param string $database The database name |
||||
101 | * @param string $collation The database collation |
||||
102 | * |
||||
103 | * @return bool |
||||
104 | */ |
||||
105 | public function createDatabase(string $database, string $collation = ''): bool |
||||
106 | { |
||||
107 | return $this->driver->createDatabase($database, $collation); |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * Drop a database |
||||
112 | * |
||||
113 | * @param string $database The database name |
||||
114 | * |
||||
115 | * @return bool |
||||
116 | */ |
||||
117 | public function dropDatabase(string $database): bool |
||||
118 | { |
||||
119 | return $this->driver->dropDatabase($database); |
||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Get the collation list |
||||
124 | * |
||||
125 | * @return array |
||||
126 | */ |
||||
127 | public function getCollations(): array |
||||
128 | { |
||||
129 | return $this->driver->collations(); |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * Get the database list |
||||
134 | * |
||||
135 | * @return array |
||||
136 | */ |
||||
137 | public function getDatabases(): array |
||||
138 | { |
||||
139 | $headers = [ |
||||
140 | $this->utils->trans->lang('Database'), |
||||
141 | $this->utils->trans->lang('Collation'), |
||||
142 | $this->utils->trans->lang('Tables'), |
||||
143 | $this->utils->trans->lang('Size'), |
||||
144 | '', |
||||
145 | ]; |
||||
146 | |||||
147 | // Get the database list |
||||
148 | $databases = $this->databases(); |
||||
149 | $tables = $this->driver->countTables($databases); |
||||
150 | $collations = $this->driver->collations(); |
||||
151 | $details = []; |
||||
152 | foreach ($databases as $database) { |
||||
153 | $details[] = [ |
||||
154 | 'name' => $this->utils->str->html($database), |
||||
155 | 'collation' => $this->utils->str->html($this->driver->databaseCollation($database, $collations)), |
||||
156 | 'tables' => array_key_exists($database, $tables) ? $tables[$database] : 0, |
||||
157 | 'size' => $this->utils->trans->formatNumber($this->driver->databaseSize($database)), |
||||
0 ignored issues
–
show
The method
formatNumber() does not exist on Lagdo\DbAdmin\Driver\Utils\TranslatorInterface . It seems like you code against a sub-type of Lagdo\DbAdmin\Driver\Utils\TranslatorInterface such as Lagdo\DbAdmin\Translator .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
158 | ]; |
||||
159 | } |
||||
160 | |||||
161 | return compact('headers', 'databases', 'details'); |
||||
162 | } |
||||
163 | |||||
164 | /** |
||||
165 | * Get the processes |
||||
166 | * |
||||
167 | * @return array |
||||
168 | */ |
||||
169 | public function getProcesses(): array |
||||
170 | { |
||||
171 | // From processlist.inc.php |
||||
172 | $processes = $this->driver->processes(); |
||||
173 | |||||
174 | // From processlist.inc.php |
||||
175 | // TODO: Add a kill column in the headers |
||||
176 | $headers = []; |
||||
177 | $details = []; |
||||
178 | if (($process = reset($processes)) !== false) { |
||||
179 | // Set the keys of the first entry as headers |
||||
180 | $headers = array_keys($process); |
||||
181 | } |
||||
182 | foreach ($processes as $process) { |
||||
183 | $attrs = []; |
||||
184 | foreach ($process as $key => $val) { |
||||
185 | $attrs[] = is_string($val) ? $this->driver->processAttr($process, $key, $val) : '(null)'; |
||||
186 | } |
||||
187 | $details[] = $attrs; |
||||
188 | } |
||||
189 | return compact('headers', 'details'); |
||||
190 | } |
||||
191 | |||||
192 | /** |
||||
193 | * Get the variables |
||||
194 | * |
||||
195 | * @return array |
||||
196 | */ |
||||
197 | public function getVariables(): array |
||||
198 | { |
||||
199 | // From variables.inc.php |
||||
200 | $variables = $this->driver->variables(); |
||||
201 | |||||
202 | $headers = false; |
||||
203 | |||||
204 | $details = []; |
||||
205 | // From variables.inc.php |
||||
206 | foreach ($variables as $key => $val) { |
||||
207 | $details[] = [$this->utils->str->html($key), is_string($val) ? $this->utils->str->shortenUtf8($val, 50) : '(null)']; |
||||
208 | } |
||||
209 | |||||
210 | return compact('headers', 'details'); |
||||
211 | } |
||||
212 | |||||
213 | /** |
||||
214 | * Get the server status |
||||
215 | * |
||||
216 | * @return array |
||||
217 | */ |
||||
218 | public function getStatus(): array |
||||
219 | { |
||||
220 | // From variables.inc.php |
||||
221 | $status = $this->driver->statusVariables(); |
||||
222 | |||||
223 | $headers = false; |
||||
224 | $details = []; |
||||
225 | // From variables.inc.php |
||||
226 | foreach ($status as $key => $val) { |
||||
227 | $details[] = [$this->utils->str->html($key), is_string($val) ? $this->utils->str->html($val) : '(null)']; |
||||
228 | } |
||||
229 | |||||
230 | return compact('headers', 'details'); |
||||
231 | } |
||||
232 | } |
||||
233 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.