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.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.  
  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.         $return = true;
  59.         if (!file_exists($datasrc)) {
  60.             return PEAR::raiseError("Datasource file does not exist."nullPEAR_ERROR_RETURN);
  61.         }
  62.         $currentSection =$obj->container;
  63.         $confArray parse_ini_file($datasrctrue);
  64.         if (!$confArray{
  65.             return PEAR::raiseError("File '$datasrc' does not contain configuration data."nullPEAR_ERROR_RETURN);
  66.         }
  67.         foreach ($confArray as $key => $value{
  68.             if (is_array($value)) {
  69.                 $currentSection =$obj->container->createSection($key);
  70.                 foreach ($value as $directive => $content{
  71.                     // try to split the value if comma found
  72.                     if (strpos($content'"'=== false{
  73.                         $values preg_split('/\s*,\s+/'$content);
  74.                         if (count($values> 1{
  75.                             foreach ($values as $k => $v{
  76.                                 $currentSection->createDirective($directive$v);
  77.                             }
  78.                         else {
  79.                             $currentSection->createDirective($directive$content);
  80.                         }
  81.                     else {
  82.                         $currentSection->createDirective($directive$content);
  83.                     }
  84.                 }
  85.             else {
  86.                 $currentSection->createDirective($key$value);
  87.             }
  88.         }
  89.         return $return;
  90.     // end func parseDatasrc
  91.  
  92.     /**
  93.     * Returns a formatted string of the object
  94.     * @param    object  $obj    Container object to be output as string
  95.     * @access   public
  96.     * @return   string 
  97.     */
  98.     function toString(&$obj)
  99.     {
  100.         static $childrenCount$commaString;
  101.  
  102.         if (!isset($string)) {
  103.             $string '';
  104.         }
  105.         switch ($obj->type{
  106.             case 'blank':
  107.                 $string "\n";
  108.                 break;
  109.             case 'comment':
  110.                 $string ';'.$obj->content."\n";
  111.                 break;
  112.             case 'directive':
  113.                 $count $obj->parent->countChildren('directive'$obj->name);
  114.                 $content $obj->content;
  115.                 if ($content === false{
  116.                     $content '0';
  117.                 elseif ($content === true{
  118.                     $content '1';
  119.                 elseif (strlen(trim($content)) strlen($content||
  120.                           strpos($content','!== false ||
  121.                           strpos($content';'!== false ||
  122.                           strpos($content'='!== false ||
  123.                           strpos($content'"'!== false ||
  124.                           strpos($content'%'!== false ||
  125.                           strpos($content'~'!== false{
  126.                     $content '"'.addslashes($content).'"';          
  127.                 }
  128.                 if ($count > 1{
  129.                     // multiple values for a directive are separated by a comma
  130.                     if (isset($childrenCount[$obj->name])) {
  131.                         $childrenCount[$obj->name]++;
  132.                     else {
  133.                         $childrenCount[$obj->name= 0;
  134.                         $commaString[$obj->name$obj->name.'=';
  135.                     }
  136.                     if ($childrenCount[$obj->name== $count-1{
  137.                         // Clean the static for future calls to toString
  138.                         $string .= $commaString[$obj->name].$content."\n";
  139.                         unset($childrenCount[$obj->name]);
  140.                         unset($commaString[$obj->name]);
  141.                     else {
  142.                         $commaString[$obj->name.= $content.', ';
  143.                     }
  144.                 else {
  145.                     $string $obj->name.'='.$content."\n";
  146.                 }
  147.                 break;
  148.             case 'section':
  149.                 if (!$obj->isRoot()) {
  150.                     $string '['.$obj->name."]\n";
  151.                 }
  152.                 if (count($obj->children> 0{
  153.                     for ($i = 0; $i count($obj->children)$i++{
  154.                         $string .= $this->toString($obj->getChild($i));
  155.                     }
  156.                 }
  157.                 break;
  158.             default:
  159.                 $string '';
  160.         }
  161.         return $string;
  162.     // end func toString
  163. // end class Config_Container_IniFile
  164. ?>

Documentation generated on Mon, 11 Mar 2019 14:57:44 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.