1 | <?php |
||
16 | class Config |
||
17 | { |
||
18 | use SingletonTrait; |
||
19 | const DEFAULT_LANGUAGE = "es"; |
||
20 | const DEFAULT_ENCODE = "UTF-8"; |
||
21 | const DEFAULT_CTYPE = "text/html"; |
||
22 | const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
||
23 | |||
24 | const CONFIG_FILE = 'config.json'; |
||
25 | |||
26 | protected $config = array(); |
||
27 | static public $defaults = array( |
||
28 | "db_host" => "localhost", |
||
29 | "db_port" => "3306", |
||
30 | "default_language" => "es_ES", |
||
31 | "debug" => true, |
||
32 | "front.version" => "v1", |
||
33 | "version" => "v1", |
||
34 | ); |
||
35 | static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language', 'debug'); |
||
|
|||
36 | static public $encrypted = array('db_password'); |
||
37 | static public $optional = [ |
||
38 | 'platform_name', // Platform name |
||
39 | 'restricted', // Restrict the web access |
||
40 | 'admin_login', // Enable web login for admin |
||
41 | 'logger.phpFire', // Enable phpFire to trace the logs in the browser |
||
42 | 'logger.memory', // Enable log memory usage un traces |
||
43 | 'poweredBy', // Show PoweredBy header customized |
||
44 | 'author', // Author for auto generated files |
||
45 | 'author_email', // Author email for auto generated files |
||
46 | 'version', // Platform version(for cache purposes) |
||
47 | 'front.version', // Static resources version |
||
48 | 'cors.enabled', // Enable CORS (regex with the domains, * for all) |
||
49 | 'pagination.limit', // Pagination limit for autogenerated api admin |
||
50 | 'api.secret', // Secret passphrase to securize the api |
||
51 | 'api.admin', // Enable de autogenerated api admin(wok) |
||
52 | 'log.level', // Max log level(default INFO) |
||
53 | 'admin_action', // Default admin url when access to /admin |
||
54 | 'cache.var', // Static cache var |
||
55 | 'twig.auto_reload', // Enable or disable auto reload templates for twig |
||
56 | 'modules.extend', // Variable for extending the current functionality |
||
57 | 'psfs.auth', // Variable for extending PSFS with the AUTH module |
||
58 | 'errors.strict', // Variable to trace all strict errors |
||
59 | 'psfs.memcache', // Add Memcache to prod cache process, ONLY for PROD environments |
||
60 | ]; |
||
61 | protected $debug = false; |
||
62 | |||
63 | /** |
||
64 | * Config Constructor |
||
65 | */ |
||
66 | 8 | public function __construct() |
|
70 | |||
71 | /** |
||
72 | * Method that load the configuration data into the system |
||
73 | * @return Config |
||
74 | */ |
||
75 | 8 | protected function init() |
|
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | */ |
||
86 | 1 | public function isLoaded() { |
|
89 | |||
90 | /** |
||
91 | * Method that saves the configuration |
||
92 | * @param array $data |
||
93 | * @param array $extra |
||
94 | * @return array |
||
95 | */ |
||
96 | 2 | protected static function saveConfigParams(array $data, array $extra) |
|
110 | |||
111 | /** |
||
112 | * Method that saves the extra parameters into the configuration |
||
113 | * @param array $data |
||
114 | * @return array |
||
115 | */ |
||
116 | 2 | protected static function saveExtraParams(array $data) |
|
129 | |||
130 | /** |
||
131 | * Method that returns if the system is in debug mode |
||
132 | * @return boolean |
||
133 | */ |
||
134 | 6 | public function getDebugMode() |
|
138 | |||
139 | /** |
||
140 | * Method that checks if the platform is proper configured |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 2 | public function isConfigured() |
|
157 | |||
158 | /** |
||
159 | * Method that check if the user is trying to save the config |
||
160 | * @return bool |
||
161 | */ |
||
162 | 2 | public function checkTryToSaveConfig() |
|
168 | |||
169 | /** |
||
170 | * Method that saves all the configuration in the system |
||
171 | * |
||
172 | * @param array $data |
||
173 | * @param array|null $extra |
||
174 | * @return boolean |
||
175 | */ |
||
176 | 2 | public static function save(array $data, array $extra = null) |
|
177 | { |
||
178 | 2 | $data = self::saveConfigParams($data, $extra); |
|
179 | 2 | $final_data = self::saveExtraParams($data); |
|
180 | 2 | $saved = false; |
|
181 | try { |
||
182 | 2 | $final_data = array_filter($final_data, function($key, $value) { |
|
183 | 2 | return in_array($key, Config::$required) || !empty($value); |
|
184 | 2 | }, ARRAY_FILTER_USE_BOTH); |
|
185 | 2 | $saved = (false !== file_put_contents(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE, json_encode($final_data, JSON_PRETTY_PRINT))); |
|
186 | 2 | Config::getInstance()->loadConfigData(); |
|
187 | 2 | $saved = true; |
|
188 | } catch (ConfigException $e) { |
||
189 | Logger::log($e->getMessage(), LOG_ERR); |
||
190 | } |
||
191 | 2 | return $saved; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Method that returns a config value |
||
196 | * @param string $param |
||
197 | * @param mixed $defaultValue |
||
198 | * |
||
199 | * @return mixed|null |
||
200 | */ |
||
201 | 7 | public function get($param, $defaultValue = null) |
|
205 | |||
206 | /** |
||
207 | * Method that returns all the configuration |
||
208 | * @return array |
||
209 | */ |
||
210 | 2 | public function dumpConfig() |
|
214 | |||
215 | /** |
||
216 | * Method that reloads config file |
||
217 | */ |
||
218 | 2 | public function loadConfigData() |
|
223 | |||
224 | /** |
||
225 | * Clear configuration set |
||
226 | */ |
||
227 | 1 | public function clearConfig() |
|
231 | |||
232 | /** |
||
233 | * Static wrapper for extracting params |
||
234 | * @param string $key |
||
235 | * @param mixed|null $defaultValue |
||
236 | * @return mixed|null |
||
237 | */ |
||
238 | 6 | public static function getParam($key, $defaultValue = null) |
|
243 | } |
||
244 |
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.