Witam
Jest to jeden z tych działów w których jeszcze nie pisałem... pomijając jednego posta...
A skoro już trochę tutaj siedzę to pasowało by coś tu wrzucić. Więc proszę
<?php
class configs
{
private $aConfigs , $sConfigFileName , $sConfigsDir;
public function __construct( $sConfigFileName , $sConfigsDir = 'configs' )
{
$this->aConfigs = array(); $this->sConfigFileName = $sConfigFileName;
$this->sConfigsDir = $sConfigsDir;
if( !$this->aConfigs = $this->LoadConfigs( $this->sConfigFileName ) )
{
return false;
}
return true;
}
private function LoadConfigs( $sConfigFileName )
{
if( is_file( $this->sConfigsDir . '/' . $sConfigFileName . '.php' ) ) {
include_once( $this->sConfigsDir . '/' . $sConfigFileName . '.php' );
return $config;
}
else
{
return false;
}
}
public function SaveConfigs()
{
if( is_writable( $this->sConfigsDir . '/' . $this->sConfigFileName . '.php' ) ) {
$sConfigFile = $this->GenerateConfigFile( $this->aConfigs );
if( file_put_contents( $this->sConfigsDir . '/' . $this->sConfigFileName . '.php' , $sConfigFile ) )
{
return true;
}
else
{
return false;
}
}
else
{
throw new Exception( 'Plik nie jest zapisywalny : ' . $this->sConfigsDir . '/' . $this->sConfigFileName . '.php' );
}
}
private function GenerateConfigFile( $aConfigs )
{
foreach( $aConfigs as $sKey => $mConfig )
{
$sPattern = '$config['%s'] = '%s';';
}
"// Wygenerowane automatycznien" .
"// Dnia : %sn%sn" .
"// Koniec Pliku ...n?>" ,
return $sConfigFile;
}
public function __set( $sConfigName , $mConfigValue )
{
$this->aConfigs[ $sConfigName ] = $mConfigValue;
}
public function __get( $sConfigName )
{
return $this->aConfigs[ $sConfigName ];
}
public function __isset( $sConfigName )
{
return isset( $this->aConfigs[ $sConfigName ] ); }
public function __unset( $sConfigName )
{
unset( $this->aConfigs[ $sConfigName ] ); }
}
?>
sposób użycia... jakże banalny
<?php
$alteratywna_sciezka_do_configow = 'ustawienia';
$configs = new configs( 'ustawienia_newsow' , $alteratywna_sciezka_do_configow );
$ile_na_strone = $configs->ile_na_strone;
/*...*/
$configs->ustawienie = 'nowa wartość';
$configs->SaveConfigs();
?>
Klasa generuje zwykłe pliki php w których znajduje się tablica $config zawierająca wszystko co sobie ustawiliśmy ...
jak się klasa spodoba to może coś tam jeszcze dodam

...