New file |
0,0 → 1,121 |
<?php |
/** |
* Config permet de charger automatiquement les fichiers ini du Framework et de l'application. |
* Elle offre l'accès en lecture seule aux paramêtres de config. |
* C'est une Singleton. |
* |
* PHP Version 5 |
* |
* @category PHP |
* @package Framework |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org> |
* @copyright 2009 Tela-Botanica |
* @license GPL-v3 et CECILL-v2 |
* @version SVN: <svn_id> |
* @link /doc/framework/ |
*/ |
|
class Config { |
|
private static $instance = null; |
private static $parametres = array(); |
|
private function __construct() { |
self::$parametres = array( |
'fw_fichier_config' => 'config.ini', |
'fw_chemin' => dirname(__FILE__).DS, |
'chemin_appli' => dirname($_SERVER['SCRIPT_FILENAME'].DS), |
); |
self::parserFichierIni(self::$parametres['fw_chemin'].self::$parametres['fw_fichier_config']); |
self::parserFichierIni(self::$parametres['chemin_appli'].self::$parametres['fw_fichier_config']); |
|
} |
|
private static function parserFichierIni($fichier_ini) { |
$retour = false; |
if (file_exists($fichier_ini)) { |
$aso_ini = parse_ini_file($fichier_ini, true); |
$aso_ini = self::evaluerPhp($aso_ini); |
self::$parametres = array_merge(self::$parametres, $aso_ini); |
$retour = true; |
} |
return $retour; |
} |
|
public static function charger($fichier_ini) { |
self::verifierCreationInstance(); |
return self::parserFichierIni($fichier_ini); |
} |
|
public static function get($param) { |
$retour = ''; |
self::verifierCreationInstance(); |
if (isset(self::$parametres[$param])) { |
$retour = self::$parametres[$param]; |
} |
return $retour; |
} |
|
public static function getSection($section, $param = null) { |
$retour = ''; |
self::verifierCreationInstance(); |
if (isset(self::$parametres[$section])) { |
if (!is_null($param)) { |
if (isset(self::$parametres[$section][$param])) { |
$retour = self::$parametres[$section][$param]; |
} |
} else { |
$retour = self::$parametres[$section]; |
} |
} |
return $retour; |
} |
|
public static function existe($param) { |
$retour = false; |
self::verifierCreationInstance(); |
if (isset(self::$parametres[$param])) { |
$retour = true; |
} |
return $retour; |
} |
|
public static function existeSection($section, $param = null) { |
$retour = false; |
self::verifierCreationInstance(); |
if (isset(self::$parametres[$section])) { |
if (!is_null($param)) { |
if (isset(self::$parametres[$section][$param])) { |
$retour = true; |
} |
} else { |
$retour = true; |
} |
} |
return $retour; |
} |
|
private static function verifierCreationInstance() { |
if (empty(self::$instance)) { |
self::$instance = new Config(); |
} |
} |
|
private static function evaluerPhp($tableau_a_analyser) { |
if (is_array($tableau_a_analyser)) { |
foreach ($tableau_a_analyser as $cle => $valeur) { |
if (is_array($valeur)) { |
$tableau_a_analyser[$cle] = self::evaluerPhp($valeur); |
} else { |
if (preg_match('/^php:(.+)$/', $valeur, $correspondances)) { |
eval('$tableau_a_analyser["'.$cle.'"] = '.$correspondances[1].';'); |
} else { |
$tableau_a_analyser[$cle] = $valeur; |
} |
} |
} |
} |
return $tableau_a_analyser; |
} |
} |
?> |