Total Complexity | 51 |
Total Lines | 263 |
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 |
||
35 | abstract class Config |
||
36 | { |
||
37 | /** |
||
38 | * Contains the information from the conf.php file. |
||
39 | * |
||
40 | * @var null|stdClass |
||
41 | */ |
||
42 | protected static $config = null; |
||
43 | |||
44 | /** |
||
45 | * Contains the information of the old conf global var. |
||
46 | * |
||
47 | * @var null|stdClass |
||
48 | */ |
||
49 | protected static $conf = null; |
||
50 | |||
51 | /** |
||
52 | * Simply replace /htdocs with /documents in $pathDir |
||
53 | * |
||
54 | * @param $pathDir |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | private static function getDataDir($pathDir) |
||
59 | { |
||
60 | return preg_replace("/\/htdocs$/", "", $pathDir) . '/documents'; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Returns the Dolibarr conf.php complete path. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public static function getDolibarrConfigFilename() |
||
69 | { |
||
70 | return BASE_PATH . '/conf/conf.php'; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Returns a stdClass with the information contained in the conf.php file. |
||
75 | * |
||
76 | * @param $reload |
||
77 | * |
||
78 | * @return stdClass|null |
||
79 | */ |
||
80 | public static function loadConfig($reload = false): ?stdClass |
||
81 | { |
||
82 | if (isset(static::$config) && !$reload) { |
||
83 | return static::$config; |
||
84 | } |
||
85 | |||
86 | $filename = static::getDolibarrConfigFilename(); |
||
87 | if (!file_exists($filename) || !is_readable($filename)) { |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | include $filename; |
||
92 | |||
93 | $config = new stdClass(); |
||
94 | |||
95 | // 'main' section |
||
96 | $config->main = new stdClass(); |
||
97 | $config->main->base_path = trim($dolibarr_main_document_root ?? constant('BASE_PATH')); |
||
|
|||
98 | $config->main->base_url = trim($dolibarr_main_url_root ?? constant('BASE_URL')); |
||
99 | $config->main->data_path = trim($dolibarr_main_data_root ?? static::getDataDir($config->main->base_path)); |
||
100 | |||
101 | $alt_base_path = $dolibarr_main_document_root_alt ?? false; |
||
102 | if ($alt_base_path !== false) { |
||
103 | $config->main->alt_base_path = trim($dolibarr_main_document_root_alt); |
||
104 | } |
||
105 | |||
106 | $alt_base_url = $dolibarr_main_url_root_alt ?? false; |
||
107 | if ($alt_base_url !== false) { |
||
108 | $config->main->alt_base_url = trim($dolibarr_main_url_root_alt); |
||
109 | } |
||
110 | |||
111 | $alt_data_path = $dolibarr_main_data_root_alt ?? false; |
||
112 | if ($alt_data_path !== false) { |
||
113 | $config->main->alt_data_path = trim($dolibarr_main_data_root_alt); |
||
114 | } |
||
115 | |||
116 | // 'db' section |
||
117 | $config->db = new stdClass(); |
||
118 | $config->db->type = trim($dolibarr_main_db_type ?? 'mysql'); |
||
119 | $config->db->host = trim($dolibarr_main_db_host ?? 'localhost'); |
||
120 | $config->db->port = trim($dolibarr_main_db_port ?? ''); |
||
121 | $config->db->name = trim($dolibarr_main_db_name ?? 'dolibarr'); |
||
122 | $config->db->user = trim($dolibarr_main_db_user ?? 'dolibarr'); |
||
123 | $config->db->pass = trim($dolibarr_main_db_pass ?? ''); |
||
124 | $config->db->prefix = trim($dolibarr_main_db_prefix ?? ''); |
||
125 | $config->db->charset = trim($dolibarr_main_db_character_set ?? 'utf8'); |
||
126 | $config->db->collation = trim($dolibarr_main_db_collation ?? 'utf8mb4_unicode_ci'); |
||
127 | |||
128 | // 'security' section |
||
129 | $config->security = new stdClass(); |
||
130 | $config->security->authentication_type = $dolibarr_main_authentication ?? 'dolibarr'; |
||
131 | $config->security->force_https = intval($dolibarr_main_force_https ?? 1); |
||
132 | $config->security->unique_id = $dolibarr_main_instance_unique_id ?? null; |
||
133 | |||
134 | $config->file = new stdClass(); |
||
135 | $config->file->instance_unique_id = $config->security->unique_id; |
||
136 | |||
137 | // Others |
||
138 | $demo = $dolibarr_main_demo ?? false; |
||
139 | if ($demo !== false) { |
||
140 | $credentials = explode(',', $demo); |
||
141 | if (count($credentials) === 2) { |
||
142 | $config->demo->user = trim($credentials[0]); |
||
143 | $config->demo->pass = trim($credentials[1]); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | $config->debug = intval($dolibarr_main_prod ?? 1) === 0; |
||
148 | |||
149 | // 'Server' section |
||
150 | $config->server = new stdClass(); |
||
151 | $config->server->detailed_info = !empty($_SERVER['MAIN_SHOW_TUNING_INFO']); |
||
152 | |||
153 | return $config; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Create the Dolibarr conf variable. |
||
158 | * |
||
159 | * @param $reload |
||
160 | * |
||
161 | * @return stdClass|null |
||
162 | */ |
||
163 | public static function loadConf($reload = false): ?stdClass |
||
164 | { |
||
165 | if (isset(static::$conf) && !$reload) { |
||
166 | return static::$conf; |
||
167 | } |
||
168 | |||
169 | $filename = static::getDolibarrConfigFilename(); |
||
170 | $exists = file_exists($filename) && is_readable($filename); |
||
171 | if ($exists) { |
||
172 | include $filename; |
||
173 | } |
||
174 | |||
175 | /* |
||
176 | * Create $conf object |
||
177 | */ |
||
178 | |||
179 | $conf = new Conf(); |
||
180 | |||
181 | // Set properties specific to database |
||
182 | $conf->db->host = empty($dolibarr_main_db_host) ? '' : $dolibarr_main_db_host; |
||
183 | $conf->db->port = empty($dolibarr_main_db_port) ? '' : $dolibarr_main_db_port; |
||
184 | $conf->db->name = empty($dolibarr_main_db_name) ? '' : $dolibarr_main_db_name; |
||
185 | $conf->db->user = empty($dolibarr_main_db_user) ? '' : $dolibarr_main_db_user; |
||
186 | $conf->db->pass = empty($dolibarr_main_db_pass) ? '' : $dolibarr_main_db_pass; |
||
187 | $conf->db->type = $dolibarr_main_db_type ?? 'mysqli'; |
||
188 | $conf->db->prefix = $dolibarr_main_db_prefix ?? 'alx_'; |
||
189 | $conf->db->character_set = $dolibarr_main_db_character_set ?? 'utf8'; |
||
190 | $conf->db->dolibarr_main_db_collation = $dolibarr_main_db_collation ?? 'utf8-unicode-ci'; |
||
191 | $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption ?? null; |
||
192 | $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey ?? null; |
||
193 | if (defined('TEST_DB_FORCE_TYPE')) { |
||
194 | $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example) |
||
195 | } |
||
196 | |||
197 | // Set properties specific to conf file |
||
198 | $conf->file->main_limit_users = $dolibarr_main_limit_users ?? null; |
||
199 | $conf->file->mailing_limit_sendbyweb = empty($dolibarr_mailing_limit_sendbyweb) ? 0 : $dolibarr_mailing_limit_sendbyweb; |
||
200 | $conf->file->mailing_limit_sendbycli = empty($dolibarr_mailing_limit_sendbycli) ? 0 : $dolibarr_mailing_limit_sendbycli; |
||
201 | $conf->file->mailing_limit_sendbyday = empty($dolibarr_mailing_limit_sendbyday) ? 0 : $dolibarr_mailing_limit_sendbyday; |
||
202 | $conf->file->main_authentication = empty($dolibarr_main_authentication) ? 'dolibarr' : $dolibarr_main_authentication; // Identification mode |
||
203 | $conf->file->main_force_https = empty($dolibarr_main_force_https) ? '' : $dolibarr_main_force_https; // Force https |
||
204 | $conf->file->strict_mode = empty($dolibarr_strict_mode) ? '' : $dolibarr_strict_mode; // Force php strict mode (for debug) |
||
205 | $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 |
||
206 | $conf->file->dol_main_url_root = $dolibarr_main_url_root ?? BASE_URL; // Define url inside the config file |
||
207 | $conf->file->dol_document_root = ['main' => (string) DOL_DOCUMENT_ROOT]; // Define array of document root directories ('/home/htdocs') |
||
208 | $conf->file->dol_url_root = ['main' => (string) DOL_URL_ROOT]; // Define array of url root path ('' or '/dolibarr') |
||
209 | if (!empty($dolibarr_main_document_root_alt)) { |
||
210 | // dolibarr_main_document_root_alt can contains several directories |
||
211 | $values = preg_split('/[;,]/', $dolibarr_main_document_root_alt); |
||
212 | $i = 0; |
||
213 | foreach ($values as $value) { |
||
214 | $conf->file->dol_document_root['alt' . ($i++)] = (string) $value; |
||
215 | } |
||
216 | $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt); |
||
217 | $i = 0; |
||
218 | foreach ($values as $value) { |
||
219 | if (preg_match('/^http(s)?:/', $value)) { |
||
220 | // Show error message |
||
221 | $correct_value = str_replace($dolibarr_main_url_root, '', $value); |
||
222 | print '<b>Error:</b><br>' . "\n"; |
||
223 | print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>' . "\n"; |
||
224 | print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>' . "\n"; |
||
225 | print 'Value found: ' . $value . '<br>' . "\n"; |
||
226 | print 'Should be replaced by: ' . $correct_value . '<br>' . "\n"; |
||
227 | print "Or something like following examples:<br>\n"; |
||
228 | print "\"/extensions\"<br>\n"; |
||
229 | print "\"/extensions1,/extensions2,...\"<br>\n"; |
||
230 | print "\"/../extensions\"<br>\n"; |
||
231 | print "\"/custom\"<br>\n"; |
||
232 | exit; |
||
233 | } |
||
234 | $conf->file->dol_url_root['alt' . ($i++)] = (string) $value; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | // Load the main includes of common libraries |
||
239 | if (!defined('NOREQUIREUSER')) { |
||
240 | require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php'; // Need 500ko memory |
||
241 | } |
||
242 | if (!defined('NOREQUIRETRAN')) { |
||
243 | require_once DOL_DOCUMENT_ROOT . '/core/class/translate.class.php'; |
||
244 | } |
||
245 | if (!defined('NOREQUIRESOC')) { |
||
246 | require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php'; |
||
247 | } |
||
248 | |||
249 | return $conf; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Create the content for the menumanager variable. |
||
254 | * |
||
255 | * @param $conf |
||
256 | * |
||
257 | * @return MenuManager |
||
258 | * @throws \Exception |
||
259 | */ |
||
260 | public static function getMenuManager($conf) |
||
298 | } |
||
299 | } |
||
300 |