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
|
|
|
public static $config = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Array of javascript configurations |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public static $jsConfig = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Gets a configuration value |
29
|
|
|
* |
30
|
|
|
* @param $key string |
31
|
|
|
* @return string|null |
32
|
|
|
* @throws Exception if configuration file doesn't exist |
33
|
|
|
*/ |
34
|
|
|
public static function get($key){ |
35
|
|
|
|
36
|
|
View Code Duplication |
if (!self::$config) { |
|
|
|
|
37
|
|
|
|
38
|
|
|
$config_file = APP . 'config/config.php'; |
39
|
|
|
|
40
|
|
|
if (!file_exists($config_file)) { |
41
|
|
|
throw new Exception("Configuration file doesn't exist"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
self::$config = require $config_file . ""; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return isset(self::$config[$key])? self::$config[$key]: null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Loads javascript configurations |
52
|
|
|
* |
53
|
|
|
* @param $key string |
54
|
|
|
* @return string|null |
55
|
|
|
* @throws Exception if configuration file doesn't exist |
56
|
|
|
*/ |
57
|
|
|
private static function loadJsConfig(){ |
58
|
|
|
|
59
|
|
View Code Duplication |
if (!self::$jsConfig) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
$config_file = APP . 'config/javascript.php'; |
62
|
|
|
|
63
|
|
|
if (!file_exists($config_file)) { |
64
|
|
|
throw new Exception("JavaScript Configuration file doesn't exist"); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
self::$jsConfig = require $config_file . ""; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets javascript configuration value(s) |
73
|
|
|
* |
74
|
|
|
* @param $key string |
75
|
|
|
* @return string|array|null |
76
|
|
|
* @throws Exception if configuration file doesn't exist |
77
|
|
|
*/ |
78
|
|
|
public static function getJsConfig($key = ""){ |
79
|
|
|
|
80
|
|
|
if (!self::$jsConfig) { |
|
|
|
|
81
|
|
|
self::loadJsConfig(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if(empty($key)){ |
85
|
|
|
return self::$jsConfig; |
86
|
|
|
}else if(isset(self::$jsConfig[$key])){ |
87
|
|
|
return self::$jsConfig[$key]; |
88
|
|
|
} |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Adds a new variable to javascript configurations |
94
|
|
|
* |
95
|
|
|
* @param string $key |
96
|
|
|
* @param mixed $value |
97
|
|
|
*/ |
98
|
|
|
public static function addJsConfig($key, $value){ |
99
|
|
|
if (!self::$jsConfig) { |
|
|
|
|
100
|
|
|
self::loadJsConfig(); |
101
|
|
|
} |
102
|
|
|
self::$jsConfig[$key] = $value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.