2 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
18 // $Id: Config.php,v 1.4 2004-04-26 20:44:36 rurban Exp $
20 require_once('PEAR.php');
21 require_once('Config/Container.php');
23 $GLOBALS['CONFIG_TYPES'] =
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')
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.
40 * @author Bertrand Mansion <bmansion@mamasam.com>
47 * Can be a file url, a dsn, an object...
53 * Type of datasource for config
54 * Ex: IniCommented, Apache...
63 var $parserOptions = array();
73 * Creates a root container
79 $this->container =& new Config_Container('section', 'root');
83 * Returns true if container is registered
85 * @param string $configType Type of config
89 function isConfigTypeRegistered($configType)
91 return isset($GLOBALS['CONFIG_TYPES'][strtolower($configType)]);
92 } // end func isConfigTypeRegistered
95 * Register a new container
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').
102 * If left false, defaults to:
103 * array('Config/Container/$configType.php',
104 * 'Config_Container_$configType')
107 * @author Greg Beaver <cellog@users.sourceforge.net>
108 * @return true|PEAR_Error true on success
110 function registerConfigType($configType, $configInfo = false)
112 if (Config::isConfigTypeRegistered($configType)) {
113 $info = $GLOBALS['CONFIG_TYPES'][strtolower($configType)];
114 if ($info[0] == $configInfo[0] &&
115 $info[1] == $configInfo[1]) {
118 return PEAR::raiseError("Config::registerConfigType registration of existing $configType failed.", null, PEAR_ERROR_RETURN);
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);
126 $file_exists = @include_once($configInfo[0]);
128 if (!class_exists($configInfo[1])) {
129 return PEAR::raiseError("Config::registerConfigType class '$configInfo[1]' not found in $configInfo[0]", null, PEAR_ERROR_RETURN);
132 return PEAR::raiseError("Config::registerConfigType file $configInfo[0] not found", null, PEAR_ERROR_RETURN);
134 $GLOBALS['CONFIG_TYPES'][strtolower($configType)] = $configInfo;
136 } // end func registerConfigType
139 * Returns the root container for this config object
142 * @return object reference to config's root container object
146 return $this->container;
147 } // end func getRoot
150 * Sets the content of the root Config_container object.
152 * This method will replace the current child of the root
153 * Config_Container object by the given object.
155 * @param object $rootContainer container to be used as the first child to root
157 * @return mixed true on success or PEAR_Error
159 function setRoot(&$rootContainer)
161 if (is_object($rootContainer) && get_class($rootContainer) == 'config_container') {
162 $this->container =& new Config_Container('section', 'root');
163 $this->container->addItem($rootContainer);
166 return PEAR::raiseError("Config::setRoot only accepts object of Config_Container type.", null, PEAR_ERROR_RETURN);
168 } // end func setRoot
171 * Parses the datasource contents
173 * This method will parse the datasource given and fill the root
174 * Config_Container object with other Config_Container objects.
176 * @param mixed $datasrc Datasource to parse
177 * @param string $configType Type of configuration
178 * @param array $options Options for the parser
180 * @return mixed PEAR_Error on error or Config_Container object
182 function &parseConfig($datasrc, $configType, $options = array())
184 $configType = strtolower($configType);
185 if (!$this->isConfigTypeRegistered($configType)) {
186 PEAR::raiseError("Configuration type '$configType' is not registered in Config::parseConfig.", null, PEAR_ERROR_RETURN);
188 $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
189 $className = $GLOBALS['CONFIG_TYPES'][$configType][1];
190 include_once($includeFile);
192 $parser = new $className($options);
193 $error = $parser->parseDatasrc($datasrc, $this);
194 if (PEAR::isError($error)) {
195 PEAR::raiseError($error, null, PEAR_ERROR_DIE);
197 $this->parserOptions = $parser->options;
198 $this->datasrc = $datasrc;
199 $this->configType = $configType;
200 return $this->container;
201 } // end func &parseConfig
204 * Writes the container contents to the datasource.
206 * @param mixed $datasrc Datasource to write to
207 * @param string $configType Type of configuration
208 * @param array $options Options for config container
210 * @return mixed PEAR_Error on error or true if ok
212 function writeConfig($datasrc = null, $configType = null, $options = array())
214 if (empty($datasrc)) {
215 $datasrc = $this->datasrc;
217 if (empty($configType)) {
218 $configType = $this->configType;
220 if (empty($options)) {
221 $options = $this->parserOptions;
223 return $this->container->writeDatasrc($datasrc, $configType, $options);
224 } // end func writeConfig
225 } // end class Config