Test Setup Failed
Branch gcconnex (718fe4)
by Ilia
21:13
created

configuration.php ➔ _elgg_load_application_config()   F

Complexity

Conditions 12
Paths 864

Size

Total Lines 80
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 52
nc 864
nop 0
dl 0
loc 80
rs 2.5
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Elgg configuration procedural code.
4
 *
5
 * Includes functions for manipulating the configuration values stored in the database
6
 * Plugin authors should use the {@link elgg_get_config()}, {@link elgg_set_config()},
7
 * {@link elgg_save_config()}, and {@unset_config()} functions to access or update
8
 * config values.
9
 *
10
 * Elgg's configuration is split among 2 tables and 1 file:
11
 * - dbprefix_config
12
 * - dbprefix_datalists
13
 * - engine/settings.php (See {@link settings.example.php})
14
 *
15
 * Upon system boot, all values in dbprefix_config are read into $CONFIG.
16
 *
17
 * @package Elgg.Core
18
 * @subpackage Configuration
19
 */
20
21
/**
22
 * Get the URL for the current (or specified) site
23
 *
24
 * @param int $site_guid The GUID of the site whose URL we want to grab
25
 * @return string
26
 * @since 1.8.0
27
 */
28
function elgg_get_site_url($site_guid = 0) {
29
	return _elgg_services()->config->getSiteUrl($site_guid);
30
}
31
32
/**
33
 * Get the plugin path for this installation
34
 *
35
 * @return string
36
 * @since 1.8.0
37
 */
38
function elgg_get_plugins_path() {
39
	return _elgg_services()->config->getPluginsPath();
40
}
41
42
/**
43
 * Get the data directory path for this installation
44
 *
45
 * @return string
46
 * @since 1.8.0
47
 */
48
function elgg_get_data_path() {
49
	return _elgg_services()->config->getDataPath();
50
}
51
52
/**
53
 * Get the root directory path for this installation
54
 *
55
 * @return string
56
 * @since 1.8.0
57
 */
58
function elgg_get_root_path() {
59
	return _elgg_services()->config->getRootPath();
60
}
61
62
/**
63
 * Get an Elgg configuration value
64
 *
65
 * @param string $name      Name of the configuration value
66
 * @param int    $site_guid null for installation setting, 0 for default site
67
 *
68
 * @return mixed Configuration value or null if it does not exist
69
 * @since 1.8.0
70
 */
71
function elgg_get_config($name, $site_guid = 0) {
72
	return _elgg_services()->config->get($name, $site_guid);
73
}
74
75
/**
76
 * Set an Elgg configuration value
77
 *
78
 * @warning This does not persist the configuration setting. Use elgg_save_config()
79
 *
80
 * @param string $name  Name of the configuration value
81
 * @param mixed  $value Value
82
 *
83
 * @return void
84
 * @since 1.8.0
85
 */
86
function elgg_set_config($name, $value) {
87
	return _elgg_services()->config->set($name, $value);
88
}
89
90
/**
91
 * Save a configuration setting
92
 *
93
 * @param string $name      Configuration name (cannot be greater than 255 characters)
94
 * @param mixed  $value     Configuration value. Should be string for installation setting
95
 * @param int    $site_guid null for installation setting, 0 for default site
96
 *
97
 * @return bool
98
 * @since 1.8.0
99
 */
100
function elgg_save_config($name, $value, $site_guid = 0) {
101
	return _elgg_services()->config->save($name, $value, $site_guid);
102
}
103
104
/**
105
 * Get the value of a datalist element.
106
 * 
107
 * Plugin authors should use elgg_get_config() and pass null for the site GUID.
108
 *
109
 * @internal Datalists are stored in the datalist table.
110
 *
111
 * @tip Use datalists to store information common to a full installation.
112
 *
113
 * @param string $name The name of the datalist
114
 * @return string|null|false String if value exists, null if doesn't, false on error
115
 * @access private
116
 */
117
function datalist_get($name) {
118
	return _elgg_services()->datalist->get($name);
119
}
120
121
/**
122
 * Set the value for a datalist element.
123
 * 
124
 * Plugin authors should use elgg_save_config() and pass null for the site GUID.
125
 * 
126
 * @warning Names should be selected so as not to collide with the names for the
127
 * site config.
128
 * 
129
 * @warning Values set through datalist_set() are not available in $CONFIG until
130
 * next page load.
131
 *
132
 * @param string $name  The name of the datalist
133
 * @param string $value The new value
134
 *
135
 * @return bool
136
 * @access private
137
 */
138
function datalist_set($name, $value) {
139
	return _elgg_services()->datalist->set($name, $value);
140
}
141
142
/**
143
 * Run a function one time per installation.
144
 *
145
 * If you pass a timestamp as the second argument, it will run the function
146
 * only if (i) it has never been run before or (ii) the timestamp is >=
147
 * the last time it was run.
148
 *
149
 * @warning Functions are determined by their name.  If you change the name of a function
150
 * it will be run again.
151
 *
152
 * @tip Use $timelastupdatedcheck in your plugins init function to perform automated
153
 * upgrades.  Schedule a function to run once and pass the timestamp of the new release.
154
 * This will cause the run once function to be run on all installations.  To perform
155
 * additional upgrades, create new functions for each release.
156
 *
157
 * @warning The function name cannot be longer than 255 characters long due to
158
 * the current schema for the datalist table.
159
 *
160
 * @internal A datalist entry $functioname is created with the value of time().
161
 *
162
 * @param string $functionname         The name of the function you want to run.
163
 * @param int    $timelastupdatedcheck A UNIX timestamp. If time() is > than this,
164
 *                                     this function will be run again.
165
 *
166
 * @return bool
167
 * @todo deprecate
168
 */
169
function run_function_once($functionname, $timelastupdatedcheck = 0) {
170
	return _elgg_services()->datalist->runFunctionOnce($functionname, $timelastupdatedcheck);
171
}
172
173
/**
174
 * Removes a config setting.
175
 *
176
 * @note Internal: These settings are stored in the dbprefix_config table and read
177
 * during system boot into $CONFIG.
178
 *
179
 * @param string $name      The name of the field.
180
 * @param int    $site_guid Optionally, the GUID of the site (default: current site).
181
 *
182
 * @return bool Success or failure
183
 *
184
 * @see get_config()
185
 * @see set_config()
186
 */
187
function unset_config($name, $site_guid = 0) {
188
	return _elgg_services()->configTable->remove($name, $site_guid);
189
}
190
191
/**
192
 * Add or update a config setting.
193
 * 
194
 * Plugin authors should use elgg_set_config().
195
 *
196
 * If the config name already exists, it will be updated to the new value.
197
 *
198
 * @warning Names should be selected so as not to collide with the names for the
199
 * datalist (application configuration)
200
 * 
201
 * @internal These settings are stored in the dbprefix_config table and read 
202
 * during system boot into $CONFIG.
203
 * 
204
 * @internal The value is serialized so we maintain type information.
205
 *
206
 * @param string $name      The name of the configuration value
207
 * @param mixed  $value     Its value
208
 * @param int    $site_guid Optionally, the GUID of the site (current site is assumed by default)
209
 *
210
 * @return bool
211
 * @see unset_config()
212
 * @see get_config()
213
 * @access private
214
 */
215
function set_config($name, $value, $site_guid = 0) {
216
	return _elgg_services()->configTable->set($name, $value, $site_guid);
217
}
218
219
/**
220
 * Gets a configuration value
221
 * 
222
 * Plugin authors should use elgg_get_config().
223
 *
224
 * @internal These settings are stored in the dbprefix_config table and read 
225
 * during system boot into $CONFIG.
226
 *
227
 * @param string $name      The name of the config value
228
 * @param int    $site_guid Optionally, the GUID of the site (default: current site)
229
 *
230
 * @return mixed|null
231
 * @see set_config()
232
 * @see unset_config()
233
 * @access private
234
 */
235
function get_config($name, $site_guid = 0) {
236
	return _elgg_services()->configTable->get($name, $site_guid);
237
}
238
239
/**
240
 * Loads configuration related to this site
241
 *
242
 * This runs on engine boot and loads from the config database table and the 
243
 * site entity. It runs after the application configuration is loaded by
244
 * _elgg_load_application_config().
245
 * 
246
 * @see _elgg_engine_boot()
247
 * 
248
 * @access private
249
 */
250
function _elgg_load_site_config() {
251
	global $CONFIG;
252
253
	$CONFIG->site_guid = (int) datalist_get('default_site');
254
	$CONFIG->site_id = $CONFIG->site_guid;
255
	$CONFIG->site = _elgg_services()->entityTable->get($CONFIG->site_guid, 'site');
256
	if (!$CONFIG->site) {
257
		throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down.");
258
	}
259
260
	$CONFIG->wwwroot = $CONFIG->site->url;
261
	$CONFIG->sitename = $CONFIG->site->name;
262
	$CONFIG->sitedescription = $CONFIG->site->description;
263
	$CONFIG->siteemail = $CONFIG->site->email;
264
	$CONFIG->url = $CONFIG->wwwroot;
265
266
	_elgg_services()->configTable->loadAll();
267
268
	// gives hint to elgg_get_config function how to approach missing values
269
	$CONFIG->site_config_loaded = true;
270
271
	if (!empty($CONFIG->debug)) {
272
		_elgg_services()->logger->setLevel($CONFIG->debug);
273
		_elgg_services()->logger->setDisplay(true);
274
	}
275
}
276
277
/**
278
 * Loads configuration related to Elgg as an application
279
 *
280
 * This runs on the engine boot and loads from the datalists database table.
281
 * 
282
 * @see _elgg_engine_boot()
283
 * 
284
 * @access private
285
 */
286
function _elgg_load_application_config() {
287
	global $CONFIG;
288
289
	$install_root = str_replace("\\", "/", dirname(dirname(dirname(__FILE__))));
290
	$defaults = array(
291
		'path' => "$install_root/",
292
		'view_path' => "$install_root/views/",
293
		'plugins_path' => "$install_root/mod/",
294
		'language' => 'en',
295
296
		// compatibility with old names for plugins not using elgg_get_config()
297
		'viewpath' => "$install_root/views/",
298
		'pluginspath' => "$install_root/mod/",
299
	);
300
301
	foreach ($defaults as $name => $value) {
302
		if (empty($CONFIG->$name)) {
303
			$CONFIG->$name = $value;
304
		}
305
	}
306
307
	// set cookie values for session and remember me
308
	if (!isset($CONFIG->cookies)) {
309
		$CONFIG->cookies = array();
310
	}
311
	if (!isset($CONFIG->cookies['session'])) {
312
		$CONFIG->cookies['session'] = array();
313
	}
314
	$session_defaults = session_get_cookie_params();
315
	$session_defaults['name'] = 'Elgg';
316
	$CONFIG->cookies['session'] = array_merge($session_defaults, $CONFIG->cookies['session']);
317
	if (!isset($CONFIG->cookies['remember_me'])) {
318
		$CONFIG->cookies['remember_me'] = array();
319
	}
320
	$session_defaults['name'] = 'elggperm';
321
	$session_defaults['expire'] = strtotime("+30 days");
322
	$CONFIG->cookies['remember_me'] = array_merge($session_defaults, $CONFIG->cookies['remember_me']);
323
324
	if (!is_memcache_available()) {
325
		_elgg_services()->datalist->loadAll();
326
	}
327
328
	// allow sites to set dataroot and simplecache_enabled in settings.php
329
	if (isset($CONFIG->dataroot)) {
330
		$CONFIG->dataroot = sanitise_filepath($CONFIG->dataroot);
331
		$CONFIG->dataroot_in_settings = true;
332
	} else {
333
		$dataroot = datalist_get('dataroot');
334
		if (!empty($dataroot)) {
335
			$CONFIG->dataroot = $dataroot;
336
		}
337
		$CONFIG->dataroot_in_settings = false;
338
	}
339
	if (isset($CONFIG->simplecache_enabled)) {
340
		$CONFIG->simplecache_enabled_in_settings = true;
341
	} else {
342
		$simplecache_enabled = datalist_get('simplecache_enabled');
343
		if ($simplecache_enabled !== false) {
344
			$CONFIG->simplecache_enabled = $simplecache_enabled;
345
		} else {
346
			$CONFIG->simplecache_enabled = 1;
347
		}
348
		$CONFIG->simplecache_enabled_in_settings = false;
349
	}
350
351
	$system_cache_enabled = datalist_get('system_cache_enabled');
352
	if ($system_cache_enabled !== false) {
353
		$CONFIG->system_cache_enabled = $system_cache_enabled;
354
	} else {
355
		$CONFIG->system_cache_enabled = 1;
356
	}
357
358
	// needs to be set before system, init for links in html head
359
	$CONFIG->lastcache = (int)datalist_get("simplecache_lastupdate");
360
361
	$CONFIG->i18n_loaded_from_cache = false;
362
363
	// this must be synced with the enum for the entities table
364
	$CONFIG->entity_types = array('group', 'object', 'site', 'user');
365
}
366
367
/**
368
 * @access private
369
 */
370
function _elgg_config_test($hook, $type, $tests) {
371
	global $CONFIG;
372
	$tests[] = "{$CONFIG->path}engine/tests/ElggCoreConfigTest.php";
373
	return $tests;
374
}
375
376
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
377
	$hooks->registerHandler('unit_test', 'system', '_elgg_config_test');
378
};
379