@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | - class Config{ |
|
| 27 | + class Config { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The list of loaded configuration |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | * The signleton of the logger |
| 43 | 43 | * @return Object the Log instance |
| 44 | 44 | */ |
| 45 | - private static function getLogger(){ |
|
| 46 | - if(self::$logger == null){ |
|
| 45 | + private static function getLogger() { |
|
| 46 | + if (self::$logger == null) { |
|
| 47 | 47 | $logger = array(); |
| 48 | - $logger[0] =& class_loader('Log', 'classes'); |
|
| 48 | + $logger[0] = & class_loader('Log', 'classes'); |
|
| 49 | 49 | $logger[0]->setLogger('Library::Config'); |
| 50 | 50 | self::$logger = $logger[0]; |
| 51 | 51 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * @param Log $logger the log object |
| 58 | 58 | * @return Log the log instance |
| 59 | 59 | */ |
| 60 | - public static function setLogger($logger){ |
|
| 60 | + public static function setLogger($logger) { |
|
| 61 | 61 | self::$logger = $logger; |
| 62 | 62 | return self::$logger; |
| 63 | 63 | } |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | /** |
| 66 | 66 | * Initialize the configuration by loading all the configuration from config file |
| 67 | 67 | */ |
| 68 | - public static function init(){ |
|
| 68 | + public static function init() { |
|
| 69 | 69 | $logger = static::getLogger(); |
| 70 | 70 | $logger->debug('Initialization of the configuration'); |
| 71 | 71 | self::$config = & load_configurations(); |
| 72 | 72 | self::setBaseUrlUsingServerVar(); |
| 73 | - if(ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info','all'))){ |
|
| 73 | + if (ENVIRONMENT == 'production' && in_array(strtolower(self::$config['log_level']), array('debug', 'info', 'all'))) { |
|
| 74 | 74 | $logger->warning('You are in production environment, please set log level to WARNING, ERROR, FATAL to increase the application performance'); |
| 75 | 75 | } |
| 76 | 76 | $logger->info('Configuration initialized successfully'); |
@@ -83,12 +83,12 @@ discard block |
||
| 83 | 83 | * @param mixed $default the default value to use if can not find the config item in the list |
| 84 | 84 | * @return mixed the config value if exist or the default value |
| 85 | 85 | */ |
| 86 | - public static function get($item, $default = null){ |
|
| 86 | + public static function get($item, $default = null) { |
|
| 87 | 87 | $logger = static::getLogger(); |
| 88 | - if(array_key_exists($item, self::$config)){ |
|
| 88 | + if (array_key_exists($item, self::$config)) { |
|
| 89 | 89 | return self::$config[$item]; |
| 90 | 90 | } |
| 91 | - $logger->warning('Cannot find config item ['.$item.'] using the default value ['.$default.']'); |
|
| 91 | + $logger->warning('Cannot find config item [' . $item . '] using the default value [' . $default . ']'); |
|
| 92 | 92 | return $default; |
| 93 | 93 | } |
| 94 | 94 | |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | * @param string $item the config item name to set |
| 98 | 98 | * @param mixed $value the config item value |
| 99 | 99 | */ |
| 100 | - public static function set($item, $value){ |
|
| 100 | + public static function set($item, $value) { |
|
| 101 | 101 | self::$config[$item] = $value; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * Get all the configuration values |
| 106 | 106 | * @return array the config values |
| 107 | 107 | */ |
| 108 | - public static function getAll(){ |
|
| 108 | + public static function getAll() { |
|
| 109 | 109 | return self::$config; |
| 110 | 110 | } |
| 111 | 111 | |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * Set the configuration values bu merged with the existing configuration |
| 114 | 114 | * @param array $config the config values to add in the configuration list |
| 115 | 115 | */ |
| 116 | - public static function setAll(array $config = array()){ |
|
| 116 | + public static function setAll(array $config = array()) { |
|
| 117 | 117 | self::$config = array_merge(self::$config, $config); |
| 118 | 118 | } |
| 119 | 119 | |
@@ -122,15 +122,15 @@ discard block |
||
| 122 | 122 | * @param string $item the config item name to be deleted |
| 123 | 123 | * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
| 124 | 124 | */ |
| 125 | - public static function delete($item){ |
|
| 125 | + public static function delete($item) { |
|
| 126 | 126 | $logger = static::getLogger(); |
| 127 | - if(array_key_exists($item, self::$config)){ |
|
| 128 | - $logger->info('Delete config item ['.$item.']'); |
|
| 127 | + if (array_key_exists($item, self::$config)) { |
|
| 128 | + $logger->info('Delete config item [' . $item . ']'); |
|
| 129 | 129 | unset(self::$config[$item]); |
| 130 | 130 | return true; |
| 131 | 131 | } |
| 132 | - else{ |
|
| 133 | - $logger->warning('Config item ['.$item.'] to be deleted does not exists'); |
|
| 132 | + else { |
|
| 133 | + $logger->warning('Config item [' . $item . '] to be deleted does not exists'); |
|
| 134 | 134 | return false; |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -139,38 +139,38 @@ discard block |
||
| 139 | 139 | * Load the configuration file. This an alias to Loader::config() |
| 140 | 140 | * @param string $config the config name to be loaded |
| 141 | 141 | */ |
| 142 | - public static function load($config){ |
|
| 142 | + public static function load($config) { |
|
| 143 | 143 | Loader::config($config); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | 147 | * Set the configuration for "base_url" if is not set in the configuration |
| 148 | 148 | */ |
| 149 | - private static function setBaseUrlUsingServerVar(){ |
|
| 149 | + private static function setBaseUrlUsingServerVar() { |
|
| 150 | 150 | $logger = static::getLogger(); |
| 151 | - if (! isset(self::$config['base_url']) || ! is_url(self::$config['base_url'])){ |
|
| 152 | - if(ENVIRONMENT == 'production'){ |
|
| 151 | + if (!isset(self::$config['base_url']) || !is_url(self::$config['base_url'])) { |
|
| 152 | + if (ENVIRONMENT == 'production') { |
|
| 153 | 153 | $logger->warning('Application base URL is not set or invalid, please set application base URL to increase the application loading time'); |
| 154 | 154 | } |
| 155 | 155 | $baseUrl = null; |
| 156 | - if (isset($_SERVER['SERVER_ADDR'])){ |
|
| 156 | + if (isset($_SERVER['SERVER_ADDR'])) { |
|
| 157 | 157 | //check if the server is running under IPv6 |
| 158 | - if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE){ |
|
| 159 | - $baseUrl = '['.$_SERVER['SERVER_ADDR'].']'; |
|
| 158 | + if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE) { |
|
| 159 | + $baseUrl = '[' . $_SERVER['SERVER_ADDR'] . ']'; |
|
| 160 | 160 | } |
| 161 | - else{ |
|
| 161 | + else { |
|
| 162 | 162 | $baseUrl = $_SERVER['SERVER_ADDR']; |
| 163 | 163 | } |
| 164 | - $port = ((isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] != '80' && ! is_https() || $_SERVER['SERVER_PORT'] != '443' && is_https()) ) ? ':' . $_SERVER['SERVER_PORT'] : ''); |
|
| 165 | - $baseUrl = (is_https() ? 'https' : 'http').'://' . $baseUrl . $port |
|
| 164 | + $port = ((isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] != '80' && !is_https() || $_SERVER['SERVER_PORT'] != '443' && is_https())) ? ':' . $_SERVER['SERVER_PORT'] : ''); |
|
| 165 | + $baseUrl = (is_https() ? 'https' : 'http') . '://' . $baseUrl . $port |
|
| 166 | 166 | . substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))); |
| 167 | 167 | } |
| 168 | - else{ |
|
| 168 | + else { |
|
| 169 | 169 | $logger->warning('Can not determine the application base URL automatically, use http://localhost as default'); |
| 170 | 170 | $baseUrl = 'http://localhost/'; |
| 171 | 171 | } |
| 172 | 172 | self::set('base_url', $baseUrl); |
| 173 | 173 | } |
| 174 | - self::$config['base_url'] = rtrim(self::$config['base_url'], '/') .'/'; |
|
| 174 | + self::$config['base_url'] = rtrim(self::$config['base_url'], '/') . '/'; |
|
| 175 | 175 | } |
| 176 | 176 | } |