Passed
Push — 1.0.0-dev ( 93958a...e1c8ef )
by nguereza
02:26
created

Config::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 extends BaseStaticClass{
28
		
29
		/**
30
		 * The list of loaded configuration
31
		 * @var array
32
		 */
33
		private static $config = array();
34
35
		/**
36
		 * Initialize the configuration by loading all the configuration from config file
37
		 */
38
		public static function init(){
39
			$logger = self::getLogger();
40
			$logger->debug('Initialization of the configuration');
41
			self::$config = & load_configurations();
42
			self::setBaseUrlUsingServerVar();
43
			if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){
44
				$logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance');
45
			}
46
			$logger->info('Configuration initialized successfully');
47
			$logger->info('The application configuration are listed below: ' . stringfy_vars(self::$config));
48
		}
49
50
		/**
51
		 * Get the configuration item value
52
		 * @param  string $item    the configuration item name to get
53
		 * @param  mixed $default the default value to use if can not find the config item in the list
54
		 * @return mixed          the config value if exist or the default value
55
		 */
56
		public static function get($item, $default = null){
57
			$logger = self::getLogger();
58
			if(array_key_exists($item, self::$config)){
59
				return self::$config[$item];
60
			}
61
			$logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']');
62
			return $default;
63
		}
64
65
		/**
66
		 * Set the configuration item value
67
		 * @param string $item  the config item name to set
68
		 * @param mixed $value the config item value
69
		 */
70
		public static function set($item, $value){
71
			self::$config[$item] = $value;
72
		}
73
74
		/**
75
		 * Get all the configuration values
76
		 * @return array the config values
77
		 */
78
		public static function getAll(){
79
			return self::$config;
80
		}
81
82
		/**
83
		 * Set the configuration values bu merged with the existing configuration
84
		 * @param array $config the config values to add in the configuration list
85
		 */
86
		public static function setAll(array $config = array()){
87
			self::$config = array_merge(self::$config, $config);
88
		}
89
90
		/**
91
		 * Delete the configuration item in the list
92
		 * @param  string $item the config item name to be deleted
93
		 * @return boolean true if the item exists and is deleted successfully otherwise will return false.
94
		 */
95
		public static function delete($item){
96
			$logger = self::getLogger();
97
			if(array_key_exists($item, self::$config)){
98
				$logger->info('Delete config item ['.$item.']');
99
				unset(self::$config[$item]);
100
				return true;
101
			}
102
			else{
103
				$logger->warning('Config item ['.$item.'] to be deleted does not exists');
104
				return false;
105
			}
106
		}
107
108
		/**
109
		 * Load the configuration file. This an alias to Loader::config()
110
		 * @param  string $config the config name to be loaded
111
		 */
112
		public static function load($config){
113
			Loader::config($config);
114
		}
115
116
		/**
117
		 * Set the configuration for "base_url" if is not set in the configuration
118
		 */
119
		private static function setBaseUrlUsingServerVar(){
120
			$logger = self::getLogger();
121
			if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){
122
				if(ENVIRONMENT == 'production'){
0 ignored issues
show
introduced by
The condition ENVIRONMENT == 'production' is always false.
Loading history...
123
					$logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time');
124
				}
125
				$baseUrl = null;
126
				$protocol = 'http';
127
				if(is_https()){
128
					$protocol = 'https';
129
				}
130
				$protocol .='://';
131
132
				if (isset($_SERVER['SERVER_ADDR'])){
133
					$baseUrl = $_SERVER['SERVER_ADDR'];
134
					//check if the server is running under IPv6
135
					if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){
136
						$baseUrl = '['.$_SERVER['SERVER_ADDR'].']';
137
					}
138
					$serverPort = 80;
139
					if (isset($_SERVER['SERVER_PORT'])) {
140
						$serverPort = $_SERVER['SERVER_PORT'];
141
					}
142
					$port = '';
143
					if($serverPort && ((is_https() && $serverPort != 443) || (!is_https() && $serverPort != 80))){
144
						$port = ':'.$serverPort;
145
					}
146
					$baseUrl = $protocol . $baseUrl . $port . substr(
147
																		$_SERVER['SCRIPT_NAME'], 
148
																		0, 
149
																		strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))
150
																	);
151
				}
152
				else{
153
					$logger->warning('Can not determine the application base URL automatically, use http://localhost as default');
154
					$baseUrl = 'http://localhost/';
155
				}
156
				self::set('base_url', $baseUrl);
157
			}
158
			self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/';
159
		}
160
	}
161