1 | <?php |
||||
2 | |||||
3 | namespace BPT\database; |
||||
4 | |||||
5 | use BPT\constants\dbTypes; |
||||
6 | use BPT\constants\loggerTypes; |
||||
7 | use BPT\exception\bptException; |
||||
8 | use BPT\logger; |
||||
9 | use BPT\settings; |
||||
10 | |||||
11 | /** |
||||
12 | * db class , for manage and handling databases |
||||
13 | */ |
||||
14 | class db { |
||||
15 | private static bool $active = false; |
||||
16 | |||||
17 | /** |
||||
18 | * @internal Only for BPT self usage , Don't use it in your source! |
||||
19 | */ |
||||
20 | public static function init (): void { |
||||
21 | if (!isset(settings::$db['type'])) { |
||||
22 | settings::$db['type'] = dbTypes::JSON; |
||||
23 | } |
||||
24 | switch (settings::$db['type']) { |
||||
25 | case dbTypes::JSON: |
||||
26 | $settings = [ |
||||
27 | 'bot_name' => settings::$name, |
||||
28 | 'global' => settings::$db['global'] ?? null, |
||||
29 | 'user' => settings::$db['user'] ?? null, |
||||
30 | 'group_user' => settings::$db['group_user'] ?? null, |
||||
31 | 'group' => settings::$db['group'] ?? null, |
||||
32 | 'supergroup' => settings::$db['supergroup'] ?? null, |
||||
33 | 'channel' => settings::$db['channel'] ?? null, |
||||
34 | ]; |
||||
35 | json::init(...array_filter($settings, fn ($value) => $value !== null)); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
36 | break; |
||||
37 | case dbTypes::MYSQL: |
||||
38 | $settings = [ |
||||
39 | 'host' => settings::$db['host'] ?? null, |
||||
40 | 'username' => settings::$db['username'] ?? settings::$db['user'] ?? null, |
||||
41 | 'password' => settings::$db['password'] ?? settings::$db['pass'] ?? null, |
||||
42 | 'dbname' => settings::$db['dbname'] ?? null, |
||||
43 | 'auto_process' => settings::$db['auto_process'] ?? null, |
||||
44 | 'port' => settings::$db['port'] ?? null, |
||||
45 | 'auto_load' => settings::$db['auto_load'] ?? null, |
||||
46 | ]; |
||||
47 | mysql::init(...array_filter($settings, fn ($value) => $value !== null)); |
||||
0 ignored issues
–
show
array_filter($settings, ...ion(...) { /* ... */ }) is expanded, but the parameter $host of BPT\database\mysql::init() does not expect variable arguments.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
48 | break; |
||||
49 | default: |
||||
50 | logger::write('DB type is wrong', loggerTypes::ERROR); |
||||
51 | throw new bptException('DB_TYPE_WRONG'); |
||||
52 | } |
||||
53 | self::$active = true; |
||||
54 | } |
||||
55 | |||||
56 | /** |
||||
57 | * @internal Only for BPT self usage , Don't use it in your source! |
||||
58 | */ |
||||
59 | public static function process(): void { |
||||
60 | if (self::$active) { |
||||
61 | switch (settings::$db['type']) { |
||||
62 | case dbTypes::JSON: |
||||
63 | json::process(); |
||||
64 | break; |
||||
65 | case dbTypes::MYSQL: |
||||
66 | mysql::process(); |
||||
67 | break; |
||||
68 | } |
||||
69 | } |
||||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * @internal Only for BPT self usage , Don't use it in your source! |
||||
74 | */ |
||||
75 | public static function save(): void { |
||||
76 | if (self::$active && settings::$db['type'] === dbTypes::JSON) { |
||||
77 | json::save(); |
||||
78 | } |
||||
79 | } |
||||
80 | } |