1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2022-2023 Rafael San José Tovar <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Core\Singletons; |
20
|
|
|
|
21
|
|
|
use Symfony\Component\Yaml\Yaml; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Config |
25
|
|
|
* |
26
|
|
|
* Carga la configuración y da soporte para su mantenimiento, leyéndola o guardándola |
27
|
|
|
* en un archivo yaml. |
28
|
|
|
* |
29
|
|
|
* @author Rafael San José Tovar <[email protected]> |
30
|
|
|
* |
31
|
|
|
* @package Alxarafe\Core\Singletons |
32
|
|
|
*/ |
33
|
|
|
abstract class Config |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Ruta completa del fichero de configuración |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static string $configFilename; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Contiene un array con el contenido íntegro del archivo de configuración. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private static array $global; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Carga el fichero de configuración en la variable $global. Las constantes definidas |
51
|
|
|
* en el mismo, se definen como constantes de php con 'define'. |
52
|
|
|
* |
53
|
|
|
* @author Rafael San José Tovar <[email protected]> |
54
|
|
|
* |
55
|
|
|
* @return false|void |
56
|
|
|
*/ |
57
|
|
|
public static function load() |
58
|
|
|
{ |
59
|
|
|
if (!isset(self::$global)) { |
60
|
|
|
self::$global = self::loadConfigurationFile(); |
61
|
|
|
if (empty(self::$global)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
self::defineConstants(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Define todas las constantes de la sección 'constants' del archivo config.yaml |
70
|
|
|
* La sección constants contiene las constantes en grupos de tipo. |
71
|
|
|
* TODO: De momento se contempla boolean y el resto. |
72
|
|
|
* |
73
|
|
|
* @author Rafael San José Tovar <[email protected]> |
74
|
|
|
*/ |
75
|
|
|
private static function defineConstants() |
76
|
|
|
{ |
77
|
|
|
foreach (self::$global['constants'] ?? [] as $type => $types) { |
78
|
|
|
foreach ($types as $name => $value) { |
79
|
|
|
switch ($type) { |
80
|
|
|
case 'boolean': |
81
|
|
|
define($name, in_array(strtolower($value), ['1', 'true'])); |
82
|
|
|
break; |
83
|
|
|
default: |
84
|
|
|
define($name, $value); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Carga el fichero de configuración y lo retorna como un array |
92
|
|
|
* |
93
|
|
|
* @author Rafael San José Tovar <[email protected]> |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
private static function loadConfigurationFile(): array |
98
|
|
|
{ |
99
|
|
|
$filename = self::getConfigFileName(); |
100
|
|
|
if (isset($filename) && file_exists($filename)) { |
101
|
|
|
$yaml = file_get_contents($filename); |
102
|
|
|
if ($yaml) { |
103
|
|
|
return YAML::parse($yaml); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retorna el nombre del fichero de configuración comprobando que la carpeta |
111
|
|
|
* que lo debe de contener existe. |
112
|
|
|
* Si la carpeta no existe, la crea. |
113
|
|
|
* |
114
|
|
|
* @return string|null |
115
|
|
|
*/ |
116
|
|
|
private static function getConfigFileName(): ?string |
117
|
|
|
{ |
118
|
|
|
if (isset(self::$configFilename)) { |
119
|
|
|
return self::$configFilename; |
120
|
|
|
} |
121
|
|
|
$filename = constant('CONFIGURATION_DIR') . 'config.yaml'; |
122
|
|
|
if ( |
123
|
|
|
file_exists($filename) || is_dir(constant('CONFIGURATION_DIR')) || mkdir(constant('CONFIGURATION_DIR'), 0777, true)) { |
124
|
|
|
self::$configFilename = $filename; |
125
|
|
|
} |
126
|
|
|
return self::$configFilename; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retorna el valor de una variable del archivo de configuración. |
131
|
|
|
* Si no existe, retorna null. |
132
|
|
|
* |
133
|
|
|
* @param string $module |
134
|
|
|
* @param string $section |
135
|
|
|
* @param string $name |
136
|
|
|
* |
137
|
|
|
* @return string|null ?string |
138
|
|
|
*/ |
139
|
|
|
public static function getVar(string $module, string $section, string $name): ?string |
140
|
|
|
{ |
141
|
|
|
return self::$global[$module][$section][$name] ?? null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Retorna un array con todas las variables de un módulo. |
146
|
|
|
* Si no existe el módulo, retorna null. |
147
|
|
|
* |
148
|
|
|
* @author Rafael San José Tovar <[email protected]> |
149
|
|
|
* |
150
|
|
|
* @param string $module |
151
|
|
|
* |
152
|
|
|
* @return array|null |
153
|
|
|
*/ |
154
|
|
|
public static function getModuleVar(string $module): ?array |
155
|
|
|
{ |
156
|
|
|
return self::$global[$module] ?? null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return true y the config file exists |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public static function configFileExists(): bool |
165
|
|
|
{ |
166
|
|
|
return (file_exists(self::getConfigFileName())); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Stores all the variables in a permanent file so that they can be loaded |
171
|
|
|
* later with loadConfigFile() |
172
|
|
|
* Returns true if there is no error when saving the file. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public static function saveConfigFile(): bool |
177
|
|
|
{ |
178
|
|
|
$configFile = self::getConfigFileName(); |
179
|
|
|
if (!isset($configFile)) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
return file_put_contents($configFile, YAML::dump(self::$global, 3)) !== false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Stores a variable. |
187
|
|
|
* |
188
|
|
|
* @param string $module |
189
|
|
|
* @param string $section |
190
|
|
|
* @param string $name |
191
|
|
|
* @param string $value |
192
|
|
|
*/ |
193
|
|
|
public static function setVar(string $module, string $section, string $name, string $value) |
194
|
|
|
{ |
195
|
|
|
self::$global[$module][$section][$name] = $value; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|