Total Complexity | 73 |
Total Lines | 492 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
|
|||
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)) { |
||
118 | static::$dolibarrConfig = static::loadConf(); |
||
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; |
||
147 | $conf->db->port = empty($dolibarr_main_db_port) ? '' : $dolibarr_main_db_port; |
||
148 | $conf->db->name = empty($dolibarr_main_db_name) ? '' : $dolibarr_main_db_name; |
||
149 | $conf->db->user = empty($dolibarr_main_db_user) ? '' : $dolibarr_main_db_user; |
||
150 | $conf->db->pass = empty($dolibarr_main_db_pass) ? '' : $dolibarr_main_db_pass; |
||
151 | $conf->db->type = $dolibarr_main_db_type ?? 'mysqli'; |
||
152 | $conf->db->prefix = $dolibarr_main_db_prefix ?? self::DEFAULT_DB_PREFIX; |
||
153 | $conf->db->charset = $dolibarr_main_db_character_set ?? 'utf8'; |
||
154 | $conf->db->collation = $dolibarr_main_db_collation ?? 'utf8_general_ci'; |
||
155 | $conf->db->encryption = $dolibarr_main_db_encryption ?? 0; |
||
156 | $conf->db->cryptkey = $dolibarr_main_db_cryptkey ?? ''; |
||
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; |
||
163 | $conf->file->mailing_limit_sendbyweb = empty($dolibarr_mailing_limit_sendbyweb) ? 0 : $dolibarr_mailing_limit_sendbyweb; |
||
164 | $conf->file->mailing_limit_sendbycli = empty($dolibarr_mailing_limit_sendbycli) ? 0 : $dolibarr_mailing_limit_sendbycli; |
||
165 | $conf->file->mailing_limit_sendbyday = empty($dolibarr_mailing_limit_sendbyday) ? 0 : $dolibarr_mailing_limit_sendbyday; |
||
166 | $conf->file->main_authentication = empty($dolibarr_main_authentication) ? 'dolibarr' : $dolibarr_main_authentication; // Identification mode |
||
167 | $conf->file->main_force_https = empty($dolibarr_main_force_https) ? '' : $dolibarr_main_force_https; // Force https |
||
168 | $conf->file->strict_mode = empty($dolibarr_strict_mode) ? '' : $dolibarr_strict_mode; // Force php strict mode (for debug) |
||
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 |
||
170 | $conf->file->main_path = empty($dolibarr_main_document_root ?? '') ? BASE_PATH : $dolibarr_main_document_root; // Define htdocs path inside the config file |
||
171 | $conf->file->main_url = empty($dolibarr_main_url_root ?? '') ? BASE_URL : $dolibarr_main_url_root; // Define url inside the config file |
||
172 | $conf->file->main_doc = empty($dolibarr_main_data_root ?? '') ? static::getDataDir($conf->file->main_path) : $dolibarr_main_data_root; |
||
173 | $conf->file->path = ['main' => $conf->file->main_path]; |
||
174 | $conf->file->url = ['main' => '/']; |
||
175 | if (!empty($dolibarr_main_document_root_alt)) { |
||
176 | $path = preg_split('/[;,]/', $dolibarr_main_document_root_alt); |
||
177 | $url = preg_split('/[;,]/', $dolibarr_main_url_root_alt); |
||
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(); |
||
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; |
||
205 | } |
||
206 | $conf->file->url['alt' . ($i++)] = (string)$value; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | $conf->file->theme = $dolibarr_main_theme ?? 'eldy'; |
||
211 | $conf->file->dol_main_stream_to_disable = $dolibarr_main_stream_to_disable ?? null; |
||
212 | $conf->debug = intval($dolibarr_main_prod ?? 1) === 0; |
||
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; |
||
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) |
||
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 |
||
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; |
||
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 |
||
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); |
||
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); |
||
362 | } |
||
363 | return static::$db; |
||
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)) { |
||
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); |
||
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)) { |
||
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); |
||
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)) { |
||
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); |
||
430 | return static::$user; |
||
431 | } |
||
432 | |||
433 | public static function getMenuManager($conf) |
||
434 | { |
||
435 | if (empty(static::$menumanager)) { |
||
436 | static::$menumanager = static::loadMenuManager(); |
||
437 | } |
||
438 | return static::$menumanager; |
||
439 | } |
||
440 | |||
441 | private static function loadMenuManager() |
||
442 | { |
||
443 | $conf = static::$dolibarrConfig; |
||
444 | $db = static::$db; |
||
445 | |||
446 | $menumanager = null; |
||
447 | if (!defined('NOREQUIREMENU')) { |
||
448 | if (empty($user->socid)) { // If internal user or not defined |
||
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; |
||
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 |
||
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; |
||
532 | static::$db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int)$conf->db->port); |
||
533 | static::$dolibarrConfig->setValues(static::$db); |
||
538 |