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 306537 2010-12-21 08:09:34Z cweiske $
  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.         $return = true;
  69.         if (!is_readable($datasrc)) {
  70.             return PEAR::raiseError("Datasource file cannot be read."nullPEAR_ERROR_RETURN);
  71.         }
  72.  
  73.         $lines file($datasrc);
  74.         $n = 0;
  75.         $lastline '';
  76.         $currentSection =$obj->container;
  77.         foreach ($lines as $line{
  78.             $n++;
  79.             if (!preg_match('/^\s*'.$this->options['comment'].'/'$line&& 
  80.                  preg_match('/^\s*(.*)'.$this->options['newline'].'\s*$/'$line$match)) {
  81.                 // directive on more than one line
  82.                 $lastline .= $match[1];
  83.                 continue;
  84.             }
  85.             if ($lastline != ''{
  86.                 $line $lastline.trim($line);
  87.                 $lastline '';
  88.             }
  89.             if (preg_match('/^\s*'.$this->options['comment'].'+\s*(.*?)\s*$/'$line$match)) {
  90.                 // a comment
  91.                 $currentSection->createComment($match[1]);
  92.             elseif (preg_match('/^\s*$/'$line)) {
  93.                 // a blank line
  94.                 $currentSection->createBlank();
  95.             elseif (preg_match('/^\s*([\w-]+)\s*'.$this->options['equals'].'\s*((.*?)|)\s*$/'$line$match)) {
  96.                 // a directive
  97.                 $currentSection->createDirective($match[1]$match[2]);
  98.             else {
  99.                 return PEAR::raiseError("Syntax error in '$datasrc' at line $n."nullPEAR_ERROR_RETURN);
  100.             }
  101.         }
  102.         return $return;
  103.     // end func parseDatasrc
  104.  
  105.     /**
  106.     * Returns a formatted string of the object
  107.     * @param    object  $obj    Container object to be output as string
  108.     * @access public
  109.     * @return string 
  110.     */
  111.     function toString(&$obj)
  112.     {
  113.         $string '';
  114.         switch ($obj->type{
  115.             case 'blank':
  116.                 $string "\n";
  117.                 break;
  118.             case 'comment':
  119.                 $string $this->options['comment'].$obj->content."\n";
  120.                 break;
  121.             case 'directive':
  122.                 $string $obj->name.$this->options['equals'].$obj->content."\n";
  123.                 break;
  124.             case 'section':
  125.                 // How to deal with sections ???
  126.                 if (count($obj->children> 0{
  127.                     for ($i = 0; $i count($obj->children)$i++{
  128.                         $string .= $this->toString($obj->getChild($i));
  129.                     }
  130.                 }
  131.                 break;
  132.             default:
  133.                 $string '';
  134.         }
  135.         return $string;
  136.     // end func toString
  137. // end class Config_Container_GenericConf
  138. ?>

Documentation generated on Mon, 11 Mar 2019 15:42:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.