Config
[ class tree: Config ] [ index: Config ] [ all elements ]

Source for file IniFile.php

Documentation is available at IniFile.php

  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.14 2003/11/29 09:31:26 mansion 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.  
  29.     /**
  30.     * This class options
  31.     * Not used at the moment
  32.     *
  33.     * @var  array 
  34.     */
  35.     var $options = array();
  36.  
  37.     /**
  38.     * Constructor
  39.     *
  40.     * @access public
  41.     * @param    string  $options    (optional)Options to be used by renderer
  42.     */
  43.     function Config_Container_IniFile($options = array())
  44.     {
  45.         $this->options = $options;
  46.     // end constructor
  47.  
  48.     /**
  49.     * Parses the data of the given configuration file
  50.     *
  51.     * @access public
  52.     * @param string $datasrc    path to the configuration file
  53.     * @param object $obj        reference to a config object
  54.     * @return mixed    returns a PEAR_ERROR, if error occurs or true if ok
  55.     */
  56.     function &parseDatasrc($datasrc&$obj)
  57.     {
  58.         if (!file_exists($datasrc)) {
  59.             return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);
  60.         }
  61.         $currentSection =$obj->container;
  62.         $confArray parse_ini_file($datasrctrue);
  63.         if (!$confArray{
  64.             return PEAR::raiseError("File '$datasrc' does not contain configuration data."nullPEAR_ERROR_RETURN);
  65.         }
  66.         foreach ($confArray as $key => $value{
  67.             if (is_array($value)) {
  68.                 $currentSection =$obj->container->createSection($key);
  69.                 foreach ($value as $directive => $content{
  70.                     // try to split the value if comma found
  71.                     if (strpos($content'"'=== false{
  72.                         $values preg_split('/\s*,\s+/'$content);
  73.                         if (count($values> 1{
  74.                             foreach ($values as $k => $v{
  75.                                 $currentSection->createDirective($directive$v);
  76.                             }
  77.                         else {
  78.                             $currentSection->createDirective($directive$content);
  79.                         }
  80.                     else {
  81.                         $currentSection->createDirective($directive$content);
  82.                     }
  83.                 }
  84.             else {
  85.                 $currentSection->createDirective($key$value);
  86.             }
  87.         }
  88.         return true;
  89.     // end func parseDatasrc
  90.  
  91.     /**
  92.     * Returns a formatted string of the object
  93.     * @param    object  $obj    Container object to be output as string
  94.     * @access   public
  95.     * @return   string 
  96.     */
  97.     function toString(&$obj)
  98.     {
  99.         static $childrenCount$commaString;
  100.  
  101.         if (!isset($string)) {
  102.             $string '';
  103.         }
  104.         switch ($obj->type{
  105.             case 'blank':
  106.                 $string "\n";
  107.                 break;
  108.             case 'comment':
  109.                 $string ';'.$obj->content."\n";
  110.                 break;
  111.             case 'directive':
  112.                 $count $obj->parent->countChildren('directive'$obj->name);
  113.                 $content $obj->content;
  114.                 if ($content === false{
  115.                     $content '0';
  116.                 elseif ($content === true{
  117.                     $content '1';
  118.                 elseif (strlen(trim($content)) strlen($content||
  119.                           strpos($content','!== false ||
  120.                           strpos($content';'!== false ||
  121.                           strpos($content'"'!== false ||
  122.                           strpos($content'%'!== false{
  123.                     $content '"'.addslashes($content).'"';          
  124.                 }
  125.                 if ($count > 1{
  126.                     // multiple values for a directive are separated by a comma
  127.                     if (isset($childrenCount[$obj->name])) {
  128.                         $childrenCount[$obj->name]++;
  129.                     else {
  130.                         $childrenCount[$obj->name= 0;
  131.                         $commaString[$obj->name$obj->name.'=';
  132.                     }
  133.                     if ($childrenCount[$obj->name== $count-1{
  134.                         // Clean the static for future calls to toString
  135.                         $string .= $commaString[$obj->name].$content."\n";
  136.                         unset($childrenCount[$obj->name]);
  137.                         unset($commaString[$obj->name]);
  138.                     else {
  139.                         $commaString[$obj->name.= $content.', ';
  140.                     }
  141.                 else {
  142.                     $string $obj->name.'='.$content."\n";
  143.                 }
  144.                 break;
  145.             case 'section':
  146.                 if (!$obj->isRoot()) {
  147.                     $string '['.$obj->name."]\n";
  148.                 }
  149.                 if (count($obj->children> 0{
  150.                     for ($i = 0; $i count($obj->children)$i++{
  151.                         $string .= $this->toString($obj->getChild($i));
  152.                     }
  153.                 }
  154.                 break;
  155.             default:
  156.                 $string '';
  157.         }
  158.         return $string;
  159.     // end func toString
  160. // end class Config_Container_IniFile
  161. ?>

Documentation generated on Mon, 11 Mar 2019 10:17:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.