[ PHPXref.com ] [ Generated: Sun Jul 20 19:19:39 2008 ] [ PEAR 1.4.5 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> Config.php (source)

   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: Config.php,v 1.21 2005/12/24 02:24:30 aashley Exp $
  19  
  20  require_once ('PEAR.php');
  21  require_once ('Config/Container.php');
  22  
  23  $GLOBALS['CONFIG_TYPES'] = 
  24          array(
  25              'apache'        =>array('Config/Container/Apache.php','Config_Container_Apache'),
  26              'genericconf'   =>array('Config/Container/GenericConf.php','Config_Container_GenericConf'),
  27              'inifile'       =>array('Config/Container/IniFile.php','Config_Container_IniFile'),
  28              'inicommented'  =>array('Config/Container/IniCommented.php','Config_Container_IniCommented'),
  29              'phparray'      =>array('Config/Container/PHPArray.php','Config_Container_PHPArray'),
  30              'xml'           =>array('Config/Container/XML.php','Config_Container_XML')
  31              );
  32  
  33  /**
  34  * Config
  35  *
  36  * This class allows for parsing and editing of configuration datasources.
  37  * Do not use this class only to read datasources because of the overhead
  38  * it creates to keep track of the configuration structure.
  39  *
  40  * @author   Bertrand Mansion <bmansion@mamasam.com>
  41  * @package  Config
  42  */
  43  class Config {
  44  
  45      /**
  46      * Datasource
  47      * Can be a file url, a dsn, an object...
  48      * @var mixed
  49      */
  50      var $datasrc;
  51  
  52      /**
  53      * Type of datasource for config
  54      * Ex: IniCommented, Apache...
  55      * @var string
  56      */
  57      var $configType = '';
  58  
  59      /**
  60      * Options for parser
  61      * @var string
  62      */
  63      var $parserOptions = array();
  64  
  65      /**
  66      * Container object
  67      * @var object
  68      */
  69      var $container;
  70  
  71      /**
  72      * Constructor
  73      * Creates a root container
  74      *
  75      * @access public
  76      */
  77      function Config()
  78      {
  79          $this->container =& new Config_Container('section', 'root');
  80      } // end constructor
  81  
  82      /**
  83      * Returns true if container is registered
  84      *
  85      * @param    string  $configType  Type of config
  86      * @access public
  87      * @return   bool
  88      */
  89      function isConfigTypeRegistered($configType)
  90      {
  91          return isset($GLOBALS['CONFIG_TYPES'][strtolower($configType)]);
  92      } // end func isConfigTypeRegistered
  93  
  94      /**
  95       * Register a new container
  96       *
  97       * @param    string       $configType  Type of config
  98       * @param    array|false  $configInfo  Array of format:
  99       *           array('path/to/Name.php',
 100       *                 'Config_Container_Class_Name').
 101       *
 102       *           If left false, defaults to:
 103       *           array('Config/Container/$configType.php',
 104       *                 'Config_Container_$configType')
 105       * @access   public
 106       * @static
 107       * @author   Greg Beaver <cellog@users.sourceforge.net>
 108       * @return   true|PEAR_Error  true on success
 109       */
 110      function registerConfigType($configType, $configInfo = false)
 111      {
 112          if (Config::isConfigTypeRegistered($configType)) {
 113              $info = $GLOBALS['CONFIG_TYPES'][strtolower($configType)];
 114              if ($info[0] == $configInfo[0] &&
 115                  $info[1] == $configInfo[1]) {
 116                  return true;
 117              } else {
 118                  return PEAR::raiseError("Config::registerConfigType registration of existing $configType failed.", null, PEAR_ERROR_RETURN);
 119              }
 120          }
 121          if (!is_array($configInfo)) {
 122              // make the normal assumption, that this is a standard config container added in at runtime
 123              $configInfo = array('Config/Container/' . $configType . '.php',
 124                                  'Config_Container_'. $configType);
 125          }
 126          $file_exists = @include_once($configInfo[0]);
 127          if ($file_exists) {
 128              if (!class_exists($configInfo[1])) {
 129                  return PEAR::raiseError("Config::registerConfigType class '$configInfo[1]' not found in $configInfo[0]", null, PEAR_ERROR_RETURN);
 130              }
 131          } else {
 132              return PEAR::raiseError("Config::registerConfigType file $configInfo[0] not found", null, PEAR_ERROR_RETURN);
 133          }
 134          $GLOBALS['CONFIG_TYPES'][strtolower($configType)] = $configInfo;
 135          return true;
 136      } // end func registerConfigType
 137  
 138      /**
 139      * Returns the root container for this config object
 140      *
 141      * @access public
 142      * @return   object  reference to config's root container object
 143      */
 144      function &getRoot()
 145      {
 146          return $this->container;
 147      } // end func getRoot
 148  
 149      /**
 150      * Sets the content of the root Config_container object.
 151      *
 152      * This method will replace the current child of the root
 153      * Config_Container object by the given object.
 154      *
 155      * @param object  $rootContainer  container to be used as the first child to root
 156      * @access public
 157      * @return   mixed    true on success or PEAR_Error
 158      */
 159      function setRoot(&$rootContainer)
 160      {
 161          if (is_object($rootContainer) && strtolower(get_class($rootContainer)) === 'config_container') {
 162              if ($rootContainer->getName() === 'root' && $rootContainer->getType() === 'section') {
 163                  $this->container =& $rootContainer;
 164              } else {
 165                  $this->container =& new Config_Container('section', 'root');
 166                  $this->container->addItem($rootContainer);
 167              }
 168              return true;
 169          } else {
 170              return PEAR::raiseError("Config::setRoot only accepts object of Config_Container type.", null, PEAR_ERROR_RETURN);
 171          }
 172      } // end func setRoot
 173  
 174      /**
 175      * Parses the datasource contents
 176      *
 177      * This method will parse the datasource given and fill the root 
 178      * Config_Container object with other Config_Container objects.
 179      *
 180      * @param mixed   $datasrc     Datasource to parse
 181      * @param string  $configType  Type of configuration
 182      * @param array   $options     Options for the parser
 183      * @access public
 184      * @return mixed PEAR_Error on error or Config_Container object
 185      */
 186      function &parseConfig($datasrc, $configType, $options = array())
 187      {
 188          $configType = strtolower($configType);
 189          if (!$this->isConfigTypeRegistered($configType)) {
 190              return PEAR::raiseError("Configuration type '$configType' is not registered in Config::parseConfig.", null, PEAR_ERROR_RETURN);
 191          }
 192          $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
 193          $className = $GLOBALS['CONFIG_TYPES'][$configType][1];
 194          include_once($includeFile);
 195  
 196          $parser = new $className($options);
 197          $error = $parser->parseDatasrc($datasrc, $this);
 198          if ($error !== true) {
 199              return $error;
 200          }
 201          $this->parserOptions = $parser->options;
 202          $this->datasrc = $datasrc;
 203          $this->configType = $configType;
 204          return $this->container;
 205      } // end func &parseConfig
 206  
 207      /**
 208      * Writes the container contents to the datasource.
 209      *
 210      * @param mixed   $datasrc     Datasource to write to
 211      * @param string  $configType  Type of configuration
 212      * @param array   $options     Options for config container
 213      * @access public
 214      * @return mixed PEAR_Error on error or true if ok
 215      */
 216      function writeConfig($datasrc = null, $configType = null, $options = array())
 217      {
 218          if (empty($datasrc)) {
 219              $datasrc = $this->datasrc;
 220          }
 221          if (empty($configType)) {
 222              $configType = $this->configType;
 223          }
 224          if (empty($options)) {
 225              $options = $this->parserOptions;
 226          }
 227          return $this->container->writeDatasrc($datasrc, $configType, $options);
 228      } // end func writeConfig
 229  } // end class Config
 230  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]