Source for file jIniFile.class.php

Documentation is available at jIniFile.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage utils
  5. @author     Loic Mathaud
  6. @contributor Laurent Jouanneau
  7. @copyright  2006 Loic Mathaud, 2008 Laurent Jouanneau
  8. @link        http://www.jelix.org
  9. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  10. */
  11.  
  12. /**
  13. * utility class to read and write an ini file
  14. @package    jelix
  15. @subpackage utils
  16. @since 1.0b1
  17. */
  18. class jIniFile {
  19.  
  20.     /**
  21.      * read an ini file
  22.      * @param string $filename the path and the name of the file to read
  23.      * @return array the content of the file or false
  24.      */
  25.     public static function read($filename{
  26.         if file_exists ($filename) ) {
  27.             return parse_ini_file($filenametrue);
  28.         else {
  29.             return false;
  30.         }
  31.     }
  32.  
  33.     /**
  34.      * write some data in an ini file
  35.      * the data array should follow the same structure returned by
  36.      * the read method (or parse_ini_file)
  37.      * @param array $array the content of an ini file
  38.      * @param string $filename the path and the name of the file use to store the content
  39.      * @param string $header   some content to insert at the begining of the file
  40.      */
  41.     public static function write($array$filename$header=''{
  42.         $result='';
  43.         foreach ($array as $k => $v{
  44.             if (is_array($v)) {
  45.                 $result.='['.$k."]\n";
  46.                 foreach($v as $k2 => $v2){
  47.                     $result .= $k2.'='.self::_iniValue($v2)."\n";
  48.                 }
  49.             else {
  50.                 // on met les valeurs simples en debut de fichier
  51.                 $result $k.'='.self::_iniValue($v)."\n".$result;
  52.             }
  53.         }
  54.  
  55.         if ($f @fopen($filename'wb')) {
  56.             fwrite($f$header.$result);
  57.             fclose($f);
  58.         else {
  59.             // jIniFile est utilisé par le compilateur des configs
  60.             // il n'y a alors pas de $gJConfig dans de cas :
  61.             // il faut générer alors une erreur sans passer par jLocale
  62.             if(isset($GLOBALS['gJConfig'])){
  63.                 throw new jException('jelix~errors.inifile.write.error'array ($filename));
  64.             }else{
  65.                 throw new Exception('(24)Error while writing ini file '.$filename);
  66.             }
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * format a value to store in a ini file
  72.      * @param string $value the value
  73.      * @return string the formated value
  74.      */
  75.     static private function _iniValue($value){
  76.         if ($value == '' || is_numeric($value|| preg_match("/^[\w]*$/"$value)) {
  77.             return $value;
  78.         else {
  79.             return '"'.$value.'"';
  80.         }
  81.     }
  82. }
  83.  
  84. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:30 +0200 by phpDocumentor 1.4.3