Source for file basicconfigreader.php

Documentation is available at basicconfigreader.php

  1. <?php
  2. /**
  3. * Generic configuration management object
  4. *
  5. * Parses the {@link bumblebee.ini } file
  6. *
  7. * @author    Stuart Prescott
  8. * @copyright  Copyright Stuart Prescott
  9. * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version    $Id: basicconfigreader.php,v 1.13 2007/02/16 23:10:19 themill Exp $
  11. * @package    Bumblebee
  12. * @subpackage Misc
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. if (version_compare(PHP_VERSION, "5") >= 0) {
  19.   // Conditionally include the singleton functions if PHP5 is being used
  20.     require_once 'inc/bb/basicconfigreadersingleton_php5.php';
  21.  
  22. } else {
  23.   // pull in the singleton file and translate the PHP5 to PHP4 (that is still known to work)
  24.     $file = file_get_contents('inc/bb/basicconfigreadersingleton_php5.php', true);
  25.   $file = preg_replace(
  26.             array('@static function@', '@<\?php@', '@\?>@'),
  27.             array('function',          '',         ''),
  28.             $file);
  29.   eval($file);
  30. }
  31.  
  32.  
  33.   var $data;
  34.  
  35.   var $configError = false;
  36.  
  37.   var $configFileLocation = null;
  38.  
  39.   function BasicConfig() {
  40.     parent::BasicConfigReaderSingleton();
  41.   }
  42.  
  43.   function SetFileLocation($directory) {
  44.     $this->configFileLocation = $directory;
  45.   }
  46.  
  47.   function LoadFile($filename, $fatalErrors=true) {
  48.     $this->data = $this->_readConfigFile($filename, $fatalErrors);
  49.   }
  50.  
  51.   function MergeFile($filename, $section=null, $fatalErrors=true) {
  52.     $newdata = $this->_readConfigFile($filename, $fatalErrors);
  53.     $this->mergeConfig($newdata, $section);
  54.   }
  55.  
  56.   function _readConfigFile($filename, $fatalErrors=true) {
  57.     $source = $this->configFileLocation . DIRECTORY_SEPARATOR . $filename;
  58.     if (! file_exists($source) && file_exists($filename)) {
  59.       $this->configError = true;
  60.       trigger_error("System misconfiguration: I could fine the config file '$filename' but
  61.       not in the designated location.", $fatalErrors ? E_USER_ERROR : E_USER_NOTICE);
  62.     } elseif (! file_exists($source)) {
  63.       $this->configError = true;
  64.       trigger_error("System misconfiguration: I could not find the config file '$filename'. Please give me a config file so I can do something useful.", $fatalErrors ? E_USER_ERROR : E_USER_NOTICE);
  65.     }
  66.     $newdata = parse_ini_file($source, 1);
  67.     if (! is_array($newdata)) {
  68.       // if the config file doesn't exist, then we're pretty much stuffed
  69.       $this->configError = true;
  70.       trigger_error("System misconfiguration: I could not find the config file '$filename'. Please give me a config file so I can do something useful.", $fatalErrors ? E_USER_ERROR : E_USER_NOTICE);
  71.     }
  72.     return $newdata;
  73.   }
  74.  
  75.   function value($section, $parameter, $default=null) {
  76.     if (isset($this->data[$section]) && is_array($this->data[$section])) {
  77.       return issetSet($this->data[$section], $parameter, $default);
  78.     } else {
  79.       return $default;
  80.     }
  81.   }
  82.  
  83.   function getSection($section) {
  84.     if(isset($this->data[$section]) && is_array($this->data[$section])) {
  85.       return $this->data[$section];
  86.     } else {
  87.       trigger_error("Tried to access non existent section");
  88.     }
  89.   }
  90.  
  91.   function getSections() {
  92.     return array_keys($this->data);
  93.   }
  94.  
  95.   /** merge an array into the config data. If a section is provided the array is added as a new sub key */
  96.   function mergeConfig($other_array, $section = null) {
  97.     if(is_array($other_array)) {
  98.       if($section == null) {
  99.         $this->data = array_merge($this->data, $other_array);
  100.       } else if(!isset($this->data[$section])) {
  101.         $this->data = array_merge($this->data, array($section => $other_array));
  102.       } else {
  103.         $tmp = $this->data[$section];
  104.         $tmp = array_merge($tmp, $other_array);
  105.         $this->data[$section] = $tmp;
  106.       }
  107.     } else {
  108.       trigger_error("Tried to merge a non array into the config values");
  109.     }
  110.   }
  111.  
  112.   function mergeDatabaseTable($table = 'settings', $sectionColumn = 'section',
  113.                               $parameterColumn = 'parameter', $valueColumn = 'value') {
  114.     global $TABLEPREFIX;
  115.     $q = "SELECT $sectionColumn as section, $parameterColumn as parameter, $valueColumn as value from $TABLEPREFIX$table";
  116.     $sql = db_get($q, false);
  117.     while ($g = db_fetch_array($sql)) {
  118.       if (! isset($this->data[$g['section']]) || ! is_array($this->data[$g['section']])) {
  119.         $this->data[$g['section']] = array();
  120.       }
  121.       $this->data[$g['section']][$g['parameter']] = $g['value'];
  122.       //printf('[%s]::$s = %s', $g['section'], $g['parameter'], $g['value']);
  123.     }
  124.   }
  125.  
  126. }
  127.  
  128.  
  129. ?>

Documentation generated on Tue, 06 Mar 2007 10:00:40 +0000 by phpDocumentor 1.3.0