forked from Siwayll/deuton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
223 lines (197 loc) · 6.03 KB
/
Copy pathConfig.php
File metadata and controls
223 lines (197 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Gestionnaire des fichiers de configurations
*
* @package Deuton
* @subpackage Core
* @author Siwaÿll <sanath.labs@gmail.com>
* @license beerware http://wikipedia.org/wiki/Beerware
*/
namespace Siwayll\Deuton;
/**
* Gestionnaire des fichiers de configurations
*
* @package Deuton
* @subpackage Core
* @author Siwaÿll <sanath.labs@gmail.com>
* @license beerware http://wikipedia.org/wiki/Beerware
*/
class Config
{
/**
* Nom de la section de configuration du .ini
*/
const KEY_CONF = '__config';
/**
* Format des variables
*/
const VAR_FORMAT = '#{%([a-z0-9_:]+)}#i';
/**
* Contenu du fichier de config
*
* @var array
*/
private $config = null;
private $headerConfig = null;
/**
* Charge un nouveau fichier de configuration
*
* @param string $iniFile Chemin vers le fichier de configuration
*
* @uses Path Contrôle du chemin du fichier
*/
public function __construct($iniFile)
{
$iniPath = new Path($iniFile);
$confPath = $iniPath->get();
if ($confPath !== false) {
$this->config = parse_ini_file($confPath, true);
}
$this->headerConfig = $config = $this->get(self::KEY_CONF);
unset($this->config[self::KEY_CONF]);
/** Extends **/
if (isset($config['extends'])) {
$extends = $config['extends'];
if (!is_array($extends)) {
$extends = array($extends);
}
foreach ($extends as $path) {
$this->setExtends($path);
}
}
$this->parseVar();
}
/**
* Renvois la section de configuration du .ini
*
* @return array
*/
public function getConfig()
{
return $this->headerConfig;
}
/**
* Fait hériter le fichier de configuration d'un autre fichier.
* Permet d'incorporer un fichier de configuration par défaut qui sera
* surchargé par le fichier actuel.
*
* @param string $path Chemin vers le fichier de configuration "défaut"
*
* @return void
*/
public function setExtends($path)
{
$iniPath = new Path($path);
$confPath = $iniPath->get();
if ($confPath !== false) {
$configBase = parse_ini_file($confPath, true);
}
$this->config = $this->arrayMerge($configBase, $this->config);
}
/**
* Merge les tableaux en replaçants les clés identiques
*
* @param array &$array1 Tableau à merge
* @param array &$array2 Tableau à merge
*
* @return array
*/
private function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value)
&& isset($merged[$key])
&& is_array($merged [$key])
) {
$merged [$key] = $this->arrayMerge($merged [$key], $value);
} else {
$merged [$key] = $value;
}
}
return $merged;
}
/**
* Application des variables
*
* @return void
*/
private function parseVar()
{
/** Parcour des options de configurations **/
foreach ($this->config as $divName => $section) {
/**
* on test si dans la section il y a une variable
* ça permet de passer à la suivante sans avoir à tout tester
**/
$testString = '';
foreach ($section as $value) {
$testString .= $value;
}
if (!preg_match(self::VAR_FORMAT, $testString)) {
continue;
}
foreach ($section as $key => $value) {
if (preg_match_all(self::VAR_FORMAT, $value, $matches)) {
$max = count($matches[0]);
for ($i = 0; $i < $max; $i++) {
$id = $matches[1][$i];
/* = Si il y a un : dans le nom de la variable c'est
| qu'elle pointe sur un autre bloc
| sinon on prend le bloc en cours
`------------------------------------------------- */
if (strpos($id, ':') !== false) {
$opt = explode(':', $id);
$val = $this->get($opt[0], $opt[1]);
} else {
$val = $this->get($divName, $id);
}
if ($val === null) {
throw new Exception(
'Aucune correspondance pour ' . $id
);
}
/* = On replace la valeur de la variable dans le champ
`------------------------------------------------- */
$value = str_replace(
$matches[0][$i],
$val,
$value
);
$this->config[$divName][$key] = $value;
}
}
}
}
}
/**
* Renvois le contenu du fichier de configuration
*
* @return array Tableau de la configuration
*/
public function getAll()
{
return $this->config;
}
/**
* Renvois la valeur d'un parametre de configuration
*
* @param string $section Code de la section
* @param string $key Nom de la clé de configuration
*
* @return mixed null si aucune configuration ne répond aux critères
*/
public function get($section, $key = null)
{
if (!empty($key)) {
if (isset($this->config[$section][$key])) {
return $this->config[$section][$key];
}
} else {
if (isset($this->config[$section])) {
return $this->config[$section];
}
}
return null;
}
}