class config (.ini) de votre application et stockage en cache en php
les informaticiens avec obama.. |
Page d'accueil
| requete HTTP et reponse avec curl ..
: Ajouté le
18/8/2009 Ã
15:39
class Config
{
private $_file_ini = "";
private $_file_ser = "";
private $content = array();
/**
* return a config for a site and load config file
* @param String $file path of the config file
* @return an objet
*/
public function __construct( $file )
{
if(file_exists($file)) {
$this->_file_ini = $file ;
$this->_file_ser = $this->_file_ini .'.php';
$this->getCache( );
}
}
/**
* return configuration value
* @param String $sectionName The section will contain the parameter name
* @param String $paramName will contani the parameter name
* @return the parameter value
*/
public function getParameterByName( $sectionName=null, $paramName=null )
{
if(is_array($this->content)) {
if($sectionName !== null && isset($this->content[ $sectionName ])) {
if($paramName !== null && isset($this->content[ $sectionName ][ $paramName ])) {
return $this->content[ $sectionName ][ $paramName ];
} elseif ($paramName !== null && !isset($this->content[ $sectionName ][ $paramName ])) {
} else {
return $this->content[ $sectionName ];
}
} elseif ($sectionName !== null && !isset($this->content[ $sectionName ])) {
} else {
return $this->content;
}
}
return null;
}
/**
* return informations in cache
* @return bool true for successful loading config or false
*/
private function getCache( )
{
if ( file_exists( $this->_file_ser )){
$unserializeContent = file_get_contents( $this->_file_ser );
// var_dump( $unserializeContent );
if( $unserializeContent ) {
$unserializeContent = str_replace( '<?php return false; ?>', '', $unserializeContent );
$result = unserialize ( $unserializeContent );
if(is_array($result) && isset($result['md5_file'])) {
if( $result['md5_file'] == Md5_file( $this->_file_ini ) ) {
$this->content = $result;
return true;
}
}
}
}
return $this->setCache();
}
/**
* create cache of ini file
* @return bool true for successful loading config or false
*/
private function setCache( )
{
$this->content = parse_ini_file( $this->_file_ini, true );
$this->content['md5_file'] = Md5_file($this->_file_ini);
$serializeContent = '<?php return false; ?>'. serialize ( $this->content );
file_put_contents ( $this->_file_ser, $serializeContent );
return true;
}
}
/*********************************** utilisisation *****************************************************/
config.ini
#<?php return false; ?>
[General]
url = "http://www.example.com"
host = localhost
username = user
password = password
db = cms
adapter = mysql
[Path]
root = "/www/root/"
/*** test ***/
<?php
require_once ("config.php");
$test = new Config("config.ini");
print '<pre> Tableau des valeur de général : '; print_r( $test-> getParameterByName("General") ); print '</pre>';
?>
les informaticiens avec obama.. |
Page d'accueil
| requete HTTP et reponse avec curl ..
» Catégorie
divers
Poster un commentaire