Subversion Repositories Applications.framework

Rev

Rev 89 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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(
25
			'fw_fichier_config' => 'config.ini',
26
			'fw_chemin' => dirname(__FILE__).DS,
27
			'chemin_appli' => dirname($_SERVER['SCRIPT_FILENAME'].DS),
28
			);
29
		self::parserFichierIni(self::$parametres['fw_chemin'].self::$parametres['fw_fichier_config']);
30
		self::parserFichierIni(self::$parametres['chemin_appli'].self::$parametres['fw_fichier_config']);
31
 
32
	}
33
 
34
	private static function parserFichierIni($fichier_ini) {
35
    	$retour = false;
36
		if (file_exists($fichier_ini)) {
37
			$aso_ini = parse_ini_file($fichier_ini, true);
38
			$aso_ini = self::evaluerPhp($aso_ini);
39
			self::$parametres = array_merge(self::$parametres, $aso_ini);
40
	    	$retour = true;
41
    	}
42
    	return $retour;
43
	}
44
 
45
	public static function charger($fichier_ini) {
46
		self::verifierCreationInstance();
47
		return self::parserFichierIni($fichier_ini);
48
	}
49
 
50
	public static function get($param) {
51
		$retour = '';
52
		self::verifierCreationInstance();
53
		if (isset(self::$parametres[$param])) {
54
			$retour = self::$parametres[$param];
55
		}
56
		return $retour;
57
	}
58
 
59
	public static function getSection($section, $param = null) {
60
		$retour = '';
61
		self::verifierCreationInstance();
62
		if (isset(self::$parametres[$section])) {
63
			if (!is_null($param)) {
64
				if (isset(self::$parametres[$section][$param])) {
65
					$retour = self::$parametres[$section][$param];
66
				}
67
			} else {
68
				$retour = self::$parametres[$section];
69
			}
70
		}
71
		return $retour;
72
	}
73
 
74
	public static function existe($param) {
75
		$retour = false;
76
		self::verifierCreationInstance();
77
		if (isset(self::$parametres[$param])) {
78
			$retour = true;
79
		}
80
		return $retour;
81
	}
82
 
83
	public static function existeSection($section, $param = null) {
84
		$retour = false;
85
		self::verifierCreationInstance();
86
		if (isset(self::$parametres[$section])) {
87
			if (!is_null($param)) {
88
				if (isset(self::$parametres[$section][$param])) {
89
					$retour = true;
90
				}
91
			} else {
92
				$retour = true;
93
			}
94
		}
95
		return $retour;
96
	}
97
 
98
	private static function verifierCreationInstance() {
99
		if (empty(self::$instance)) {
100
			self::$instance = new Config();
101
		}
102
	}
103
 
104
	private static function evaluerPhp($tableau_a_analyser) {
105
		if (is_array($tableau_a_analyser)) {
106
			foreach ($tableau_a_analyser as $cle => $valeur) {
107
	    		if (is_array($valeur)) {
108
	    			$tableau_a_analyser[$cle] = self::evaluerPhp($valeur);
109
	    		} else {
110
					if (preg_match('/^php:(.+)$/', $valeur, $correspondances)) {
111
		    			eval('$tableau_a_analyser["'.$cle.'"] = '.$correspondances[1].';');
112
		    		} else {
113
		    			$tableau_a_analyser[$cle] = $valeur;
114
		    		}
115
	    		}
116
	    	}
117
		}
118
		return $tableau_a_analyser;
119
	}
120
}
121
?>