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 | ]; |
||
58 | protected $debug = false; |
||
59 | |||
60 | /** |
||
61 | * Config Constructor |
||
62 | */ |
||
63 | 8 | public function __construct() |
|
67 | |||
68 | /** |
||
69 | * Method that load the configuration data into the system |
||
70 | * @return Config |
||
71 | */ |
||
72 | 8 | protected function init() |
|
73 | { |
||
74 | 8 | if (file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE)) { |
|
75 | 7 | $this->loadConfigData(); |
|
76 | } |
||
77 | 8 | return $this; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Method that saves the configuration |
||
82 | * @param array $data |
||
83 | * @param array $extra |
||
84 | * @return array |
||
85 | */ |
||
86 | 2 | protected static function saveConfigParams(array $data, array $extra) |
|
87 | { |
||
88 | 2 | Logger::log('Saving required config parameters'); |
|
89 | //En caso de tener parámetros nuevos los guardamos |
||
90 | 2 | if (array_key_exists('label', $extra) && is_array($extra['label'])) { |
|
91 | 1 | foreach ($extra['label'] as $index => $field) { |
|
92 | 1 | if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
|
93 | /** @var $data array */ |
||
94 | 1 | $data[$field] = $extra['value'][$index]; |
|
95 | } |
||
96 | } |
||
97 | } |
||
98 | 2 | return $data; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Method that saves the extra parameters into the configuration |
||
103 | * @param array $data |
||
104 | * @return array |
||
105 | */ |
||
106 | 2 | protected static function saveExtraParams(array $data) |
|
107 | { |
||
108 | 2 | $final_data = array(); |
|
109 | 2 | if (count($data) > 0) { |
|
110 | 2 | Logger::log('Saving extra configuration parameters'); |
|
111 | 2 | foreach ($data as $key => $value) { |
|
112 | 2 | if (null !== $value || $value !== '') { |
|
113 | 2 | $final_data[$key] = $value; |
|
114 | } |
||
115 | } |
||
116 | } |
||
117 | 2 | return $final_data; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Method that returns if the system is in debug mode |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 6 | public function getDebugMode() |
|
128 | |||
129 | /** |
||
130 | * Method that checks if the platform is proper configured |
||
131 | * @return boolean |
||
132 | */ |
||
133 | 2 | public function isConfigured() |
|
134 | { |
||
135 | 2 | Logger::log('Checking configuration'); |
|
136 | 2 | $configured = (count($this->config) > 0); |
|
137 | 2 | if ($configured) { |
|
138 | 1 | foreach (static::$required as $required) { |
|
139 | 1 | if (!array_key_exists($required, $this->config)) { |
|
140 | 1 | $configured = false; |
|
141 | 1 | break; |
|
142 | } |
||
143 | } |
||
144 | } |
||
145 | 2 | return ($configured || $this->checkTryToSaveConfig()); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Method that check if the user is trying to save the config |
||
150 | * @return bool |
||
151 | */ |
||
152 | 2 | public function checkTryToSaveConfig() |
|
158 | |||
159 | /** |
||
160 | * Method that saves all the configuration in the system |
||
161 | * |
||
162 | * @param array $data |
||
163 | * @param array|null $extra |
||
164 | * @return boolean |
||
165 | */ |
||
166 | 2 | public static function save(array $data, array $extra = null) |
|
167 | { |
||
168 | 2 | $data = self::saveConfigParams($data, $extra); |
|
169 | 2 | $final_data = self::saveExtraParams($data); |
|
170 | 2 | $saved = false; |
|
171 | try { |
||
172 | 2 | Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . self::CONFIG_FILE, $final_data, Cache::JSON, true); |
|
173 | 2 | Config::getInstance()->loadConfigData(); |
|
174 | 2 | $saved = true; |
|
175 | } catch (ConfigException $e) { |
||
176 | Logger::log($e->getMessage(), LOG_ERR); |
||
177 | } |
||
178 | 2 | return $saved; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * Method that returns a config value |
||
183 | * @param string $param |
||
184 | * @param mixed $defaultValue |
||
185 | * |
||
186 | * @return mixed|null |
||
187 | */ |
||
188 | 4 | public function get($param, $defaultValue = null) |
|
192 | |||
193 | /** |
||
194 | * Method that returns all the configuration |
||
195 | * @return array |
||
196 | */ |
||
197 | 2 | public function dumpConfig() |
|
201 | |||
202 | /** |
||
203 | * Method that reloads config file |
||
204 | */ |
||
205 | 2 | public function loadConfigData() |
|
212 | |||
213 | /** |
||
214 | * Clear configuration set |
||
215 | */ |
||
216 | 1 | public function clearConfig() |
|
220 | |||
221 | /** |
||
222 | * Static wrapper for extracting params |
||
223 | * @param string $key |
||
224 | * @param mixed|null $defaultValue |
||
225 | * @return mixed|null |
||
226 | */ |
||
227 | 2 | public static function getParam($key, $defaultValue = null) |
|
232 | } |
||
233 |
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.