| 80 |
jpm |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Config permet de charger automatiquement les fichiers ini du Framework et de l'application.
|
|
|
4 |
* Elle offre l'accès en lecture seule aux paramêtres de config.
|
|
|
5 |
* C'est une Singleton.
|
|
|
6 |
*
|
|
|
7 |
* PHP Version 5
|
|
|
8 |
*
|
|
|
9 |
* @category PHP
|
|
|
10 |
* @package Framework
|
|
|
11 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
12 |
* @copyright 2009 Tela-Botanica
|
|
|
13 |
* @license GPL-v3 et CECILL-v2
|
|
|
14 |
* @version SVN: <svn_id>
|
|
|
15 |
* @link /doc/framework/
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
class Config {
|
|
|
19 |
|
|
|
20 |
private static $instance = null;
|
|
|
21 |
private static $parametres = array();
|
|
|
22 |
|
|
|
23 |
private function __construct() {
|
|
|
24 |
self::$parametres = array(
|
| 89 |
jpm |
25 |
'fw_fichier_config' => 'config%s.ini',
|
| 91 |
jpm |
26 |
'fw_chemin' => dirname(__FILE__).DS
|
| 80 |
jpm |
27 |
);
|
| 89 |
jpm |
28 |
self::parserFichierIni(self::$parametres['fw_chemin'].sprintf(self::$parametres['fw_fichier_config'], ''));
|
| 80 |
jpm |
29 |
|
| 89 |
jpm |
30 |
$chemin_config_appli_contextuel = self::$parametres['dossier_configurations'];
|
|
|
31 |
if (defined('PAP_VERSION')) {
|
|
|
32 |
$chemin_config_appli_contextuel .= sprintf(self::$parametres['fw_fichier_config'], '_papyrus');
|
|
|
33 |
} else {
|
|
|
34 |
$chemin_config_appli_contextuel .= sprintf(self::$parametres['fw_fichier_config'], '');
|
|
|
35 |
}
|
|
|
36 |
self::parserFichierIni($chemin_config_appli_contextuel);
|
|
|
37 |
|
| 80 |
jpm |
38 |
}
|
|
|
39 |
|
|
|
40 |
private static function parserFichierIni($fichier_ini) {
|
|
|
41 |
$retour = false;
|
|
|
42 |
if (file_exists($fichier_ini)) {
|
| 91 |
jpm |
43 |
$ini = parse_ini_file($fichier_ini, true);
|
|
|
44 |
$ini = self::evaluerPhp($ini);
|
|
|
45 |
self::fusionner($ini);
|
| 80 |
jpm |
46 |
$retour = true;
|
|
|
47 |
}
|
|
|
48 |
return $retour;
|
|
|
49 |
}
|
|
|
50 |
|
| 91 |
jpm |
51 |
private static function fusionner(array $ini) {
|
|
|
52 |
self::$parametres = array_merge(self::$parametres, $ini);
|
|
|
53 |
}
|
|
|
54 |
|
| 80 |
jpm |
55 |
public static function charger($fichier_ini) {
|
|
|
56 |
self::verifierCreationInstance();
|
|
|
57 |
return self::parserFichierIni($fichier_ini);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public static function get($param) {
|
|
|
61 |
$retour = '';
|
|
|
62 |
self::verifierCreationInstance();
|
|
|
63 |
if (isset(self::$parametres[$param])) {
|
|
|
64 |
$retour = self::$parametres[$param];
|
|
|
65 |
}
|
|
|
66 |
return $retour;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public static function getSection($section, $param = null) {
|
|
|
70 |
$retour = '';
|
|
|
71 |
self::verifierCreationInstance();
|
|
|
72 |
if (isset(self::$parametres[$section])) {
|
|
|
73 |
if (!is_null($param)) {
|
|
|
74 |
if (isset(self::$parametres[$section][$param])) {
|
|
|
75 |
$retour = self::$parametres[$section][$param];
|
|
|
76 |
}
|
|
|
77 |
} else {
|
|
|
78 |
$retour = self::$parametres[$section];
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
return $retour;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public static function existe($param) {
|
|
|
85 |
$retour = false;
|
|
|
86 |
self::verifierCreationInstance();
|
|
|
87 |
if (isset(self::$parametres[$param])) {
|
|
|
88 |
$retour = true;
|
|
|
89 |
}
|
|
|
90 |
return $retour;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public static function existeSection($section, $param = null) {
|
|
|
94 |
$retour = false;
|
|
|
95 |
self::verifierCreationInstance();
|
|
|
96 |
if (isset(self::$parametres[$section])) {
|
|
|
97 |
if (!is_null($param)) {
|
|
|
98 |
if (isset(self::$parametres[$section][$param])) {
|
|
|
99 |
$retour = true;
|
|
|
100 |
}
|
|
|
101 |
} else {
|
|
|
102 |
$retour = true;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
return $retour;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
private static function verifierCreationInstance() {
|
|
|
109 |
if (empty(self::$instance)) {
|
|
|
110 |
self::$instance = new Config();
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private static function evaluerPhp($tableau_a_analyser) {
|
|
|
115 |
if (is_array($tableau_a_analyser)) {
|
|
|
116 |
foreach ($tableau_a_analyser as $cle => $valeur) {
|
|
|
117 |
if (is_array($valeur)) {
|
|
|
118 |
$tableau_a_analyser[$cle] = self::evaluerPhp($valeur);
|
|
|
119 |
} else {
|
|
|
120 |
if (preg_match('/^php:(.+)$/', $valeur, $correspondances)) {
|
|
|
121 |
eval('$tableau_a_analyser["'.$cle.'"] = '.$correspondances[1].';');
|
|
|
122 |
} else {
|
|
|
123 |
$tableau_a_analyser[$cle] = $valeur;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
return $tableau_a_analyser;
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
?>
|