|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Config class. |
|
5
|
|
|
* Gets a configuration value |
|
6
|
|
|
* |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
8
|
|
|
* @author Omar El Gabry <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class Config{ |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Array of configurations |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $config = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Prefixes used to load specific configurations. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $prefix = [ |
|
26
|
|
|
'default' => 'config', |
|
27
|
|
|
'js' => 'javascript' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get default configuration value(s) |
|
32
|
|
|
* |
|
33
|
|
|
* @param $key string |
|
34
|
|
|
* @return string|array|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function get($key){ |
|
37
|
|
|
return self::_get($key, self::$prefix['default']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set or add a default configuration value |
|
42
|
|
|
* |
|
43
|
|
|
* @param $key string |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function set($key, $value){ |
|
46
|
|
|
self::_set($key, $value, self::$prefix['default']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get javascript configuration value(s) |
|
51
|
|
|
* |
|
52
|
|
|
* @param $key string |
|
53
|
|
|
* @return string|array|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function getJsConfig($key = ""){ |
|
56
|
|
|
return self::_get($key, self::$prefix['js']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set or add a javascript configuration value |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @param mixed $value |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function setJsConfig($key, $value){ |
|
66
|
|
|
self::_set($key, $value, self::$prefix['js']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get a configuration value(s) |
|
71
|
|
|
* |
|
72
|
|
|
* @param $key string |
|
73
|
|
|
* @param $source string |
|
74
|
|
|
* @return string|null |
|
75
|
|
|
* @throws Exception if configuration file doesn't exist |
|
76
|
|
|
*/ |
|
77
|
|
|
private static function _get($key, $source){ |
|
78
|
|
|
|
|
79
|
|
|
if (!isset(self::$config[$source])) { |
|
80
|
|
|
|
|
81
|
|
|
$config_file = APP . 'config/' . $source . '.php'; |
|
82
|
|
|
|
|
83
|
|
|
if (!file_exists($config_file)) { |
|
84
|
|
|
throw new Exception("Configuration file " . $source . " doesn't exist"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
self::$config[$source] = require $config_file . ""; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if(empty($key)){ |
|
91
|
|
|
return self::$config[$source]; |
|
92
|
|
|
} else if(isset(self::$config[$source][$key])){ |
|
93
|
|
|
return self::$config[$source][$key]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Set or adds a configuration value |
|
101
|
|
|
* |
|
102
|
|
|
* @param $key string |
|
103
|
|
|
* @param $value string |
|
104
|
|
|
* @param $source string |
|
105
|
|
|
*/ |
|
106
|
|
|
private static function _set($key, $value, $source){ |
|
107
|
|
|
|
|
108
|
|
|
// load configurations if not already loaded |
|
109
|
|
|
if (!isset(self::$config[$source])) { |
|
110
|
|
|
self::_get($key, $source); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if($key && $source){ |
|
114
|
|
|
self::$config[$source][$key] = $value; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.