-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
172 lines (150 loc) · 4.32 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Managing the application's configuration
*/
class Config_Exception extends Exception {}
class Config
{
CONST CONFIG_NODES_SEPARATOR = '/';
protected static $_instance;
// Options list, here is default config
protected $_options = [
'plugins' => 'Plugin',
'router' => [
'default' => [
'controller' => 'index',
'action' => 'index'
],
'suffix' => [
'controller' => 'Controller',
'action' => 'Action'
],
'convertGetAsParams' => true,
'controllersFolder' => '/Controllers'
],
'view' => [
'extension' => '.phtml',
'folder' => '/Views',
'helpersFolder' => '/Helpers'
]
];
/**
* Get main config instance
*
* @return Config
*/
public static function getInstance()
{
if (self::$_instance == null)
self::$_instance = new self();
return self::$_instance;
}
/**
* Load config from an array
*
* @param array $options
* @param bool $merge
* merge (true) or replace (false) existing configuration
*/
public function loadConfig($options, $merge = true)
{
$this->_options = $merge
? self::array_merge_recursive($this->_options, $options)
: $options;
}
/**
* Get JSON data from a file and decode it as an associative array
*
* @param string $filename
* @throws Config_Exception
* @return array
*/
public static function getArrayFromJSONFile($filename)
{
if (!file_exists($filename))
throw new Config_Exception('"'.$filename.'" not found');
$config = @json_decode(file_get_contents($filename), true);
if (json_last_error() != JSON_ERROR_NONE)
throw new Config_Exception('Error while loading "'.$filename.'". JSON Error: '.json_last_error_msg());
return $config;
}
/**
* Load configuration data from a file
*
* @param string $filename
* @return Config
*/
public function loadFromJSONFile($filename, $merge = true)
{
$this->loadConfig(self::getArrayFromJSONFile($filename), $merge);
return $this;
}
/**
* Get config value from node name
*
* @param string $name
* @param mixed $defaultValue
* @return mixed
*/
public function getOption($name, $defaultValue = null)
{
$path = explode(self::CONFIG_NODES_SEPARATOR, $name);
$result = $defaultValue;
$node = $this->_options;
if (!empty($path))
{
$nodeFound = true;
foreach ($path as $field)
{
if (isset($node[$field]))
$node = $node[$field];
else
{
$nodeFound = false;
break;
}
}
if ($nodeFound)
$result = $node;
}
return $result;
}
/**
* Set config value
*
* @param string $name
* @param mixed $value
*/
public function setOption($name, $value)
{
$path = explode(self::CONFIG_NODES_SEPARATOR, $name);
$temp = &$this->_options;
foreach($path as $key) {
$temp = &$temp[$key];
}
$temp = $value;
unset($temp);
}
/**
* Merge two multi-dimmensional arrays.
* On duplicate value it takes one from the second array
*
* @param array $array1
* @param array $array2
* @return array
*/
private static function array_merge_recursive(array $array1, array $array2)
{
$merged = $array1;
foreach ($array2 as $key => & $value)
{
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key]))
$merged[$key] = self::array_merge_recursive($merged[$key], $value);
else if (is_numeric($key) && !in_array($value, $merged))
$merged[] = $value;
else
$merged[$key] = $value;
}
return $merged;
}
}