[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Config/Container/ -> IniFile.php (source)

   1  <?php
   2  // +----------------------------------------------------------------------+
   3  // | PHP Version 4                                                        |
   4  // +----------------------------------------------------------------------+
   5  // | Copyright (c) 1997-2003 The PHP Group                                |
   6  // +----------------------------------------------------------------------+
   7  // | This source file is subject to version 2.0 of the PHP license,       |
   8  // | that is bundled with this package in the file LICENSE, and is        |
   9  // | available at through the world-wide-web at                           |
  10  // | http://www.php.net/license/2_02.txt.                                 |
  11  // | If you did not receive a copy of the PHP license and are unable to   |
  12  // | obtain it through the world-wide-web, please send a note to          |
  13  // | license@php.net so we can mail you a copy immediately.               |
  14  // +----------------------------------------------------------------------+
  15  // | Authors: Bertrand Mansion <bmansion@mamasam.com>                     |
  16  // +----------------------------------------------------------------------+
  17  //
  18  // $Id: IniFile.php,v 1.17 2005/12/24 02:36:56 aashley Exp $
  19  
  20  /**
  21  * Config parser for PHP .ini files
  22  * Faster because it uses parse_ini_file() but get rid of comments,
  23  * quotes, types and converts On, Off, True, False, Yes, No to 0 and 1.
  24  *
  25  * @author      Bertrand Mansion <bmansion@mamasam.com>
  26  * @package     Config
  27  */
  28  class Config_Container_IniFile {
  29  
  30      /**
  31      * This class options
  32      * Not used at the moment
  33      *
  34      * @var  array
  35      */
  36      var $options = array();
  37  
  38      /**
  39      * Constructor
  40      *
  41      * @access public
  42      * @param    string  $options    (optional)Options to be used by renderer
  43      */
  44      function Config_Container_IniFile($options = array())
  45      {
  46          $this->options = $options;
  47      } // end constructor
  48  
  49      /**
  50      * Parses the data of the given configuration file
  51      *
  52      * @access public
  53      * @param string $datasrc    path to the configuration file
  54      * @param object $obj        reference to a config object
  55      * @return mixed    returns a PEAR_ERROR, if error occurs or true if ok
  56      */
  57      function &parseDatasrc($datasrc, &$obj)
  58      {
  59          $return = true;
  60          if (!file_exists($datasrc)) {
  61              return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN);
  62          }
  63          $currentSection =& $obj->container;
  64          $confArray = parse_ini_file($datasrc, true);
  65          if (!$confArray) {
  66              return PEAR::raiseError("File '$datasrc' does not contain configuration data.", null, PEAR_ERROR_RETURN);
  67          }
  68          foreach ($confArray as $key => $value) {
  69              if (is_array($value)) {
  70                  $currentSection =& $obj->container->createSection($key);
  71                  foreach ($value as $directive => $content) {
  72                      // try to split the value if comma found
  73                      if (strpos($content, '"') === false) {
  74                          $values = preg_split('/\s*,\s+/', $content);
  75                          if (count($values) > 1) {
  76                              foreach ($values as $k => $v) {
  77                                  $currentSection->createDirective($directive, $v);
  78                              }
  79                          } else {
  80                              $currentSection->createDirective($directive, $content);
  81                          }
  82                      } else {
  83                          $currentSection->createDirective($directive, $content);
  84                      }
  85                  }
  86              } else {
  87                  $currentSection->createDirective($key, $value);
  88              }
  89          }
  90          return $return;
  91      } // end func parseDatasrc
  92  
  93      /**
  94      * Returns a formatted string of the object
  95      * @param    object  $obj    Container object to be output as string
  96      * @access   public
  97      * @return   string
  98      */
  99      function toString(&$obj)
 100      {
 101          static $childrenCount, $commaString;
 102  
 103          if (!isset($string)) {
 104              $string = '';
 105          }
 106          switch ($obj->type) {
 107              case 'blank':
 108                  $string = "\n";
 109                  break;
 110              case 'comment':
 111                  $string = ';'.$obj->content."\n";
 112                  break;
 113              case 'directive':
 114                  $count = $obj->parent->countChildren('directive', $obj->name);
 115                  $content = $obj->content;
 116                  if ($content === false) {
 117                      $content = '0';
 118                  } elseif ($content === true) {
 119                      $content = '1';
 120                  } elseif (strlen(trim($content)) < strlen($content) ||
 121                            strpos($content, ',') !== false ||
 122                            strpos($content, ';') !== false ||
 123                            strpos($content, '=') !== false ||
 124                            strpos($content, '"') !== false ||
 125                            strpos($content, '%') !== false ||
 126                            strpos($content, '~') !== false) {
 127                      $content = '"'.addslashes($content).'"';          
 128                  }
 129                  if ($count > 1) {
 130                      // multiple values for a directive are separated by a comma
 131                      if (isset($childrenCount[$obj->name])) {
 132                          $childrenCount[$obj->name]++;
 133                      } else {
 134                          $childrenCount[$obj->name] = 0;
 135                          $commaString[$obj->name] = $obj->name.'=';
 136                      }
 137                      if ($childrenCount[$obj->name] == $count-1) {
 138                          // Clean the static for future calls to toString
 139                          $string .= $commaString[$obj->name].$content."\n";
 140                          unset($childrenCount[$obj->name]);
 141                          unset($commaString[$obj->name]);
 142                      } else {
 143                          $commaString[$obj->name] .= $content.', ';
 144                      }
 145                  } else {
 146                      $string = $obj->name.'='.$content."\n";
 147                  }
 148                  break;
 149              case 'section':
 150                  if (!$obj->isRoot()) {
 151                      $string = '['.$obj->name."]\n";
 152                  }
 153                  if (count($obj->children) > 0) {
 154                      for ($i = 0; $i < count($obj->children); $i++) {
 155                          $string .= $this->toString($obj->getChild($i));
 156                      }
 157                  }
 158                  break;
 159              default:
 160                  $string = '';
 161          }
 162          return $string;
 163      } // end func toString
 164  } // end class Config_Container_IniFile
 165  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7