-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_config.php
More file actions
56 lines (49 loc) · 1.75 KB
/
get_config.php
File metadata and controls
56 lines (49 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* get_config.php
*
* Chooses the correct INI file based on where the code is running:
* • Web request → look at $_SERVER['REQUEST_URI']
* • CLI / cron → look at the script directory (__DIR__)
*
* Results:
* /etc/apps/chatdev_config.ini (environment = dev)
* /etc/apps/chattest_config.ini (environment = test)
* /etc/apps/chatprod_config.ini (environment = prod, default)
*/
# 1. Default to production
$environment = '';
# 2. If we're in a web context, key off REQUEST_URI
if (!empty($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, 'chatdev') !== false) {
$environment = 'dev';
} elseif (strpos($uri, 'chattest') !== false) {
$environment = 'test';
}
# 3. Otherwise (CLI / cron), look at the directory path of this file
} else {
$dir = __DIR__; // e.g. /var/www/ai.nhlbi.nih.gov/chatdev
if (strpos($dir, 'chatdev') !== false) {
$environment = 'dev';
} elseif (strpos($dir, 'chattest') !== false) {
$environment = 'test';
}
}
# 4. Load the appropriate INI file first
$config_file = "/etc/apps/chat{$environment}_config.ini";
if (!is_readable($config_file)) {
$explicit_path = getenv('CHAT_CONFIG_PATH');
if ($explicit_path === false || $explicit_path === '') {
if (defined('CHAT_CONFIG_PATH') && CHAT_CONFIG_PATH !== '') {
$explicit_path = CHAT_CONFIG_PATH;
}
}
if ($explicit_path !== false && $explicit_path !== '') {
$config_file = $explicit_path;
$environment = 'custom';
} else {
throw new RuntimeException("Config file not found for environment '{$environment}': {$config_file}");
}
}
$config = parse_ini_file($config_file, true);