]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/pear/Config.php
locking table specific for better databases
[SourceForge/phpwiki.git] / lib / pear / Config.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: Config.php,v 1.4 2004-04-26 20:44:36 rurban 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) && get_class($rootContainer) == 'config_container') {
162             $this->container =& new Config_Container('section', 'root');
163             $this->container->addItem($rootContainer);
164             return true;
165         } else {
166             return PEAR::raiseError("Config::setRoot only accepts object of Config_Container type.", null, PEAR_ERROR_RETURN);
167         }
168     } // end func setRoot
169
170     /**
171     * Parses the datasource contents
172     *
173     * This method will parse the datasource given and fill the root 
174     * Config_Container object with other Config_Container objects.
175     *
176     * @param mixed   $datasrc     Datasource to parse
177     * @param string  $configType  Type of configuration
178     * @param array   $options     Options for the parser
179     * @access public
180     * @return mixed PEAR_Error on error or Config_Container object
181     */
182     function &parseConfig($datasrc, $configType, $options = array())
183     {
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);
187         }
188         $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
189         $className = $GLOBALS['CONFIG_TYPES'][$configType][1];
190         include_once($includeFile);
191
192         $parser = new $className($options);
193         $error = $parser->parseDatasrc($datasrc, $this);
194         if (PEAR::isError($error)) {
195             PEAR::raiseError($error, null, PEAR_ERROR_DIE);
196         }
197         $this->parserOptions = $parser->options;
198         $this->datasrc = $datasrc;
199         $this->configType = $configType;
200         return $this->container;
201     } // end func &parseConfig
202
203     /**
204     * Writes the container contents to the datasource.
205     *
206     * @param mixed   $datasrc     Datasource to write to
207     * @param string  $configType  Type of configuration
208     * @param array   $options     Options for config container
209     * @access public
210     * @return mixed PEAR_Error on error or true if ok
211     */
212     function writeConfig($datasrc = null, $configType = null, $options = array())
213     {
214         if (empty($datasrc)) {
215             $datasrc = $this->datasrc;
216         }
217         if (empty($configType)) {
218             $configType = $this->configType;
219         }
220         if (empty($options)) {
221             $options = $this->parserOptions;
222         }
223         return $this->container->writeDatasrc($datasrc, $configType, $options);
224     } // end func writeConfig
225 } // end class Config
226 ?>