Passed
Push — main ( e2b3b9...324b47 )
by Rafael
46:15
created

Config::loadConfig()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 61
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 38
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 61
rs 8.6897

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace DoliCore\Base;
20
21
use Conf;
22
use DoliCore\Lib\TraceableDB;
23
use DoliCore\Tools\Debug;
24
use DoliDB;
25
use DoliModules\User\Model\User;
26
use MenuManager;
27
use HookManager;
28
use stdClass;
29
use Alxarafe\Base\Config as ConfigBase;
30
31
require_once BASE_PATH . '/core/class/conf.class.php';
32
require_once BASE_PATH . '/core/class/hookmanager.class.php';
33
require_once BASE_PATH . '/../Dolibarr/Core/Menu/standard/eldy_menu.php';
34
35
/**
36
 * Generate an object with the configuration of the Dolibarr conf.php file.
37
 *
38
 * @info https://wiki.dolibarr.org/index.php/Configuration_file
39
 *
40
 * This class is only needed for compatibility with Dolibarr.
41
 *
42
 * @package DoliCore\Base
43
 */
44
abstract class Config extends ConfigBase
45
{
46
    const DEFAULT_DB_PREFIX = 'alx_';
47
48
    /**
49
     * Contains the information of the old $conf global var.
50
     *
51
     * Config::getConf() can be used at any point to retrieve the contents of the
52
     * $conf variable used globally by Dolibarr.
53
     *
54
     * The content of the variable is saved with the first call and this copy is
55
     * returned. If it is necessary to regenerate it, the parameter true can be
56
     * passed to it.
57
     *
58
     * @var null|Conf
59
     *
60
     * @deprecated Use $config instead
61
     */
62
    private static $dolibarrConfig = null;
63
64
    /**
65
     * Contains the information from the conf.php file in a normalized stdClass.
66
     *
67
     * The objective is to move what is really needed to this object and update the
68
     * configuration file to a data file outside of public space.
69
     *
70
     * @var null|stdClass
71
     */
72
    private static $config = null;
73
74
    /**
75
     * Contains a DoliDB connection.
76
     *
77
     * @var DoliDB, null
78
     */
79
    private static $db;
80
81
    /**
82
     * Contains a HookManager class.
83
     *
84
     * @var $hookManager
0 ignored issues
show
Documentation Bug introduced by
The doc comment $hookManager at position 0 could not be parsed: Unknown type name '$hookManager' at position 0 in $hookManager.
Loading history...
85
     */
86
    private static $hookManager;
87
88
    /**
89
     * Contains a Translate class
90
     *
91
     * @var Translate
92
     */
93
    private static $langs;
94
95
    /**
96
     * Contains a User class instance.
97
     *
98
     * @var User
99
     */
100
    private static $user;
101
102
    private static $menumanager;
103
104
    /**
105
     * Load the Dolibarr configuration file and enter the content for the Dolibarr global
106
     * variable $conf.
107
     *
108
     * The result is cached for future queries. If we want to reload the configuration file
109
     * we will have to pass the parameter true.
110
     *
111
     * @param $reload
112
     *
113
     * @return stdClass|null
114
     */
115
    public static function getConf($reload = false): ?stdClass
116
    {
117
        if ($reload || !isset(static::$dolibarrConfig)) {
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
118
            static::$dolibarrConfig = static::loadConf();
0 ignored issues
show
Deprecated Code introduced by
The function DoliCore\Base\Config::loadConf() has been deprecated: Use loadConfig() instead! ( Ignorable by Annotation )

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

118
            static::$dolibarrConfig = /** @scrutinizer ignore-deprecated */ static::loadConf();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
        }
120
121
        return static::$dolibarrConfig;
122
    }
123
124
    /**
125
     * Load the configuration file and return the content that the $conf variable
126
     * used globally by Dolibarr should have.
127
     *
128
     * @return Conf|null
129
     *
130
     * @deprecated Use loadConfig() instead!
131
     */
132
    private static function loadConf()
133
    {
134
        $filename = static::getDolibarrConfigFilename();
135
        $exists = file_exists($filename) && is_readable($filename);
136
        if ($exists) {
137
            include $filename;
138
        }
139
140
        /*
141
         * Create $conf object
142
         */
143
        $conf = new Conf();
144
145
        // Set properties specific to database
146
        $conf->db->host = empty($dolibarr_main_db_host) ? '' : $dolibarr_main_db_host;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_host seems to never exist and therefore empty should always be true.
Loading history...
147
        $conf->db->port = empty($dolibarr_main_db_port) ? '' : $dolibarr_main_db_port;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_port seems to never exist and therefore empty should always be true.
Loading history...
148
        $conf->db->name = empty($dolibarr_main_db_name) ? '' : $dolibarr_main_db_name;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_name seems to never exist and therefore empty should always be true.
Loading history...
149
        $conf->db->user = empty($dolibarr_main_db_user) ? '' : $dolibarr_main_db_user;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_user seems to never exist and therefore empty should always be true.
Loading history...
150
        $conf->db->pass = empty($dolibarr_main_db_pass) ? '' : $dolibarr_main_db_pass;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_pass seems to never exist and therefore empty should always be true.
Loading history...
151
        $conf->db->type = $dolibarr_main_db_type ?? 'mysqli';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_type seems to never exist and therefore isset should always be false.
Loading history...
152
        $conf->db->prefix = $dolibarr_main_db_prefix ?? self::DEFAULT_DB_PREFIX;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_prefix seems to never exist and therefore isset should always be false.
Loading history...
153
        $conf->db->charset = $dolibarr_main_db_character_set ?? 'utf8';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_character_set seems to never exist and therefore isset should always be false.
Loading history...
154
        $conf->db->collation = $dolibarr_main_db_collation ?? 'utf8_general_ci';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_collation seems to never exist and therefore isset should always be false.
Loading history...
155
        $conf->db->encryption = $dolibarr_main_db_encryption ?? 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_encryption seems to never exist and therefore isset should always be false.
Loading history...
156
        $conf->db->cryptkey = $dolibarr_main_db_cryptkey ?? '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_db_cryptkey seems to never exist and therefore isset should always be false.
Loading history...
157
        if (defined('TEST_DB_FORCE_TYPE')) {
158
            $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
159
        }
160
161
        // Set properties specific to conf file
162
        $conf->file->main_limit_users = $dolibarr_main_limit_users ?? null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_limit_users seems to never exist and therefore isset should always be false.
Loading history...
163
        $conf->file->mailing_limit_sendbyweb = empty($dolibarr_mailing_limit_sendbyweb) ? 0 : $dolibarr_mailing_limit_sendbyweb;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbyweb seems to never exist and therefore empty should always be true.
Loading history...
164
        $conf->file->mailing_limit_sendbycli = empty($dolibarr_mailing_limit_sendbycli) ? 0 : $dolibarr_mailing_limit_sendbycli;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbycli seems to never exist and therefore empty should always be true.
Loading history...
165
        $conf->file->mailing_limit_sendbyday = empty($dolibarr_mailing_limit_sendbyday) ? 0 : $dolibarr_mailing_limit_sendbyday;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_mailing_limit_sendbyday seems to never exist and therefore empty should always be true.
Loading history...
166
        $conf->file->main_authentication = empty($dolibarr_main_authentication) ? 'dolibarr' : $dolibarr_main_authentication; // Identification mode
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_authentication seems to never exist and therefore empty should always be true.
Loading history...
167
        $conf->file->main_force_https = empty($dolibarr_main_force_https) ? '' : $dolibarr_main_force_https; // Force https
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_force_https seems to never exist and therefore empty should always be true.
Loading history...
168
        $conf->file->strict_mode = empty($dolibarr_strict_mode) ? '' : $dolibarr_strict_mode; // Force php strict mode (for debug)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_strict_mode seems to never exist and therefore empty should always be true.
Loading history...
169
        $conf->file->instance_unique_id = empty($dolibarr_main_instance_unique_id) ? (empty($dolibarr_main_cookie_cryptkey) ? '' : $dolibarr_main_cookie_cryptkey) : $dolibarr_main_instance_unique_id; // Unique id of instance
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_cookie_cryptkey seems to never exist and therefore empty should always be true.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_instance_unique_id seems to never exist and therefore empty should always be true.
Loading history...
170
        $conf->file->main_path = empty($dolibarr_main_document_root ?? '') ? BASE_PATH : $dolibarr_main_document_root;  // Define htdocs path inside the config file
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root seems to never exist and therefore isset should always be false.
Loading history...
171
        $conf->file->main_url = empty($dolibarr_main_url_root ?? '') ? BASE_URL : $dolibarr_main_url_root;    // Define url inside the config file
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root seems to never exist and therefore isset should always be false.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root seems to be never defined.
Loading history...
172
        $conf->file->main_doc = empty($dolibarr_main_data_root ?? '') ? static::getDataDir($conf->file->main_path) : $dolibarr_main_data_root;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root seems to never exist and therefore isset should always be false.
Loading history...
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_data_root seems to be never defined.
Loading history...
173
        $conf->file->path = ['main' => $conf->file->main_path];
174
        $conf->file->url = ['main' => '/'];
175
        if (!empty($dolibarr_main_document_root_alt)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_document_root_alt seems to never exist and therefore empty should always be true.
Loading history...
176
            $path = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
177
            $url = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_url_root_alt seems to be never defined.
Loading history...
178
179
            if (count($path) !== count($url)) {
180
                print '<b>Error:</b><br>$dolibarr_main_document_root_alt and $dolibarr_main_url_root_alt must contain the same number of elements.<br>';
181
                die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
182
            }
183
184
            $i = 0;
185
            foreach ($path as $value) {
186
                $conf->file->path['alt' . ($i++)] = (string)$value;
187
            }
188
            $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
189
            $i = 0;
190
            foreach ($url as $value) {
191
                if (preg_match('/^http(s)?:/', $value)) {
192
                    // Show error message
193
                    $correct_value = str_replace($conf->file->url, '', $value);
194
                    print '<b>Error:</b><br>' . "\n";
195
                    print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>' . "\n";
196
                    print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>' . "\n";
197
                    print 'Value found: ' . $value . '<br>' . "\n";
198
                    print 'Should be replaced by: ' . $correct_value . '<br>' . "\n";
199
                    print "Or something like following examples:<br>\n";
200
                    print "\"/extensions\"<br>\n";
201
                    print "\"/extensions1,/extensions2,...\"<br>\n";
202
                    print "\"/../extensions\"<br>\n";
203
                    print "\"/custom\"<br>\n";
204
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
205
                }
206
                $conf->file->url['alt' . ($i++)] = (string)$value;
207
            }
208
        }
209
210
        $conf->file->theme = $dolibarr_main_theme ?? 'eldy';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_theme seems to never exist and therefore isset should always be false.
Loading history...
211
        $conf->file->dol_main_stream_to_disable = $dolibarr_main_stream_to_disable ?? null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_stream_to_disable seems to never exist and therefore isset should always be false.
Loading history...
212
        $conf->debug = intval($dolibarr_main_prod ?? 1) === 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_prod seems to never exist and therefore isset should always be false.
Loading history...
213
214
        // Load the main includes of common libraries
215
        if (!defined('NOREQUIRETRAN')) {
216
            require_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php';
217
        }
218
219
        static::$dolibarrConfig = $conf;
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
220
        return $conf;
221
    }
222
223
    /**
224
     * Returns the Dolibarr conf.php complete path.
225
     *
226
     * @return string
227
     */
228
    public static function getDolibarrConfigFilename()
229
    {
230
        return BASE_PATH . '/conf/conf.php';
231
    }
232
233
    /**
234
     * Simply replace /htdocs with /documents in $pathDir
235
     *
236
     * @param $pathDir
237
     *
238
     * @return string
239
     */
240
    public static function getDataDir($pathDir)
241
    {
242
        return preg_replace("/\/htdocs$/", "", $pathDir) . '/documents';
243
    }
244
245
    /**
246
     * Returns a stdClass with the information contained in the conf.php file.
247
     *
248
     * @param $reload
249
     *
250
     * @return stdClass|null
251
     */
252
    public static function getConfig($reload = false): ?stdClass
253
    {
254
        if ($reload || !isset(static::$config)) {
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $config to at least protected.
Loading history...
255
            static::$config = static::loadConfig();
256
        }
257
258
        return static::$config;
259
    }
260
261
    /**
262
     * Returns a normalized config file.
263
     *
264
     * @param bool $reload
265
     * @return stdClass|null
266
     */
267
    public static function loadConfig(bool $reload = false): stdClass
268
    {
269
        $conf = parent::loadConfig($reload);
270
        if ($conf) {
271
            return $conf;
272
        }
273
274
        $conf = static::loadConf();
275
        if (empty($conf)) {
276
            return null;
277
        }
278
279
        $config = new stdClass();
280
281
        // 'main' section
282
        $config->main = new stdClass();
283
        $config->main->path = $conf->file->main_path ?? constant('BASE_PATH');
284
        $config->main->url = $conf->file->main_url ?? constant('BASE_URL');
285
        $config->main->data_path = $conf->file->main_doc ?? '';
286
        $config->main->alt_base_path = $conf->file->path;
287
        $config->main->alt_base_url = $conf->file->url;
288
        $config->main->theme = $conf->file->theme;
289
290
        // 'db' section
291
        $config->db = $conf->db;
292
        $config->db->charset = $conf->db->charset;
293
        $config->db->collation = $conf->db->collation;
294
        $config->db->encryption = $conf->db->encryption;
295
        $config->db->cryptkey = $conf->db->cryptkey;
296
297
        // 'security' section
298
        $config->security = new stdClass();
299
        $config->security->authentication_type = $conf->file->main_authentication;
300
        $config->security->force_https = $conf->file->main_force_https;
301
        $config->security->unique_id = $conf->file->instance_unique_id;
302
        $config->security->stream_to_disable = null;
303
        if (is_array($conf->file->dol_main_stream_to_disable)) {
304
            $config->security->stream_to_disable = $conf->file->dol_main_stream_to_disable;
305
        }
306
307
        $config->file = $conf->file;
308
309
        // Others
310
        $demo = $dolibarr_main_demo ?? false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dolibarr_main_demo seems to never exist and therefore isset should always be false.
Loading history...
311
        if ($demo !== false) {
312
            $credentials = explode(',', $demo);
313
            if (count($credentials) === 2) {
314
                $config->demo->user = trim($credentials[0]);
315
                $config->demo->pass = trim($credentials[1]);
316
            }
317
        }
318
319
        $config->debug = $conf->debug;
320
321
        // 'Server' section
322
        $config->server = new stdClass();
323
        $config->server->detailed_info = !empty($_SERVER['MAIN_SHOW_TUNING_INFO']);
324
325
        static::$config = $config;
326
327
        return $config;
328
    }
329
330
    /**
331
     * Returns a DoliDB connection instance.
332
     *
333
     * @return DoliDB|null
334
     */
335
    public static function getDb(): ?DoliDB
336
    {
337
        return static::$db;
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
338
    }
339
340
    /**
341
     * Fills in the additional data of the $conf variable, taken once the database
342
     * is initialized
343
     *
344
     * @param $conf
345
     */
346
    public static function setConfigValues($conf)
347
    {
348
        // Here we read database (llx_const table) and define conf var $conf->global->XXX.
349
        // print "We work with data into entity instance number '".$conf->entity."'";
350
        $conf->setValues(static::$db);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
351
    }
352
353
    /**
354
     * Returns a TraceableDB connection instance.
355
     *
356
     * @return TraceableDB|null
357
     */
358
    public static function debugDb(): ?TraceableDB
359
    {
360
        if (isModEnabled('debugbar')) {
361
            static::$db = new TraceableDB(static::$db);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
362
        }
363
        return static::$db;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::db returns the type DoliDB which includes types incompatible with the type-hinted return DoliCore\Lib\TraceableDB|null.
Loading history...
364
    }
365
366
    /**
367
     * Returns a HookManager class instance.
368
     *
369
     * @return HookManager|null
370
     */
371
    public static function getHookManager(): ?HookManager
372
    {
373
        if (empty(static::$hookManager)) {
0 ignored issues
show
Bug introduced by
Since $hookManager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hookManager to at least protected.
Loading history...
374
            static::$hookManager = static::loadHookManager();
375
        }
376
        return static::$hookManager;
377
    }
378
379
    /**
380
     * Returns a HookManager class instance.
381
     *
382
     * @return mixed
383
     */
384
    private static function loadHookManager()
385
    {
386
        static::$hookManager = new HookManager(static::$db);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
Bug introduced by
Since $hookManager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $hookManager to at least protected.
Loading history...
387
        return static::$hookManager;
388
    }
389
390
    /**
391
     * Returns a Translate class instance.
392
     *
393
     * @return Translate|null
394
     */
395
    public static function getLangs(): ?Translate
396
    {
397
        if (empty(static::$langs)) {
0 ignored issues
show
Bug introduced by
Since $langs is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $langs to at least protected.
Loading history...
398
            static::$langs = static::loadLangs();
399
        }
400
        return static::$langs;
401
    }
402
403
    /**
404
     * Returns a Translate class instance.
405
     *
406
     * @return Translate
407
     */
408
    private static function loadLangs()
409
    {
410
        static::$langs = new Translate('', static::$dolibarrConfig);
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
Bug introduced by
Since $langs is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $langs to at least protected.
Loading history...
411
        return static::$langs;
412
    }
413
414
    /**
415
     * Returns a User class instance.
416
     *
417
     * @return User|null
418
     */
419
    public static function getUser(): ?User
420
    {
421
        if (empty(static::$user)) {
0 ignored issues
show
Bug introduced by
Since $user is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $user to at least protected.
Loading history...
422
            static::$user = static::loadUser();
423
        }
424
        return static::$user;
425
    }
426
427
    private static function loadUser()
428
    {
429
        static::$user = new User(static::$db);
0 ignored issues
show
Bug introduced by
Since $user is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $user to at least protected.
Loading history...
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
430
        return static::$user;
431
    }
432
433
    public static function getMenuManager($conf)
434
    {
435
        if (empty(static::$menumanager)) {
0 ignored issues
show
Bug introduced by
Since $menumanager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $menumanager to at least protected.
Loading history...
436
            static::$menumanager = static::loadMenuManager();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to static::menumanager is correct as static::loadMenuManager() targeting DoliCore\Base\Config::loadMenuManager() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
437
        }
438
        return static::$menumanager;
439
    }
440
441
    private static function loadMenuManager()
442
    {
443
        $conf = static::$dolibarrConfig;
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
444
        $db = static::$db;
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
445
446
        $menumanager = null;
447
        if (!defined('NOREQUIREMENU')) {
448
            if (empty($user->socid)) {    // If internal user or not defined
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
449
                $conf->standard_menu = (!getDolGlobalString('MAIN_MENU_STANDARD_FORCED') ? (!getDolGlobalString('MAIN_MENU_STANDARD') ? 'eldy_menu.php' : $conf->global->MAIN_MENU_STANDARD) : $conf->global->MAIN_MENU_STANDARD_FORCED);
450
            } else {
451
                // If external user
452
                $conf->standard_menu = (!getDolGlobalString('MAIN_MENUFRONT_STANDARD_FORCED') ? (!getDolGlobalString('MAIN_MENUFRONT_STANDARD') ? 'eldy_menu.php' : $conf->global->MAIN_MENUFRONT_STANDARD) : $conf->global->MAIN_MENUFRONT_STANDARD_FORCED);
453
            }
454
455
            // Load the menu manager (only if not already done)
456
            $file_menu = $conf->standard_menu;
457
            if (GETPOST('menu', 'alpha')) {
458
                $file_menu = GETPOST('menu', 'alpha'); // example: menu=eldy_menu.php
459
            }
460
            if (!class_exists('MenuManager')) {
461
                $menufound = 0;
462
                $dirmenus = array_merge(["/core/menus/"], (array)$conf->modules_parts['menus']);
463
                foreach ($dirmenus as $dirmenu) {
464
                    $menufound = dol_include_once($dirmenu . "standard/" . $file_menu);
465
                    if (class_exists('MenuManager')) {
466
                        break;
467
                    }
468
                }
469
                if (!class_exists('MenuManager')) { // If failed to include, we try with standard eldy_menu.php
470
                    dol_syslog("You define a menu manager '" . $file_menu . "' that can not be loaded.", LOG_WARNING);
471
                    $file_menu = 'eldy_menu.php';
472
                    include_once DOL_DOCUMENT_ROOT . "/core/menus/standard/" . $file_menu;
473
                }
474
            }
475
            $menumanager = new MenuManager($db, empty($user->socid) ? 0 : 1);
476
            $menumanager->loadMenu();
477
        }
478
479
        static::$menumanager = $menumanager;
0 ignored issues
show
Bug introduced by
Since $menumanager is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $menumanager to at least protected.
Loading history...
480
    }
481
482
    /**
483
     * Load all Dolibar global variables.
484
     *
485
     * @return false|void
486
     * @throws \DebugBar\DebugBarException
487
     */
488
    public static function load(): bool
489
    {
490
        global $conf;
491
        global $config;
492
        global $db;
493
        global $hookmanager;
494
        global $langs;
495
        global $user;
496
        global $menumanager;
497
498
        $config = parent::loadConfig();
499
        $conf = static::$dolibarrConfig = static::loadConf();
0 ignored issues
show
Deprecated Code introduced by
The function DoliCore\Base\Config::loadConf() has been deprecated: Use loadConfig() instead! ( Ignorable by Annotation )

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

499
        $conf = static::$dolibarrConfig = /** @scrutinizer ignore-deprecated */ static::loadConf();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
500
        if (!isset($config->db) || empty($conf->db->name ?? '')) {
501
            return false;
502
        }
503
504
        $config = static::$config = static::loadConfig();
505
        $db = static::$db = static::loadDb();
506
        $hookmanager = static::$hookManager = static::loadHookManager();
507
        $langs = static::$langs = static::loadLangs();
508
        $user = static::$user = static::loadUser();
509
        if ($user->id > 0) {
510
            $menumanager = static::$menumanager = static::loadMenuManager();
511
        }
512
        Debug::load();
513
514
        new Database($config->db);
515
516
        // TODO: Example of calling a SELECT from Eloquent and from Dolibarr
517
        // DB::select('SELECT * FROM alx_user'); // use Illuminate\Database\Capsule\Manager as DB;
518
        // $db->query('SELECT * FROM alx_user');
519
520
        return true;
521
    }
522
523
    /**
524
     * Returns a Dolibarr DB connection (DoliDB) instance.
525
     *
526
     * @return DoliDb
527
     * @throws \Exception
528
     */
529
    private static function loadDb()
530
    {
531
        $conf = static::$dolibarrConfig;
0 ignored issues
show
Bug introduced by
Since $dolibarrConfig is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $dolibarrConfig to at least protected.
Loading history...
532
        static::$db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int)$conf->db->port);
0 ignored issues
show
Bug introduced by
Since $db is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $db to at least protected.
Loading history...
533
        static::$dolibarrConfig->setValues(static::$db);
0 ignored issues
show
Bug introduced by
The method setValues() 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

533
        static::$dolibarrConfig->/** @scrutinizer ignore-call */ 
534
                                 setValues(static::$db);

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...
534
535
        return static::$db;
536
    }
537
}
538