Passed
Push — 1.0.0-dev ( ea6d41...cf37bc )
by nguereza
03:00
created

Config::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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