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

Source for file GenericConf.php

Documentation is available at GenericConf.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. // | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: GenericConf.php,v 1.6 2003/03/24 14:16:52 mansion Exp $
  19.  
  20. /**
  21. * Config parser for  generic .conf files like
  22. * htdig.conf...
  23. *
  24. @author      Bertrand Mansion <bmansion@mamasam.com>
  25. @package     Config
  26. */
  27.  
  28.     /**
  29.     * This class options:
  30.     *   Ex: $options['comment'] = '#';
  31.     *   Ex: $options['equals'] = ':';
  32.     *   Ex: $options['newline'] = '\\';
  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_GenericConf($options = array())
  45.     {
  46.         if (empty($options['comment'])) {
  47.             $options['comment''#';
  48.         }
  49.         if (empty($options['equals'])) {
  50.             $options['equals'':';
  51.         }
  52.         if (empty($options['newline'])) {
  53.             $options['newline''\\';
  54.         }
  55.         $this->options = $options;
  56.     // end constructor
  57.  
  58.     /**
  59.     * Parses the data of the given configuration file
  60.     *
  61.     * @access public
  62.     * @param string $datasrc    path to the configuration file
  63.     * @param object $obj        reference to a config object
  64.     * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
  65.     */
  66.     function &parseDatasrc($datasrc&$obj)
  67.     {
  68.         if (!is_readable($datasrc)) {
  69.             return PEAR::raiseError("Datasource file cannot be read."nullPEAR_ERROR_RETURN);
  70.         }
  71.  
  72.         $lines file($datasrc);
  73.         $n = 0;
  74.         $lastline '';
  75.         $currentSection =$obj->container;
  76.         foreach ($lines as $line{
  77.             $n++;
  78.             if (!preg_match('/^\s*'.$this->options['comment'].'/'$line&& 
  79.                  preg_match('/^\s*(.*)\s+'.$this->options['newline'].'\s*$/'$line$match)) {
  80.                 // directive on more than one line
  81.                 $lastline .= $match[1].' ';
  82.                 continue;
  83.             }
  84.             if ($lastline != ''{
  85.                 $line $lastline.trim($line);
  86.                 $lastline '';
  87.             }
  88.             if (preg_match('/^\s*'.$this->options['comment'].'+\s*(.*?)\s*$/'$line$match)) {
  89.                 // a comment
  90.                 $currentSection->createComment($match[1]);
  91.             elseif (preg_match('/^\s*$/'$line)) {
  92.                 // a blank line
  93.                 $currentSection->createBlank();
  94.             elseif (preg_match('/^\s*(\w+)'.$this->options['equals'].'\s*((.*?)|)\s*$/'$line$match)) {
  95.                 // a directive
  96.                 $currentSection->createDirective($match[1]$match[2]);
  97.             else {
  98.                 return PEAR::raiseError("Syntax error in '$datasrc' at line $n."nullPEAR_ERROR_RETURN);
  99.             }
  100.         }
  101.         return true;
  102.     // end func parseDatasrc
  103.  
  104.     /**
  105.     * Returns a formatted string of the object
  106.     * @param    object  $obj    Container object to be output as string
  107.     * @access public
  108.     * @return string 
  109.     */
  110.     function toString(&$obj)
  111.     {
  112.         $string '';
  113.         switch ($obj->type{
  114.             case 'blank':
  115.                 $string "\n";
  116.                 break;
  117.             case 'comment':
  118.                 $string $this->options['comment'].$obj->content."\n";
  119.                 break;
  120.             case 'directive':
  121.                 $string $obj->name.$this->options['equals'].$obj->content."\n";
  122.                 break;
  123.             case 'section':
  124.                 // How to deal with sections ???
  125.                 if (count($obj->children> 0{
  126.                     for ($i = 0; $i count($obj->children)$i++{
  127.                         $string .= $this->toString($obj->getChild($i));
  128.                     }
  129.                 }
  130.                 break;
  131.             default:
  132.                 $string '';
  133.         }
  134.         return $string;
  135.     // end func toString
  136. // end class Config_Container_GenericConf
  137. ?>

Documentation generated on Mon, 11 Mar 2019 13:51:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.