Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Konfiguracje PHP 5
PiratNowegoPokol...
post 15.03.2007, 20:17:43
Post #1





Grupa: Zarejestrowani
Postów: 132
Pomógł: 0
Dołączył: 12.10.2006

Ostrzeżenie: (0%)
-----


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ę
  1. <?php
  2. class configs
  3. {
  4. private $aConfigs , $sConfigFileName , $sConfigsDir;
  5.  
  6. public function __construct( $sConfigFileName , $sConfigsDir = 'configs' )
  7. {
  8. $this->aConfigs = array();
  9. $this->sConfigFileName = $sConfigFileName;
  10. $this->sConfigsDir = $sConfigsDir;
  11.  
  12. if( !$this->aConfigs = $this->LoadConfigs( $this->sConfigFileName ) )
  13. {
  14. return false;
  15. }
  16.  
  17. return true;
  18. }
  19.  
  20. private function LoadConfigs( $sConfigFileName )
  21. {
  22. if( is_file( $this->sConfigsDir . '/' . $sConfigFileName . '.php' ) )
  23. {
  24. include_once( $this->sConfigsDir . '/' . $sConfigFileName . '.php' );
  25.  
  26. return $config;
  27. }
  28. else
  29. {
  30. return false;
  31. }
  32. }
  33.  
  34. public function SaveConfigs()
  35. {
  36. if( is_writable( $this->sConfigsDir . '/' . $this->sConfigFileName . '.php' ) )
  37. {
  38. $sConfigFile = $this->GenerateConfigFile( $this->aConfigs );
  39.  
  40. if( file_put_contents( $this->sConfigsDir . '/' . $this->sConfigFileName . '.php' , $sConfigFile ) )
  41. {
  42. return true;
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. }
  49. else
  50. {
  51. throw new Exception( 'Plik nie jest zapisywalny : ' . $this->sConfigsDir . '/' . $this->sConfigFileName . '.php' );
  52. }
  53. }
  54.  
  55. private function GenerateConfigFile( $aConfigs )
  56. {
  57. $aData = array();
  58.  
  59. foreach( $aConfigs as $sKey => $mConfig )
  60. {
  61. $sPattern = '$config['%s'] = '%s';';
  62. $aData[] = sprintf( $sPattern , $sKey , var_export( $mConfig , true ) );
  63. }
  64.  
  65. $sConfigFile = sprintf("<?phpn" . 
  66.  "// Wygenerowane automatycznien" . 
  67.  "// Dnia : %sn%sn" .
  68.  "// Koniec Pliku ...n?>" ,
  69.  date( 'Y/m/d H:i:s' ), implode( "n", $aData ) );
  70. return $sConfigFile;
  71. }
  72.  
  73. public function __set( $sConfigName , $mConfigValue )
  74. {
  75. $this->aConfigs[ $sConfigName ] = $mConfigValue;
  76. }
  77.  
  78. public function __get( $sConfigName )
  79. {
  80. return $this->aConfigs[ $sConfigName ];
  81. }
  82.  
  83. public function __isset( $sConfigName )
  84. {
  85. return isset( $this->aConfigs[ $sConfigName ] );
  86. }
  87.  
  88. public function __unset( $sConfigName )
  89. {
  90. unset( $this->aConfigs[ $sConfigName ] );
  91. }
  92.  
  93. }
  94. ?>

sposób użycia... jakże banalny
  1. <?php
  2. $alteratywna_sciezka_do_configow = 'ustawienia';
  3. $configs = new configs( 'ustawienia_newsow' , $alteratywna_sciezka_do_configow );
  4. $ile_na_strone = $configs->ile_na_strone;
  5. /*...*/
  6. $configs->ustawienie = 'nowa wartość'; 
  7. $configs->SaveConfigs();
  8. ?>

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 tongue.gif ...
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 06:10