Passed
Push — main ( baad3c...521dc8 )
by Thierry
01:50
created

Package::getOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin;
4
5
use Jaxon\Plugin\Package as JaxonPackage;
6
use Lagdo\DbAdmin\App\Ajax\Server;
7
8
use function jaxon;
9
10
/**
11
 * Adminer package
12
 */
13
class Package extends JaxonPackage
14
{
15
    /**
16
     * The constructor
17
     */
18
    public function __construct()
19
    {
20
        jaxon()->callback()->boot(function() {
21
            $template = $this->getConfig()->getOption('template', 'bootstrap3');
22
            jaxon()->template()->pagination(__DIR__ . "/../templates/views/$template/pagination/");
23
        });
24
    }
25
26
    /**
27
     * Get the div id of the HTML element
28
     *
29
     * @return string
30
     */
31
    public function getContainerId()
32
    {
33
        return 'adminer';
34
    }
35
36
    /**
37
     * Get the div id of the HTML element
38
     *
39
     * @return string
40
     */
41
    public function getUserInfoId()
42
    {
43
        return 'adminer-user-info';
44
    }
45
46
    /**
47
     * Get the div id of the HTML element
48
     *
49
     * @return string
50
     */
51
    public function getServerInfoId()
52
    {
53
        return 'adminer-server-info';
54
    }
55
56
    /**
57
     * Get the div id of the HTML element
58
     *
59
     * @return string
60
     */
61
    public function getBreadcrumbsId()
62
    {
63
        return 'adminer-breadcrumbs';
64
    }
65
66
    /**
67
     * Get the div id of the HTML element
68
     *
69
     * @return string
70
     */
71
    public function getMainActionsId()
72
    {
73
        return 'adminer-main-actions';
74
    }
75
76
    /**
77
     * Get the div id of the HTML element
78
     *
79
     * @return string
80
     */
81
    public function getServerActionsId()
82
    {
83
        return 'adminer-server-actions';
84
    }
85
86
    /**
87
     * Get the div id of the HTML element
88
     *
89
     * @return string
90
     */
91
    public function getDbListId()
92
    {
93
        return 'adminer-database-list';
94
    }
95
96
    /**
97
     * Get the div id of the HTML element
98
     *
99
     * @return string
100
     */
101
    public function getSchemaListId()
102
    {
103
        return 'adminer-schema-list';
104
    }
105
106
    /**
107
     * Get the div id of the HTML element
108
     *
109
     * @return string
110
     */
111
    public function getDbMenuId()
112
    {
113
        return 'adminer-database-menu';
114
    }
115
116
    /**
117
     * Get the div id of the HTML element
118
     *
119
     * @return string
120
     */
121
    public function getDbActionsId()
122
    {
123
        return 'adminer-database-actions';
124
    }
125
126
    /**
127
     * Get the div id of the HTML element
128
     *
129
     * @return string
130
     */
131
    public function getDbContentId()
132
    {
133
        return 'adminer-database-content';
134
    }
135
136
    /**
137
     * Get all the ids
138
     *
139
     * @return array
140
     */
141
    public function getIds()
142
    {
143
        return [
144
            'containerId' => $this->getContainerId(),
145
            'userInfoId' => $this->getUserInfoId(),
146
            'serverInfoId' => $this->getServerInfoId(),
147
            'breadcrumbsId' => $this->getBreadcrumbsId(),
148
            'mainActionsId' => $this->getMainActionsId(),
149
            'serverActionsId' => $this->getServerActionsId(),
150
            'dbListId' => $this->getDbListId(),
151
            'schemaListId' => $this->getSchemaListId(),
152
            'dbMenuId' => $this->getDbMenuId(),
153
            'dbActionsId' => $this->getDbActionsId(),
154
            'dbContentId' => $this->getDbContentId(),
155
        ];
156
    }
157
158
    /**
159
     * Get the path to the config file
160
     *
161
     * @return string
162
     */
163
    public static function getConfigFile()
164
    {
165
        return realpath(__DIR__ . '/../config/config.php');
166
    }
167
168
    /**
169
     * Get a given server options
170
     *
171
     * @param string $server    The server name in the configuration
172
     *
173
     * @return array
174
     */
175
    public function getServerOptions($server)
176
    {
177
        return $this->getConfig()->getOption("servers.$server", []);
178
    }
179
180
    /**
181
     * Get the driver of a given server
182
     *
183
     * @param string $server    The server name in the configuration
184
     *
185
     * @return string
186
     */
187
    public function getServerDriver($server)
188
    {
189
        return $this->getConfig()->getOption("servers.$server.driver", '');
190
    }
191
192
    /**
193
     * Get the value of a given package option
194
     *
195
     * @param string $option    The option name
196
     * @param mixed  $default   The default value
197
     *
198
     * @return mixed
199
     */
200
    public function getOption($option, $default = null)
201
    {
202
        return $this->getConfig()->getOption($option, $default);
203
    }
204
205
    /**
206
     * Get the default server to connect to
207
     *
208
     * @return string
209
     */
210
    private function getDefaultServer()
211
    {
212
        $servers = $this->getConfig()->getOption('servers', []);
213
        $default = $this->getConfig()->getOption('default', '');
214
        if(\array_key_exists($default, $servers))
215
        {
216
            return $default;
217
        }
218
        // if(\count($servers) > 0)
219
        // {
220
        //     return $servers[0];
221
        // }
222
        return '';
223
    }
224
225
    /**
226
     * Get the HTML tags to include CSS code and files into the page
227
     *
228
     * The code must be enclosed in the appropriate HTML tags.
229
     *
230
     * @return string
231
     */
232
    public function getCss()
233
    {
234
        return $this->view()->render('adminer::codes::css', $this->getIds()) .
235
            "\n" . $this->view()->render('adminer::views::styles', $this->getIds());
236
    }
237
238
    /**
239
     * Get the HTML tags to include javascript code and files into the page
240
     *
241
     * The code must be enclosed in the appropriate HTML tags.
242
     *
243
     * @return string
244
     */
245
    public function getJs()
246
    {
247
        return $this->view()->render('adminer::codes::js', $this->getIds());
248
    }
249
250
    /**
251
     * Get the javascript code to include into the page
252
     *
253
     * The code must NOT be enclosed in HTML tags.
254
     *
255
     * @return string
256
     */
257
    public function getScript()
258
    {
259
        return $this->view()->render('adminer::codes::script', $this->getIds());
260
    }
261
262
    /**
263
     * Get the javascript code to execute after page load
264
     *
265
     * @return string
266
     */
267
    public function getReadyScript()
268
    {
269
        if(!($server = $this->getDefaultServer()))
270
        {
271
            return '';
272
        }
273
        return jaxon()->request(Server::class)->connect($server);
274
    }
275
276
    /**
277
     * Get the HTML code of the package home page
278
     *
279
     * @return string
280
     */
281
    public function getHtml()
282
    {
283
        // Add an HTML container block for each server in the config file
284
        $servers = $this->getConfig()->getOption('servers', []);
285
        \array_walk($servers, function(&$server) {
286
            $server = $server['name'];
287
        });
288
289
        $connect = \jaxon()->request(Server::class)->connect(\pm()->select('adminer-dbhost-select'));
290
291
        return $this->view()->render('adminer::views::home', $this->getIds())
292
            ->with('connect', $connect)
293
            ->with('servers', $servers)
294
            ->with('default', $this->getConfig()->getOption('default', ''));
295
    }
296
}
297