Issues (52)

src/Db/Facades/DatabaseFacade.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Facades;
4
5
use function array_filter;
6
use function array_intersect;
7
use function array_values;
8
use function is_array;
9
10
/**
11
 * Facade to database functions
12
 */
13
class DatabaseFacade extends AbstractFacade
14
{
15
    /**
16
     * The final schema list
17
     *
18
     * @var array|null
19
     */
20
    protected $finalSchemas = null;
21
22
    /**
23
     * The schemas the user has access to
24
     *
25
     * @var array|null
26
     */
27
    protected $userSchemas = null;
28
29
    /**
30
     * The constructor
31
     *
32
     * @param AbstractFacade $dbFacade
33
     * @param array $options    The server config options
34
     */
35
    public function __construct(AbstractFacade $dbFacade, array $options)
36
    {
37
        parent::__construct($dbFacade);
38
        // Set the user schemas, if defined.
39
        if (is_array(($userSchemas = $options['access']['schemas'] ?? null))) {
40
            $this->userSchemas = $userSchemas;
41
        }
42
    }
43
44
    /**
45
     * Get the schemas from the connected database
46
     *
47
     * @param bool $schemaAccess
48
     *
49
     * @return array
50
     */
51
    protected function schemas(bool $schemaAccess)
52
    {
53
        // Get the schema lists
54
        if ($this->finalSchemas === null) {
55
            $this->finalSchemas = $this->driver->schemas();
0 ignored issues
show
The method schemas() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            /** @scrutinizer ignore-call */ 
56
            $this->finalSchemas = $this->driver->schemas();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            if ($this->userSchemas !== null) {
57
                // Only keep schemas that appear in the config.
58
                $this->finalSchemas = array_values(array_intersect($this->finalSchemas, $this->userSchemas));
59
            }
60
        }
61
        return $schemaAccess ? $this->finalSchemas : array_filter($this->finalSchemas,
62
            fn($schema) => !$this->driver->isSystemSchema($schema));
63
    }
64
65
    /**
66
     * Connect to a database server
67
     *
68
     * @param bool $schemaAccess
69
     *
70
     * @return array
71
     */
72
    public function getDatabaseInfo(bool $schemaAccess)
73
    {
74
        // From db.inc.php
75
        $schemas = null;
76
        if ($this->driver->support("scheme")) {
77
            $schemas = $this->schemas($schemaAccess);
78
        }
79
        // $tables_list = $this->driver->tables();
80
81
        // $tables = [];
82
        // foreach($tableStatus as $table)
83
        // {
84
        //     $tables[] = $this->utils->str->html($table);
85
        // }
86
87
        return \compact('schemas'/*, 'tables'*/);
88
    }
89
90
    /**
91
     * Get the tables from a database server
92
     *
93
     * @return array
94
     */
95
    public function getTables()
96
    {
97
        $headers = [
98
            $this->utils->trans->lang('Table'),
99
            $this->utils->trans->lang('Engine'),
100
            $this->utils->trans->lang('Collation'),
101
            // $this->utils->trans->lang('Data Length'),
102
            // $this->utils->trans->lang('Index Length'),
103
            // $this->utils->trans->lang('Data Free'),
104
            // $this->utils->trans->lang('Auto Increment'),
105
            // $this->utils->trans->lang('Rows'),
106
            $this->utils->trans->lang('Comment'),
107
        ];
108
109
        // From db.inc.php
110
        // $tableStatus = $this->driver->tableStatuses(true); // Tables details
111
        $tableStatus = $this->driver->tableStatuses(); // Tables details
112
113
        $details = [];
114
        foreach ($tableStatus as $table => $status) {
115
            if (!$this->driver->isView($status)) {
116
                $details[] = [
117
                    'name' => $this->admin->tableName($status),
118
                    'engine' => $status->engine,
119
                    'collation' => '',
120
                    'comment' => $status->comment,
121
                ];
122
            }
123
        }
124
125
        return \compact('headers', 'details');
126
    }
127
128
    /**
129
     * Get the views from a database server
130
     * Almost the same as getTables()
131
     *
132
     * @return array
133
     */
134
    public function getViews()
135
    {
136
        $headers = [
137
            $this->utils->trans->lang('View'),
138
            $this->utils->trans->lang('Engine'),
139
            // $this->utils->trans->lang('Data Length'),
140
            // $this->utils->trans->lang('Index Length'),
141
            // $this->utils->trans->lang('Data Free'),
142
            // $this->utils->trans->lang('Auto Increment'),
143
            // $this->utils->trans->lang('Rows'),
144
            $this->utils->trans->lang('Comment'),
145
        ];
146
147
        // From db.inc.php
148
        // $tableStatus = $this->driver->tableStatuses(true); // Tables details
149
        $tableStatus = $this->driver->tableStatuses(); // Tables details
150
151
        $details = [];
152
        foreach ($tableStatus as $table => $status) {
153
            if ($this->driver->isView($status)) {
154
                $details[] = [
155
                    'name' => $this->admin->tableName($status),
156
                    'engine' => $status->engine,
157
                    'comment' => $status->comment,
158
                ];
159
            }
160
        }
161
162
        return \compact('headers', 'details');
163
    }
164
165
    /**
166
     * Get the routines from a given database
167
     *
168
     * @return array
169
     */
170
    public function getRoutines()
171
    {
172
        $headers = [
173
            $this->utils->trans->lang('Name'),
174
            $this->utils->trans->lang('Type'),
175
            $this->utils->trans->lang('Return type'),
176
        ];
177
178
        // From db.inc.php
179
        $routines = $this->driver->routines();
180
        $details = [];
181
        foreach ($routines as $routine) {
182
            // not computed on the pages to be able to print the header first
183
            // $name = ($routine["SPECIFIC_NAME"] == $routine["ROUTINE_NAME"] ?
184
            //     "" : "&name=" . urlencode($routine["ROUTINE_NAME"]));
185
186
            $details[] = [
187
                'name' => $this->utils->str->html($routine->name),
188
                'type' => $this->utils->str->html($routine->type),
189
                'returnType' => $this->utils->str->html($routine->dtd),
190
                // 'alter' => $this->utils->trans->lang('Alter'),
191
            ];
192
        }
193
194
        return \compact('headers', 'details');
195
    }
196
197
    /**
198
     * Get the routines from a given database
199
     *
200
     * @return array
201
     */
202
    public function getSequences()
203
    {
204
        $headers = [
205
            $this->utils->trans->lang('Name'),
206
        ];
207
208
        $details = [];
209
        foreach ($this->driver->sequences() as $sequence) {
210
            $details[] = [
211
                'name' => $this->utils->str->html($sequence),
212
            ];
213
        }
214
215
        return \compact('headers', 'details');
216
    }
217
218
    /**
219
     * Get the routines from a given database
220
     *
221
     * @return array
222
     */
223
    public function getUserTypes()
224
    {
225
        $headers = [
226
            $this->utils->trans->lang('Name'),
227
        ];
228
229
        // From db.inc.php
230
        $details = [];
231
        foreach ($this->driver->userTypes() as $userType) {
232
            $details[] = [
233
                'name' => $this->utils->str->html($userType),
234
            ];
235
        }
236
237
        return \compact('headers', 'details');
238
    }
239
240
    /**
241
     * Get the routines from a given database
242
     *
243
     * @return array
244
     */
245
    public function getEvents()
246
    {
247
        $headers = [
248
            $this->utils->trans->lang('Name'),
249
            $this->utils->trans->lang('Schedule'),
250
            $this->utils->trans->lang('Start'),
251
            // $this->utils->trans->lang('End'),
252
        ];
253
254
        // From db.inc.php
255
        $details = [];
256
        foreach ($this->driver->events() as $event) {
257
            $detail = [
258
                'name' => $this->utils->str->html($event["Name"]),
259
            ];
260
            if (($event["Execute at"])) {
261
                $detail['schedule'] = $this->utils->trans->lang('At given time');
262
                $detail['start'] = $event["Execute at"];
263
            // $detail['end'] = '';
264
            } else {
265
                $detail['schedule'] = $this->utils->trans->lang('Every') . " " .
266
                    $event["Interval value"] . " " . $event["Interval field"];
267
                $detail['start'] = $event["Starts"];
268
                // $detail['end'] = '';
269
            }
270
            $details[] = $detail;
271
        }
272
273
        return \compact('headers', 'details');
274
    }
275
}
276