1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base\config; |
3
|
|
|
|
4
|
|
|
use PSFS\base\exception\ConfigException; |
5
|
|
|
use PSFS\base\Logger; |
6
|
|
|
use PSFS\base\Request; |
7
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Config |
11
|
|
|
* @package PSFS\base\config |
12
|
|
|
*/ |
13
|
|
|
class Config |
14
|
|
|
{ |
15
|
|
|
use SingletonTrait; |
16
|
|
|
|
17
|
|
|
const DEFAULT_LANGUAGE = "es"; |
18
|
|
|
const DEFAULT_ENCODE = "UTF-8"; |
19
|
|
|
const DEFAULT_CTYPE = "text/html"; |
20
|
|
|
const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
21
|
|
|
|
22
|
|
|
const CONFIG_FILE = 'config.json'; |
23
|
|
|
|
24
|
|
|
protected $config = array(); |
25
|
|
|
static public $defaults = array( |
26
|
|
|
"db_host" => "localhost", |
27
|
|
|
"db_port" => "3306", |
28
|
|
|
"default_language" => "es_ES", |
29
|
|
|
"debug" => true, |
30
|
|
|
"front.version" => "v1", |
31
|
|
|
"version" => "v1", |
32
|
|
|
); |
33
|
|
|
static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language', 'debug'); |
|
|
|
|
34
|
|
|
static public $encrypted = array('db_password'); |
35
|
|
|
static public $optional = [ |
36
|
|
|
'platform_name', // Platform name |
37
|
|
|
'restricted', // Restrict the web access |
38
|
|
|
'admin_login', // Enable web login for admin |
39
|
|
|
'logger.phpFire', // Enable phpFire to trace the logs in the browser |
40
|
|
|
'logger.memory', // Enable log memory usage un traces |
41
|
|
|
'poweredBy', // Show PoweredBy header customized |
42
|
|
|
'author', // Author for auto generated files |
43
|
|
|
'author_email', // Author email for auto generated files |
44
|
|
|
'version', // Platform version(for cache purposes) |
45
|
|
|
'front.version', // Static resources version |
46
|
|
|
'cors.enabled', // Enable CORS (regex with the domains, * for all) |
47
|
|
|
'pagination.limit', // Pagination limit for autogenerated api admin |
48
|
|
|
'api.secret', // Secret passphrase to securize the api |
49
|
|
|
'api.admin', // Enable de autogenerated api admin(wok) |
50
|
|
|
'log.level', // Max log level(default INFO) |
51
|
|
|
'admin_action', // Default admin url when access to /admin |
52
|
|
|
'cache.var', // Static cache var |
53
|
|
|
'twig.auto_reload', // Enable or disable auto reload templates for twig |
54
|
|
|
'modules.extend', // Variable for extending the current functionality |
55
|
|
|
'psfs.auth', // Variable for extending PSFS with the AUTH module |
56
|
|
|
'errors.strict', // Variable to trace all strict errors |
57
|
|
|
'psfs.memcache', // Add Memcache to prod cache process, ONLY for PROD environments |
58
|
|
|
]; |
59
|
|
|
protected $debug = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Config Constructor |
63
|
|
|
*/ |
64
|
|
|
public function __construct() |
65
|
|
|
{ |
66
|
|
|
$this->init(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Method that load the configuration data into the system |
71
|
|
|
* @return Config |
72
|
|
|
*/ |
73
|
|
|
protected function init() |
74
|
|
|
{ |
75
|
|
|
if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE)) { |
76
|
|
|
$this->loadConfigData(); |
77
|
|
|
} |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function isLoaded() { |
85
|
|
|
return !empty($this->config); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Method that saves the configuration |
90
|
|
|
* @param array $data |
91
|
|
|
* @param array $extra |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
2 |
|
protected static function saveConfigParams(array $data, array $extra) |
95
|
|
|
{ |
96
|
2 |
|
Logger::log('Saving required config parameters'); |
97
|
|
|
//En caso de tener parámetros nuevos los guardamos |
98
|
2 |
|
if (array_key_exists('label', $extra) && is_array($extra['label'])) { |
99
|
1 |
|
foreach ($extra['label'] as $index => $field) { |
100
|
1 |
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
101
|
|
|
/** @var $data array */ |
102
|
1 |
|
$data[$field] = $extra['value'][$index]; |
103
|
1 |
|
} |
104
|
1 |
|
} |
105
|
1 |
|
} |
106
|
2 |
|
return $data; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Method that saves the extra parameters into the configuration |
111
|
|
|
* @param array $data |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
2 |
|
protected static function saveExtraParams(array $data) |
115
|
|
|
{ |
116
|
2 |
|
$final_data = array(); |
117
|
2 |
|
if (count($data) > 0) { |
118
|
2 |
|
Logger::log('Saving extra configuration parameters'); |
119
|
2 |
|
foreach ($data as $key => $value) { |
120
|
2 |
|
if (null !== $value || $value !== '') { |
121
|
2 |
|
$final_data[$key] = $value; |
122
|
2 |
|
} |
123
|
2 |
|
} |
124
|
2 |
|
} |
125
|
2 |
|
return $final_data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Method that returns if the system is in debug mode |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
6 |
|
public function getDebugMode() |
133
|
|
|
{ |
134
|
6 |
|
return $this->debug; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Method that checks if the platform is proper configured |
139
|
|
|
* @return boolean |
140
|
|
|
*/ |
141
|
2 |
|
public function isConfigured() |
142
|
|
|
{ |
143
|
2 |
|
Logger::log('Checking configuration'); |
144
|
2 |
|
$configured = (count($this->config) > 0); |
145
|
2 |
|
if ($configured) { |
146
|
1 |
|
foreach (static::$required as $required) { |
147
|
1 |
|
if (!array_key_exists($required, $this->config)) { |
148
|
1 |
|
$configured = false; |
149
|
1 |
|
break; |
150
|
|
|
} |
151
|
1 |
|
} |
152
|
1 |
|
} |
153
|
2 |
|
return ($configured || $this->checkTryToSaveConfig()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Method that check if the user is trying to save the config |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
2 |
|
public function checkTryToSaveConfig() |
161
|
|
|
{ |
162
|
2 |
|
$uri = Request::getInstance()->getRequestUri(); |
163
|
2 |
|
$method = Request::getInstance()->getMethod(); |
164
|
2 |
|
return (preg_match('/^\/admin\/(config|setup)$/', $uri) !== false && strtoupper($method) === 'POST'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Method that saves all the configuration in the system |
169
|
|
|
* |
170
|
|
|
* @param array $data |
171
|
|
|
* @param array|null $extra |
172
|
|
|
* @return boolean |
173
|
|
|
*/ |
174
|
2 |
|
public static function save(array $data, array $extra = null) |
175
|
|
|
{ |
176
|
2 |
|
$data = self::saveConfigParams($data, $extra); |
177
|
2 |
|
$final_data = self::saveExtraParams($data); |
178
|
2 |
|
$saved = false; |
179
|
|
|
try { |
180
|
2 |
|
$final_data = array_filter($final_data, function($key, $value) { |
181
|
2 |
|
return in_array($key, Config::$required) || !empty($value); |
182
|
2 |
|
}, ARRAY_FILTER_USE_BOTH); |
183
|
2 |
|
$saved = (false !== file_put_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE, json_encode($final_data, JSON_PRETTY_PRINT))); |
|
|
|
|
184
|
2 |
|
Config::getInstance()->loadConfigData(); |
185
|
2 |
|
$saved = true; |
186
|
2 |
|
} catch (ConfigException $e) { |
187
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
188
|
|
|
} |
189
|
2 |
|
return $saved; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Method that returns a config value |
194
|
|
|
* @param string $param |
195
|
|
|
* @param mixed $defaultValue |
196
|
|
|
* |
197
|
|
|
* @return mixed|null |
198
|
|
|
*/ |
199
|
5 |
|
public function get($param, $defaultValue = null) |
200
|
|
|
{ |
201
|
5 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : $defaultValue; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Method that returns all the configuration |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
2 |
|
public function dumpConfig() |
209
|
|
|
{ |
210
|
2 |
|
return $this->config ?: []; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Method that reloads config file |
215
|
|
|
*/ |
216
|
2 |
|
public function loadConfigData() |
217
|
|
|
{ |
218
|
2 |
|
$this->config = json_decode(file_get_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE), true) ?: []; |
|
|
|
|
219
|
2 |
|
$this->debug = (array_key_exists('debug', $this->config)) ? (bool)$this->config['debug'] : FALSE; |
220
|
2 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Clear configuration set |
224
|
|
|
*/ |
225
|
1 |
|
public function clearConfig() |
226
|
|
|
{ |
227
|
1 |
|
$this->config = []; |
228
|
1 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Static wrapper for extracting params |
232
|
|
|
* @param string $key |
233
|
|
|
* @param mixed|null $defaultValue |
234
|
|
|
* @return mixed|null |
235
|
|
|
*/ |
236
|
4 |
|
public static function getParam($key, $defaultValue = null) |
237
|
|
|
{ |
238
|
4 |
|
$param = Config::getInstance()->get($key); |
239
|
4 |
|
return (null !== $param) ? $param : $defaultValue; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.