|
1
|
|
|
<?php |
|
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
|
3
|
|
|
/** |
|
4
|
|
|
* TNH Framework |
|
5
|
|
|
* |
|
6
|
|
|
* A simple PHP framework using HMVC architecture |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2017 TNH Framework |
|
11
|
|
|
* |
|
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
14
|
|
|
* in the Software without restriction, including without limitation the rights |
|
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
17
|
|
|
* furnished to do so, subject to the following conditions: |
|
18
|
|
|
* |
|
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
20
|
|
|
* copies or substantial portions of the Software. |
|
21
|
|
|
* |
|
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
28
|
|
|
* SOFTWARE. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
class Config extends BaseClass { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The list of loaded configuration |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $config = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Initialize the configuration by loading all the configuration from config file |
|
41
|
|
|
* |
|
42
|
|
|
* @param boolean $init whether to load the configuration |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($init = true) { |
|
45
|
|
|
parent::__construct(); |
|
46
|
|
|
if ($init) { |
|
47
|
|
|
$this->init(); |
|
48
|
|
|
//@codeCoverageIgnoreStart |
|
49
|
|
|
if (ENVIRONMENT == 'production' && in_array(strtolower($this->config['log_level']), array('debug', 'info', 'notice'))) { |
|
50
|
|
|
$this->logger->warning('You are in production environment, please set ' |
|
51
|
|
|
. 'log level to WARNING, ERROR, CRITICAL, ALERT, EMERGENCY to increase the application performance'); |
|
52
|
|
|
} |
|
53
|
|
|
//@codeCoverageIgnoreEnd |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the configuration item value |
|
59
|
|
|
* @param string $item the configuration item name to get |
|
60
|
|
|
* @param mixed $default the default value to use if can not find the config item in the list |
|
61
|
|
|
* @return mixed the config value if exist or the default value |
|
62
|
|
|
*/ |
|
63
|
|
|
public function get($item, $default = null) { |
|
64
|
|
|
if (array_key_exists($item, $this->config)) { |
|
65
|
|
|
return $this->config[$item]; |
|
66
|
|
|
} |
|
67
|
|
|
$this->logger->warning('Cannot find config item [' . $item . '] using the default value [' . stringfy_vars($default) . ']'); |
|
68
|
|
|
return $default; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the configuration item value |
|
73
|
|
|
* @param string $item the config item name to set |
|
74
|
|
|
* @param mixed $value the config item value |
|
75
|
|
|
*/ |
|
76
|
|
|
public function set($item, $value) { |
|
77
|
|
|
$this->config[$item] = $value; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get all the configuration values |
|
82
|
|
|
* @return array the config values |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getAll() { |
|
85
|
|
|
return $this->config; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set the configuration values by merged with the existing configuration |
|
90
|
|
|
* @param array $config the config values to add in the configuration list |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setAll(array $config = array()) { |
|
93
|
|
|
$this->config = array_merge($this->config, $config); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Delete the configuration item in the list |
|
98
|
|
|
* @param string $item the config item name to be deleted |
|
99
|
|
|
* @return boolean true if the item exists and is deleted successfully otherwise will return false. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function delete($item) { |
|
102
|
|
|
if (array_key_exists($item, $this->config)) { |
|
103
|
|
|
$this->logger->info('Delete config item [' . $item . ']'); |
|
104
|
|
|
unset($this->config[$item]); |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
$this->logger->warning('Config item [' . $item . '] to be deleted does not exist'); |
|
108
|
|
|
return false; |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Delete all the configuration values |
|
114
|
|
|
*/ |
|
115
|
|
|
public function deleteAll() { |
|
116
|
|
|
$this->config = array(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Load the configuration using config file and check if the config "base_url" is not set |
|
121
|
|
|
* try to set it using serve variable |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function init() { |
|
124
|
|
|
$this->logger->debug('Initialization of the configuration'); |
|
125
|
|
|
$this->config = & load_configurations(); |
|
126
|
|
|
$this->setBaseUrlUsingServerVar(); |
|
127
|
|
|
$this->logger->info('Configuration initialized successfully'); |
|
128
|
|
|
$this->logger->info('The application configuration are listed below: ' . stringfy_vars($this->config)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set the configuration for "base_url" if is not set in the configuration |
|
134
|
|
|
* @codeCoverageIgnore |
|
135
|
|
|
*/ |
|
136
|
|
|
private function setBaseUrlUsingServerVar() { |
|
137
|
|
|
if (empty($this->config['base_url'])) { |
|
138
|
|
|
if (ENVIRONMENT == 'production') { |
|
|
|
|
|
|
139
|
|
|
$this->logger->warning('Application base URL is not set or invalid, please' |
|
140
|
|
|
. ' set application base URL to increase the application loading time'); |
|
141
|
|
|
} |
|
142
|
|
|
$baseUrl = null; |
|
143
|
|
|
$protocol = 'http'; |
|
144
|
|
|
if (is_https()) { |
|
145
|
|
|
$protocol = 'https'; |
|
146
|
|
|
} |
|
147
|
|
|
$protocol .= '://'; |
|
148
|
|
|
$globals = & class_loader('GlobalVar', 'classes'); |
|
149
|
|
|
$serverAddr = $globals->server('SERVER_ADDR'); |
|
150
|
|
|
if ($serverAddr) { |
|
151
|
|
|
$baseUrl = $serverAddr; |
|
152
|
|
|
//check if the server is running under IPv6 |
|
153
|
|
|
if (strpos($serverAddr, ':') !== FALSE) { |
|
154
|
|
|
$baseUrl = '[' . $serverAddr . ']'; |
|
155
|
|
|
} |
|
156
|
|
|
$port = $this->getServerPort(); |
|
157
|
|
|
$baseUrl = $protocol . $baseUrl . $port . substr( |
|
158
|
|
|
$globals->server('SCRIPT_NAME'), |
|
159
|
|
|
0, |
|
160
|
|
|
strpos( |
|
161
|
|
|
$globals->server('SCRIPT_NAME'), |
|
162
|
|
|
basename($globals->server('SCRIPT_FILENAME') |
|
163
|
|
|
)) |
|
164
|
|
|
); |
|
165
|
|
|
} else { |
|
166
|
|
|
$this->logger->warning('Can not determine the application ' |
|
167
|
|
|
. 'base URL automatically, use http://localhost as default'); |
|
168
|
|
|
$baseUrl = 'http://localhost/'; |
|
169
|
|
|
} |
|
170
|
|
|
$this->config['base_url'] = $baseUrl; |
|
171
|
|
|
} |
|
172
|
|
|
$this->config['base_url'] = rtrim($this->config['base_url'], '/') . '/'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Return the server port using variable |
|
177
|
|
|
* |
|
178
|
|
|
* @codeCoverageIgnore |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function getServerPort() { |
|
182
|
|
|
$globals = & class_loader('GlobalVar', 'classes'); |
|
183
|
|
|
$serverPort = $globals->server('SERVER_PORT'); |
|
184
|
|
|
$port = ''; |
|
185
|
|
|
if (!in_array($serverPort, array(80, 443))) { |
|
186
|
|
|
$port = ':' . $serverPort; |
|
187
|
|
|
} |
|
188
|
|
|
return $port; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|