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 306597 2010-12-24 05:11:09Z aharvey $
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 'phpconstants' => array('Config/Container/PHPConstants.php', 'Config_Container_PHPConstants'),
31 'xml' => array('Config/Container/XML.php', 'Config_Container_XML')
37 * This class allows for parsing and editing of configuration datasources.
38 * Do not use this class only to read datasources because of the overhead
39 * it creates to keep track of the configuration structure.
41 * @author Bertrand Mansion <bmansion@mamasam.com>
48 * Can be a file url, a dsn, an object...
54 * Type of datasource for config
55 * Ex: IniCommented, Apache...
64 var $parserOptions = array();
74 * Creates a root container
80 $this->container = new Config_Container('section', 'root');
84 * Returns true if container is registered
86 * @param string $configType Type of config
90 function isConfigTypeRegistered($configType)
92 return isset($GLOBALS['CONFIG_TYPES'][strtolower($configType)]);
93 } // end func isConfigTypeRegistered
96 * Register a new container
98 * @param string $configType Type of config
99 * @param array|false $configInfo Array of format:
100 * array('path/to/Name.php',
101 * 'Config_Container_Class_Name').
103 * If left false, defaults to:
104 * array('Config/Container/$configType.php',
105 * 'Config_Container_$configType')
108 * @author Greg Beaver <cellog@users.sourceforge.net>
109 * @return true|PEAR_Error true on success
111 function registerConfigType($configType, $configInfo = false)
113 if (Config::isConfigTypeRegistered($configType)) {
114 $info = $GLOBALS['CONFIG_TYPES'][strtolower($configType)];
115 if ($info[0] == $configInfo[0] &&
116 $info[1] == $configInfo[1]) {
119 return PEAR::raiseError("Config::registerConfigType registration of existing $configType failed.", null, PEAR_ERROR_RETURN);
122 if (!is_array($configInfo)) {
123 // make the normal assumption, that this is a standard config container added in at runtime
124 $configInfo = array('Config/Container/' . $configType . '.php',
125 'Config_Container_'. $configType);
127 $file_exists = @include_once($configInfo[0]);
129 if (!class_exists($configInfo[1])) {
130 return PEAR::raiseError("Config::registerConfigType class '$configInfo[1]' not found in $configInfo[0]", null, PEAR_ERROR_RETURN);
133 return PEAR::raiseError("Config::registerConfigType file $configInfo[0] not found", null, PEAR_ERROR_RETURN);
135 $GLOBALS['CONFIG_TYPES'][strtolower($configType)] = $configInfo;
137 } // end func registerConfigType
140 * Returns the root container for this config object
143 * @return object reference to config's root container object
147 return $this->container;
148 } // end func getRoot
151 * Sets the content of the root Config_container object.
153 * This method will replace the current child of the root
154 * Config_Container object by the given object.
156 * @param object $rootContainer container to be used as the first child to root
158 * @return mixed true on success or PEAR_Error
160 function setRoot(&$rootContainer)
162 if (is_object($rootContainer) && strtolower(get_class($rootContainer)) === 'config_container') {
163 if ($rootContainer->getName() === 'root' && $rootContainer->getType() === 'section') {
164 $this->container =& $rootContainer;
166 $this->container = new Config_Container('section', 'root');
167 $this->container->addItem($rootContainer);
171 return PEAR::raiseError("Config::setRoot only accepts object of Config_Container type.", null, PEAR_ERROR_RETURN);
173 } // end func setRoot
176 * Parses the datasource contents
178 * This method will parse the datasource given and fill the root
179 * Config_Container object with other Config_Container objects.
181 * @param mixed $datasrc Datasource to parse
182 * @param string $configType Type of configuration
183 * @param array $options Options for the parser
185 * @return mixed PEAR_Error on error or Config_Container object
187 function &parseConfig($datasrc, $configType, $options = array())
189 $configType = strtolower($configType);
190 if (!$this->isConfigTypeRegistered($configType)) {
191 return PEAR::raiseError("Configuration type '$configType' is not registered in Config::parseConfig.", null, PEAR_ERROR_RETURN);
193 $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
194 $className = $GLOBALS['CONFIG_TYPES'][$configType][1];
195 include_once($includeFile);
197 $parser = new $className($options);
198 $error = $parser->parseDatasrc($datasrc, $this);
199 if ($error !== true) {
202 $this->parserOptions = $parser->options;
203 $this->datasrc = $datasrc;
204 $this->configType = $configType;
205 return $this->container;
206 } // end func &parseConfig
209 * Writes the container contents to the datasource.
211 * @param mixed $datasrc Datasource to write to
212 * @param string $configType Type of configuration
213 * @param array $options Options for config container
215 * @return mixed PEAR_Error on error or true if ok
217 function writeConfig($datasrc = null, $configType = null, $options = array())
219 if (empty($datasrc)) {
220 $datasrc = $this->datasrc;
222 if (empty($configType)) {
223 $configType = $this->configType;
225 if (empty($options)) {
226 $options = $this->parserOptions;
228 return $this->container->writeDatasrc($datasrc, $configType, $options);
229 } // end func writeConfig
230 } // end class Config