1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PSFS\base\config; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use PSFS\base\Cache; |
7
|
|
|
use PSFS\base\exception\ConfigException; |
8
|
|
|
use PSFS\base\Logger; |
9
|
|
|
use PSFS\base\types\SingletonTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Config |
13
|
|
|
* @package PSFS\base\config |
14
|
|
|
*/ |
15
|
|
|
class Config { |
16
|
|
|
|
17
|
|
|
use SingletonTrait; |
18
|
|
|
const DEFAULT_LANGUAGE = "es"; |
19
|
|
|
const DEFAULT_ENCODE = "UTF-8"; |
20
|
|
|
const DEFAULT_CTYPE = "text/html"; |
21
|
|
|
const DEFAULT_DATETIMEZONE = "Europe/Madrid"; |
22
|
|
|
|
23
|
|
|
protected $config = array(); |
24
|
|
|
static public $defaults = array( |
25
|
|
|
"db_host" => "localhost", |
26
|
|
|
"db_port" => "3306", |
27
|
|
|
"default_language" => "es_ES", |
28
|
|
|
); |
29
|
|
|
static public $required = array('db_host', 'db_port', 'db_name', 'db_user', 'db_password', 'home_action', 'default_language'); |
|
|
|
|
30
|
|
|
static public $encrypted = array('db_password'); |
31
|
|
|
static public $optional = array('platform_name', 'debug', 'restricted', 'admin_login', 'logger.phpFire', 'logger.memory', 'poweredBy', 'author', 'author_email', 'version', 'front.version', 'cors.enabled', 'pagination.limit', 'api.secret'); |
|
|
|
|
32
|
|
|
protected $debug = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Config Constructor |
36
|
|
|
*/ |
37
|
8 |
|
public function __construct() { |
38
|
8 |
|
$this->init(); |
39
|
8 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Método que carga la configuración del sistema |
43
|
|
|
* @return Config |
44
|
|
|
*/ |
45
|
8 |
|
protected function init() { |
46
|
8 |
|
if (file_exists(CONFIG_DIR.DIRECTORY_SEPARATOR."config.json")) |
47
|
8 |
|
{ |
48
|
7 |
|
$this->loadConfigData(); |
49
|
7 |
|
} else { |
50
|
1 |
|
$this->debug = true; |
51
|
|
|
} |
52
|
8 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Método que guarda los datos de la configuración |
57
|
|
|
* @param array $data |
58
|
|
|
* @param array $extra |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
2 |
|
protected static function saveConfigParams(array $data, array $extra) { |
62
|
|
|
//En caso de tener parámetros nuevos los guardamos |
63
|
2 |
|
if (!empty($extra['label'])) { |
64
|
1 |
|
foreach ($extra['label'] as $index => $field) { |
65
|
1 |
|
if (array_key_exists($index, $extra['value']) && !empty($extra['value'][$index])) { |
66
|
|
|
/** @var $data array */ |
67
|
1 |
|
$data[$field] = $extra['value'][$index]; |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
1 |
|
} |
71
|
2 |
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Método que guarda los parámetros adicionales de la configuración |
76
|
|
|
* @param array $data |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
2 |
|
protected static function saveExtraParams(array $data) { |
80
|
2 |
|
$final_data = array(); |
81
|
2 |
|
if (count($data) > 0) { |
82
|
2 |
|
foreach ($data as $key => $value) { |
83
|
2 |
|
if (null !== $value || $value !== '') { |
84
|
2 |
|
$final_data[$key] = $value; |
85
|
2 |
|
} |
86
|
2 |
|
} |
87
|
2 |
|
} |
88
|
2 |
|
return $final_data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Método que devuelve si la plataforma está en modo debug |
93
|
|
|
* @return boolean |
94
|
|
|
*/ |
95
|
|
|
public function getDebugMode() { return $this->debug; } |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Método que devuelve el path de cache |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getCachePath() { return CACHE_DIR; } |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Método que devuelve el path general de templates de PSFS |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
2 |
|
public function getTemplatePath() { |
108
|
2 |
|
$path = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR; |
|
|
|
|
109
|
2 |
|
return realpath($path); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Método que indica si se ha configurado correctamente la plataforma |
114
|
|
|
* @return boolean |
115
|
|
|
*/ |
116
|
3 |
|
public function isConfigured() |
117
|
|
|
{ |
118
|
3 |
|
$configured = true; |
119
|
3 |
|
foreach (static::$required as $required) { |
120
|
3 |
|
if (!array_key_exists($required, $this->config)) { |
121
|
3 |
|
$configured = false; |
122
|
3 |
|
break; |
123
|
|
|
} |
124
|
3 |
|
} |
125
|
3 |
|
return $configured; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Método que guarda la configuración del framework |
130
|
|
|
* |
131
|
|
|
* @param array $data |
132
|
|
|
* @param array|null $extra |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
2 |
|
public static function save(array $data, array $extra = null) { |
136
|
2 |
|
$data = self::saveConfigParams($data, $extra); |
137
|
2 |
|
$final_data = self::saveExtraParams($data); |
138
|
2 |
|
$saved = false; |
139
|
|
|
try { |
140
|
2 |
|
Cache::getInstance()->storeData(CONFIG_DIR.DIRECTORY_SEPARATOR."config.json", $final_data, Cache::JSON, true); |
|
|
|
|
141
|
2 |
|
$saved = true; |
142
|
2 |
|
}catch (ConfigException $e) { |
143
|
|
|
Logger::getInstance()->errorLog($e->getMessage()); |
144
|
|
|
} |
145
|
2 |
|
return $saved; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Método que devuelve un parámetro de configuración |
150
|
|
|
* @param string $param |
151
|
|
|
* |
152
|
|
|
* @return mixed|null |
153
|
|
|
*/ |
154
|
3 |
|
public function get($param) { |
155
|
3 |
|
return array_key_exists($param, $this->config) ? $this->config[$param] : null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Método que devuelve toda la configuración en un array |
160
|
|
|
* @return array|null |
161
|
|
|
*/ |
162
|
2 |
|
public function dumpConfig() { |
163
|
2 |
|
return $this->config; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Servicio que devuelve los parámetros de configuración de Propel para las BD |
168
|
|
|
* @return array|null |
|
|
|
|
169
|
|
|
*/ |
170
|
|
|
public function getPropelParams() { |
171
|
|
|
return Cache::getInstance()->getDataFromFile(__DIR__.DIRECTORY_SEPARATOR.'properties.json', Cache::JSON, true); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Método estático para la generación de directorios |
176
|
|
|
* @param string $dir |
177
|
|
|
* throws ConfigException |
178
|
|
|
*/ |
179
|
5 |
|
public static function createDir($dir) { |
180
|
|
|
try { |
181
|
5 |
|
if (@mkdir($dir, 0775, true) === false) { |
182
|
1 |
|
throw new \Exception(_('Can\'t create directory ').$dir); |
183
|
|
|
} |
184
|
5 |
|
}catch (\Exception $e) { |
185
|
4 |
|
if (!file_exists(dirname($dir))) { |
186
|
|
|
throw new ConfigException($e->getMessage().$dir); |
187
|
|
|
} |
188
|
|
|
} |
189
|
5 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Método estático que elimina los directorio del document root |
193
|
|
|
*/ |
194
|
1 |
|
public static function clearDocumentRoot() { |
195
|
1 |
|
$rootDirs = array("css", "js", "media", "font"); |
196
|
1 |
|
foreach ($rootDirs as $dir) { |
197
|
1 |
|
if (file_exists(WEB_DIR.DIRECTORY_SEPARATOR.$dir)) { |
198
|
|
|
try { |
199
|
1 |
|
@shell_exec("rm -rf ".WEB_DIR.DIRECTORY_SEPARATOR.$dir); |
200
|
1 |
|
} catch(\Exception $e) { |
201
|
|
|
Logger::getInstance()->errorLog($e->getMessage()); |
202
|
|
|
} |
203
|
1 |
|
} |
204
|
1 |
|
} |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Method that reloads config file |
209
|
|
|
*/ |
210
|
1 |
|
public function loadConfigData() |
211
|
|
|
{ |
212
|
1 |
|
$this->config = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "config.json", Cache::JSON, TRUE) ?: array(); |
|
|
|
|
213
|
1 |
|
$this->debug = (array_key_exists('debug', $this->config)) ? (bool)$this->config['debug'] : FALSE; |
214
|
1 |
|
} |
215
|
|
|
} |
216
|
|
|
|
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.