Passed
Push — 1.0.0-dev ( 407604...83bedf )
by nguereza
03:26
created

Config::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 GNU GPL License (GPL)
9
	 *
10
	 * Copyright (C) 2017 Tony NGUEREZA
11
	 *
12
	 * This program is free software; you can redistribute it and/or
13
	 * modify it under the terms of the GNU General Public License
14
	 * as published by the Free Software Foundation; either version 3
15
	 * of the License, or (at your option) any later version.
16
	 *
17
	 * This program is distributed in the hope that it will be useful,
18
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
	 * GNU General Public License for more details.
21
	 *
22
	 * You should have received a copy of the GNU General Public License
23
	 * along with this program; if not, write to the Free Software
24
	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
	*/
26
27
	class Config{
28
		
29
		/**
30
		 * The list of loaded configuration
31
		 * @var array
32
		 */
33
		private static $config = array();
34
35
		/**
36
		 * The logger instance
37
		 * @var Log
38
		 */
39
		private static $logger;
40
41
		/**
42
		 * The signleton of the logger
43
		 * @return Object the Log instance
44
		 */
45
		private static function getLogger(){
46
			if(self::$logger == null){
47
				$logger = array();
48
				$logger[0] =& class_loader('Log', 'classes');
49
				$logger[0]->setLogger('Library::Config');
50
				self::$logger = $logger[0];
51
			}
52
			return self::$logger;			
53
		}
54
55
		/**
56
		 * Set the log instance for future use
57
		 * @param Log $logger the log object
58
		 * @return Log the log instance
59
		 */
60
		public static function setLogger($logger){
61
			self::$logger = $logger;
62
			return self::$logger;
63
		}
64
65
		/**
66
		 * Initialize the configuration by loading all the configuration from config file
67
		 */
68
		public static function init(){
69
			$logger = static::getLogger();
70
			$logger->debug('Initialization of the configuration');
71
			self::$config = & load_configurations();
72
			self::setBaseUrlUsingServerVar();
73
			if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){
74
				$logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance');
75
			}
76
			$logger->info('Configuration initialized successfully');
77
			$logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config));
78
		}
79
80
		/**
81
		 * Get the configuration item value
82
		 * @param  string $item    the configuration item name to get
83
		 * @param  mixed $default the default value to use if can not find the config item in the list
84
		 * @return mixed          the config value if exist or the default value
85
		 */
86
		public static function get($item, $default = null){
87
			$logger = static::getLogger();
88
			if(array_key_exists($item, self::$config)){
89
				return self::$config[$item];
90
			}
91
			$logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']');
92
			return $default;
93
		}
94
95
		/**
96
		 * Set the configuration item value
97
		 * @param string $item  the config item name to set
98
		 * @param mixed $value the config item value
99
		 */
100
		public static function set($item, $value){
101
			self::$config[$item] = $value;
102
		}
103
104
		/**
105
		 * Get all the configuration values
106
		 * @return array the config values
107
		 */
108
		public static function getAll(){
109
			return self::$config;
110
		}
111
112
		/**
113
		 * Set the configuration values bu merged with the existing configuration
114
		 * @param array $config the config values to add in the configuration list
115
		 */
116
		public static function setAll(array $config = array()){
117
			self::$config = array_merge(self::$config, $config);
118
		}
119
120
		/**
121
		 * Delete the configuration item in the list
122
		 * @param  string $item the config item name to be deleted
123
		 * @return boolean true if the item exists and is deleted successfully otherwise will return false.
124
		 */
125
		public static function delete($item){
126
			$logger = static::getLogger();
127
			if(array_key_exists($item, self::$config)){
128
				$logger->info('Delete config item ['.$item.']');
129
				unset(self::$config[$item]);
130
				return true;
131
			}
132
			else{
133
				$logger->warning('Config item ['.$item.'] to be deleted does not exists');
134
				return false;
135
			}
136
		}
137
138
		/**
139
		 * Load the configuration file. This an alias to Loader::config()
140
		 * @param  string $config the config name to be loaded
141
		 */
142
		public static function load($config){
143
			Loader::config($config);
144
		}
145
146
		/**
147
		 * Set the configuration for "base_url" if is not set in the configuration
148
		 */
149
		private static function setBaseUrlUsingServerVar(){
150
			$logger = static::getLogger();
151
			if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){
152
				if(ENVIRONMENT == 'production'){
0 ignored issues
show
introduced by
The condition ENVIRONMENT == 'production' is always false.
Loading history...
153
					$logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time');
154
				}
155
				$baseUrl = null;
156
				$protocol = 'http';
157
				if(is_https()){
158
					$protocol = 'https';
159
				}
160
				$protocol .='://';
161
162
				if (isset($_SERVER['SERVER_ADDR'])){
163
					$baseUrl = $_SERVER['SERVER_ADDR'];
164
					//check if the server is running under IPv6
165
					if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){
166
						$baseUrl = '['.$_SERVER['SERVER_ADDR'].']';
167
					}
168
					$serverPort = 80;
169
					if (isset($_SERVER['SERVER_PORT'])) {
170
						$serverPort = $_SERVER['SERVER_PORT'];
171
					}
172
					$port = '';
173
					if($serverPort && ((is_https() && $serverPort != 443) || (!is_https() && $serverPort != 80))){
174
						$port = ':'.$serverPort;
0 ignored issues
show
Unused Code introduced by
The assignment to $port is dead and can be removed.
Loading history...
175
					}
176
					$port = ((($serverPort != '80' && ! is_https()) || ($serverPort != '443' && is_https())) ? ':' . $serverPort : '');
177
					$baseUrl = $protocol . $baseUrl . $port . substr(
178
																		$_SERVER['SCRIPT_NAME'], 
179
																		0, 
180
																		strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))
181
																	);
182
				}
183
				else{
184
					$logger->warning('Can not determine the application base URL automatically, use http://localhost as default');
185
					$baseUrl = 'http://localhost/';
186
				}
187
				self::set('base_url', $baseUrl);
188
			}
189
			self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/';
190
		}
191
	}
192