Subversion Repositories Applications.framework

Rev

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